【问题标题】:How to give Json schema file to spark 1.6 to load schema to spark DataFrame如何给 Json 模式文件以激发 1.6 以加载模式以激发 DataFrame
【发布时间】:2018-06-30 16:50:33
【问题描述】:

您好,我正在尝试加载 CSV 文件来触发数据框。我正在使用 DataBricks CSV jar 来加载数据。我在 Json 文件中有数据架构,并希望将该架构应用于 DataFrame。

以下是我的 Json 架构文件:-

 {
  "type" : "struct",
  "doc": "This is sample",
  "fields" : [ {
    "name" : "Name",
    "type" : "string" ,
    "nullable" : "true" 
  }, {
    "name" : "Address1",
    "type" : "string",
    "nullable" : "true" 
  }, {
    "name" : "Address2",
    "type" : "string",
    "nullable" : "true" 
  }, {
    "name" : "City",
    "type" : "string",
    "nullable" : "true" 
  }]
}

【问题讨论】:

    标签: java apache-spark spark-dataframe databricks


    【解决方案1】:

    以下代码可能对您有所帮助。

    StructType tempSchema = new StructType(new StructField[]{
                new StructField("name", DataTypes.StringType, true, Metadata.empty()),
                new StructField("Address1", DataTypes.StringType, true, Metadata.empty()),
                new StructField("Address2", DataTypes.StringType, true, Metadata.empty()),
                new StructField("City", DataTypes.StringType, true, Metadata.empty())
            });
    
        Dataset<Row> resultDs = spark.createDataFrame(dataRows, tempSchema);
    

    【讨论】:

    • 谢谢,斯里努...目前我正在使用这种方式为数据框提供架构。我要问的是,spark 是否有可能自动读取架构文件并从该架构文件中获取架构。
    • 好的Himanshu,请检查这可能对你有帮助github.com/databricks/spark-avro
    • 谢谢。 Srinu .. 我使用的输入数据是 CSV 格式,我认为上面的链接是针对 avro 数据文件的。我正在使用 Spark 1.6
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 2021-05-06
    • 1970-01-01
    相关资源
    最近更新 更多