简体   繁体   English

如何有效地将多个 json 文件读入 Dataframe 或 JavaRDD?

[英]How can I efficiently read multiple json files into a Dataframe or JavaRDD?

I can use the following code to read a single json file but I need to read multiple json files and merge them into one Dataframe.我可以使用以下代码读取单个 json 文件,但我需要读取多个 json 文件并将它们合并到一个 Dataframe 中。 How can I do this?我怎样才能做到这一点?

DataFrame jsondf = sqlContext.read().json("/home/spark/articles/article.json");

Or is there a way to read multiple json files into JavaRDD then convert to Dataframe?或者有没有办法将多个json文件读入JavaRDD然后转换为Dataframe?

To read multiple inputs in Spark, use wildcards. 要在Spark中读取多个输入,请使用通配符。 That's going to be true whether you're constructing a dataframe or an rdd. 无论您是构建数据帧还是rdd,情况都是如此。

context.read().json("/home/spark/articles/*.json")
// or getting json out of s3
context.read().json("s3n://bucket/articles/201510*/*.json")

You can use exactly the same code to read multiple JSON files. 您可以使用完全相同的代码来读取多个JSON文件。 Just pass a path-to-a-directory / path-with-wildcards instead of path to a single file. 只需将路径传递到目录/路径与通配符而不是路径到单个文件。

DataFrameReader also provides json method with a following signature : DataFrameReader还为json方法提供以下签名

json(jsonRDD: JavaRDD[String])

which can be used to parse JSON already loaded into JavaRDD . 可用于解析已加载到JavaRDD JSON。

function spark.read.json accepts list of file as a parameter.函数spark.read.json接受文件列表作为参数。

spark.read.json(List_all_json file)

This will read all the files in the list and return a single data frame for all the information in the files.这将读取列表中的所有文件并返回文件中所有信息的单个数据框。

Function json(String... paths) takes variable arguments. 函数json(String... paths)采用变量参数。 ( documentation ) 文件

So you can change your code like this: 所以你可以改变你的代码:

sqlContext.read().json(file1, file2, ...)

Using pyspark, if you have all the json files in the same folder, you can use df = spark.read.json('folder_path') .使用 pyspark,如果所有 json 文件都在同一个文件夹中,则可以使用df = spark.read.json('folder_path') This instruction will load all the json files inside the folder.此指令将加载文件夹中的所有 json 文件。

For reading performance, I recommend you for providing dataframe the schema:为了读取性能,我建议您提供数据框架构:

import pyspark.sql.types as T

billing_schema = billing_schema = T.StructType([
  T.StructField('accountId', T.LongType(),True),
  T.StructField('accountName',T.StringType(),True),
  T.StructField('accountOwnerEmail',T.StringType(),True),
  T.StructField('additionalInfo',T.StringType(),True),
  T.StructField('chargesBilledSeparately',T.BooleanType(),True),
  T.StructField('consumedQuantity',T.DoubleType(),True),
  T.StructField('consumedService',T.StringType(),True),
  T.StructField('consumedServiceId',T.LongType(),True),
  T.StructField('cost',T.DoubleType(),True),
  T.StructField('costCenter',T.StringType(),True),
  T.StructField('date',T.StringType(),True),
  T.StructField('departmentId',T.LongType(),True),
  T.StructField('departmentName',T.StringType(),True),
  T.StructField('instanceId',T.StringType(),True),
  T.StructField('location',T.StringType(),True),
  T.StructField('meterCategory',T.StringType(),True),
  T.StructField('meterId',T.StringType(),True),
  T.StructField('meterName',T.StringType(),True),
  T.StructField('meterRegion',T.StringType(),True),
  T.StructField('meterSubCategory',T.StringType(),True),
  T.StructField('offerId',T.StringType(),True),
  T.StructField('partNumber',T.StringType(),True),
  T.StructField('product',T.StringType(),True),
  T.StructField('productId',T.LongType(),True),
  T.StructField('resourceGroup',T.StringType(),True),
  T.StructField('resourceGuid',T.StringType(),True),
  T.StructField('resourceLocation',T.StringType(),True),
  T.StructField('resourceLocationId',T.LongType(),True),
  T.StructField('resourceRate',T.DoubleType(),True),
  T.StructField('serviceAdministratorId',T.StringType(),True),
  T.StructField('serviceInfo1',T.StringType(),True),
  T.StructField('serviceInfo2',T.StringType(),True),
  T.StructField('serviceName',T.StringType(),True),
  T.StructField('serviceTier',T.StringType(),True),
  T.StructField('storeServiceIdentifier',T.StringType(),True),
  T.StructField('subscriptionGuid',T.StringType(),True),
  T.StructField('subscriptionId',T.LongType(),True),
  T.StructField('subscriptionName',T.StringType(),True),
  T.StructField('tags',T.StringType(),True),
  T.StructField('unitOfMeasure',T.StringType(),True)
])

billing_df = spark.read.json('/mnt/billingsources/raw-files/202106/', schema=billing_schema)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM