天真的安装PySpark还支持S3访问 [英] Naive install of PySpark to also support S3 access

查看:90
本文介绍了天真的安装PySpark还支持S3访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从PySpark读取存储在S3上的Parquet数据.

I would like to read Parquet data stored on S3 from PySpark.

我从这里下载了spark:

I've downloaded spark from here:

http://www.apache.org/dist/spark/spark-2.1.0/spark-2.1.0-bin-hadoop2.7.tgz

并天真地将其安装到Python

And installed it to Python naively

cd python
python setup.py install

这似乎运行良好,我可以导入pyspark,创建SparkContext等.但是,当我阅读一些可公开访问的镶木地板数据时,会得到以下信息:

This seems to function fine and I can import pyspark, make a SparkContext, etc.. However when I go to read some publicly accessible parquet data I get the following:

import pyspark
sc = pyspark.SparkContext('local[4]')
sql = pyspark.SQLContext(sc)
df = sql.read.parquet('s3://bucket-name/mydata.parquet')

我收到以下异常

Py4JJavaError: An error occurred while calling o55.parquet.
: java.io.IOException: No FileSystem for scheme: s3
    at org.apache.hadoop.fs.FileSystem.getFileSystemClass(FileSystem.java:2660)
    at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:2667)
    at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:94)
    at org.apache.hadoop.fs.FileSystem$Cache.getInternal(FileSystem.java:2703)
    at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:2685)
    at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:373)
    at org.apache.hadoop.fs.Path.getFileSystem(Path.java:295)
    at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$14.apply(DataSource.scala:372)
    at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$14.apply(DataSource.scala:370)
    at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:241)
    at scala.collection.TraversableLike$$anonfun$flatMap$1.apply(TraversableLike.scala:241)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at scala.collection.TraversableLike$class.flatMap(TraversableLike.scala:241)
    at scala.collection.immutable.List.flatMap(List.scala:344)
    at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:370)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:152)
    at org.apache.spark.sql.DataFrameReader.parquet(DataFrameReader.scala:441)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:280)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:214)
    at java.lang.Thread.run(Thread.java:745)

此错误从Google搜索中弹出.到目前为止,所提供的解决方案均无济于事.

This error pops up a bit from google searches. So far none of the solutions provided have been helpful.

我在个人计算机上的Linux(Ubuntu 16.04)操作系统上,没有安装太多其他东西(一切都很正常).

I'm on Linux (Ubuntu 16.04) on a personal computer without much else installed (everything is pretty stock).

我降级为

I downgraded to http://www.apache.org/dist/spark/spark-2.1.0/spark-2.1.0-bin-hadoop2.4.tgz to have AWS included by default.

很不幸,现在我的AWS凭证没有被领取.我已经尝试了几件事:

Now unfortunately my AWS credentials aren't being picked up. I've tried a few things:

  1. 将它们包含为SparkConf参数

  1. Including them as SparkConf parameters

conf = (pyspark.SparkConf()
               .set('fs.s3.awsAccessKeyId', ...')
               .set('fs.s3.awsSecretAccessKey', '...'))
sc = pyspark.SparkContext('local[4]', conf=conf)

  • 将它们包含在我的本地.aws/credentials文件中
  • 将它们包含在URL中(由于我的访问密钥带有正斜杠,因此无效)
  • 不幸的是,在所有情况下,我都会收到如下所示的回溯

    Unfortunately in all cases I receive a traceback like the following

    IllegalArgumentException: 'AWS Access Key ID and Secret Access Key must be specified as the username or password (respectively) of a s3 URL, or by setting the fs.s3.awsAccessKeyId or fs.s3.awsSecretAccessKey properties (respectively).'
    

    推荐答案

    使用预先构建的spark 2.X二进制文件的Hadoop-2.4构建版本(我相信该版本随附s3功能),您可以通过编程方式配置spark来提取s3数据的方式如下:

    Using the Hadoop-2.4 build of the pre-built spark 2.X binary (which I believe ships with s3 functionality) you can programmatically configure spark to pull s3 data in the following manner:

    import pyspark
    conf = pyspark.SparkConf()
    
    sc = pyspark.SparkContext('local[4]', conf=conf)
    sc._jsc.hadoopConfiguration().set("fs.s3n.awsAccessKeyId", "")
    sc._jsc.hadoopConfiguration().set("fs.s3n.awsSecretAccessKey", "")
    
    sql = pyspark.SQLContext(sc)
    df = sql.read.parquet('s3n://bucket-name/mydata.parquet')
    

    需要注意的关键是存储区URI和配置名称中的前缀 s3n

    A critical thing to note is the prefix s3n in both the URI for the bucket and the configuration name

    这篇关于天真的安装PySpark还支持S3访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆