json - 由于数据类型不匹配而获取 : argument 2 requires integral type error while parsing Json data Spark SQL

标签 json apache-spark apache-spark-sql spark-dataframe databricks

这个问题在这里已经有了答案:





Querying Spark SQL DataFrame with complex types

(3 个回答)


3年前关闭。




我正在尝试解析 JSON 数据,为此我为此编写了自定义架构。通过添加架构或不添加架构来解析数据时,我收到以下错误:

Exception in thread "main" org.apache.spark.sql.AnalysisException: cannot resolve '`queryResults`.`searchResponse`.`response`.`docs`.`transactions`['code']' due to data type mismatch: argument 2 requires integral type, however, ''code'' is of string type.;;

这是我的示例数据:
{
    "queryResults": {
        "searchResponse": {
            "response": {
                "docs": [{
                    "transactions": [{
                        "recordDate": "2010-02-02 00:00:00",
                        "code": "PGM/",
                        "description": "Recordation of Patent Grant Mailed"
                    }, {
                        "recordDate": "2010-01-13 00:00:00",
                        "code": "WPIR",
                        "description": "Issue Notification Mailed"
                    }, {
                        "recordDate": "2009-12-17 00:00:00",
                        "code": "R1021",
                        "description": "Receipt into Pubs"
                    }]
                }]
            }
        }
    }
}

这是我的架构:
val schema=StructType(List(
      StructField("queryResults",StructType(
        List(StructField("searchResponse",StructType(
          List(StructField("response",StructType(
            List(StructField("docs",ArrayType(StructType(
              List(
                StructField("appCustNumber", StringType, nullable = true),
                StructField("transactions",ArrayType(StructType(
                  List
                  (
                    StructField("code", StringType, nullable = true),
                    StructField("description", StringType, nullable = true),
                    StructField("recordDate", StringType, nullable = true)
                  )
                )))
              )
            ))))
          )))
        )))
      ))
    ))

这是我尝试获取数据的方式:
val dff = sqlCotext.read.schema(schema).json("file locatiuon")
dff.select("queryResults.searchResponse.response.docs.transactions.code").show()

提前致谢。

最佳答案

尝试这个 ...

val dfContent = dff.select(explode(dff("queryResults.searchResponse.response.docs.transactions"))).toDF("transaction")

val code = dfContent.select("transaction.code")

code.show(false)

关于json - 由于数据类型不匹配而获取 : argument 2 requires integral type error while parsing Json data Spark SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49644510/

相关文章:

javascript - 导出函数中 Node.js 中响应正文的未定义值。

java - 如何通过使用 scala 类扩展我的 java 类使其可写?

scala - 如何在 Spark 应用程序中进行有效的日志记录

apache-spark - Hive with Hadoop vs Hive with spark vs Spark sql vs HDFS-它们如何相互配合?

pyspark : TypeError: unsupported operand type(s) for +: 'int' and 'str'

json - 如何在 Django JSONField 数据上聚合(最小/最大等)?

php - Laravel Eloquent toArray 不使用方括号

scala - 配置 Spark 和独立的 Hive MetaStore 将 DafaFrames 持久保存到 s3

apache-spark-sql - 如何计算 pyspark dataframe 中的每日基础(时间序列)

json - 将 hashmap 转换为 JSON 对象时从 Gson 获取 stackoverflowerror