0
私は、配列<構造体の列を持ちますが、jsonファイルから導き出しました。 配列<構造体を文字列に変換したいので、この配列列をそのままハイブに保持し、RDBMSに単一の列としてエクスポートすることができます。spark scala:構造体列の配列を文字列に変換します
temp.json
{"properties":{"items":[{"invoicid":{"value":"923659"},"job_id":
{"value":"296160"},"sku_id":
{"value":"312002"}}],"user_id":"6666","zip_code":"666"}}
処理:
scala> val temp = spark.read.json("s3://check/1/temp1.json")
temp: org.apache.spark.sql.DataFrame = [properties: struct<items:
array<struct<invoicid:struct<value:string>,job_id:struct<value:string>,sku_id:struct<value:string>>>, user_id: string ... 1 more field>]
scala> temp.printSchema
root
|-- properties: struct (nullable = true)
| |-- items: array (nullable = true)
| | |-- element: struct (containsNull = true)
| | | |-- invoicid: struct (nullable = true)
| | | | |-- value: string (nullable = true)
| | | |-- job_id: struct (nullable = true)
| | | | |-- value: string (nullable = true)
| | | |-- sku_id: struct (nullable = true)
| | | | |-- value: string (nullable = true)
| |-- user_id: string (nullable = true)
| |-- zip_code: string (nullable = true)
scala> temp.select("properties").show
+--------------------+
| properties|
+--------------------+
|[WrappedArray([[9...|
+--------------------+
scala> temp.select("properties.items").show
+--------------------+
| items|
+--------------------+
|[[[923659],[29616...|
+--------------------+
scala> temp.createOrReplaceTempView("tempTable")
scala> spark.sql("select properties.items from tempTable").show
+--------------------+
| items|
+--------------------+
|[[[923659],[29616...|
+--------------------+
私のような結果を得ることができます方法:そのまま配列要素の値を取得するために
+-----------------------------------------------------------------------------------------+
| items |
+-----------------------------------------------------------------------------------------+
[{"invoicid":{"value":"923659"},"job_id":{"value":"296160"},"sku_id":{"value":"312002"}}] |
+-----------------------------------------------------------------------------------------+
。
を探している機能は非常に迅速な解決のためにあなたを.Thanksです。 –