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



我按照本文中的示例将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
#  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)

希望这个例子有帮助。

最新更新