使用org.apache.hadoop:hadoop-aws从pyspark中的s3中读取文件

时间:2019-12-01 06:30:41

标签: apache-spark amazon-s3 pyspark

尝试使用hadoop-aws从s3中读取文件,下面提到了用于运行代码的命令。 请帮助我解决此问题,并了解我在做什么错。

# run using command
# time spark-submit --packages org.apache.hadoop:hadoop-aws:3.2.1 connect_s3_using_keys.py

from pyspark import SparkContext, SparkConf
import ConfigParser
import pyspark

# create Spark context with Spark configuration
conf = SparkConf().setAppName("Deepak_1ST_job")
sc = SparkContext(conf=conf)
sc.setLogLevel("ERROR")

hadoop_conf = sc._jsc.hadoopConfiguration()

config = ConfigParser.ConfigParser()
config.read("/home/deepak/Desktop/secure/awsCred.cnf")
accessKeyId = config.get("aws_keys", "access_key")
secretAccessKey = config.get("aws_keys", "secret_key")

hadoop_conf.set(
    "fs.s3n.impl", "org.apache.hadoop.fs.s3a.S3AFileSystem")
hadoop_conf.set("fs3a.access.key", accessKeyId)
hadoop_conf.set("s3a.secret.key", secretAccessKey)

sqlContext = pyspark.SQLContext(sc)

df = sqlContext.read.json("s3a://bucket_name/logs/20191117log.json")
df.show()

编辑1:

由于我是pyspark的新手,因此我并不了解这些依赖项,因此该错误也不容易理解。

得到错误为

File "/home/deepak/spark/spark-3.0.0-preview-bin-hadoop3.2/python/lib/pyspark.zip/pyspark/sql/utils.py", line 98, in deco
  File "/home/deepak/spark/spark-3.0.0-preview-bin-hadoop3.2/python/lib/py4j-0.10.8.1-src.zip/py4j/protocol.py", line 328, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o28.json.
: java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)V
        at org.apache.hadoop.fs.s3a.S3AUtils.lookupPassword(S3AUtils.java:816)
        at org.apache.hadoop.fs.s3a.S3AUtils.lookupPassword(S3AUtils.java:792)
        at org.apache.hadoop.fs.s3a.S3AUtils.getAWSAccessKeys(S3AUtils.java:747)
        at org.apache.hadoop.fs.s3a.SimpleAWSCredentialsProvider.

1 个答案:

答案 0 :(得分:3)

我在spark 3.0.0 / hadoop 3.2中遇到了同样的问题。

对我有用的是将hadoop-aws-3.2.1.jar中的spark-3.0.0-bin-hadoop3.2/jars替换为以下位置的hadoop-aws-3.2.0.jarhttps://mvnrepository.com/artifact/org.apache.hadoop/hadoop-aws/3.2.0