从DataFrame写出后,将csv数据读入SparkR

时间:2017-06-09 17:28:15

标签: sparkr databricks

我按照this post中的示例写了一个DataFrame作为csv到AWS S3存储桶。结果不是单个文件,而是具有许多.csv文件的文件夹。我现在无法在SparkR中将此文件夹作为DataFrame读取。以下是我尝试过的内容,但它们并没有导致我写出的DataFrame相同。

write.df(df, 's3a://bucket/df', source="csv") #Creates a folder named df in S3 bucket

df_in1 <- read.df("s3a://bucket/df", source="csv")
df_in2 <- read.df("s3a://bucket/df/*.csv", source="csv")
#Neither df_in1 or df_in2 result in DataFrames that are the same as df

1 个答案:

答案 0 :(得分:0)

#  Spark 1.4 is used in this example
# 
# Download the nyc flights dataset as a CSV from https://s3-us-west-2.amazonaws.com/sparkr-data/nycflights13.csv

# Launch SparkR using 
# ./bin/sparkR --packages com.databricks:spark-csv_2.10:1.0.3

# The SparkSQL context should already be created for you as sqlContext
sqlContext
# Java ref type org.apache.spark.sql.SQLContext id 1

# Load the flights CSV file using `read.df`. Note that we use the CSV reader Spark package here.
flights <- read.df(sqlContext, "./nycflights13.csv", "com.databricks.spark.csv", header="true")

# Print the first few rows
head(flights)

希望这个例子有所帮助。