简体   繁体   English

将列从 StringType 转换为 Json(对象)

[英]Convert a column from StringType to Json (object)

Here is a sample data这是一个示例数据

val df4 = sc.parallelize(List(
  ("A1",45, "5", 1, 90),
  ("A2",60, "1", 1, 120),
  ("A6", 30, "9", 1, 450),
  ("A7", 89, "7", 1, 333),
  ("A7", 89, "4", 1, 320),
  ("A2",60, "5", 1, 22),
  ("A1",45, "22", 1, 1)
)).toDF("CID","age", "children", "marketplace_id","value")

thanks to @Shu for this piece of code感谢@Shu 提供这段代码

val df5 = df4.selectExpr("CID","""to_json(named_struct("id", children)) as item""", "value", "marketplace_id")
+---+-----------+-----+--------------+
|CID|item       |value|marketplace_id|
+---+-----------+-----+--------------+
|A1 |{"id":"5"} |90   |1             |
|A2 |{"id":"1"} |120  |1             |
|A6 |{"id":"9"} |450  |1             |
|A7 |{"id":"7"} |333  |1             |
|A7 |{"id":"4"} |320  |1             |
|A2 |{"id":"5"} |22   |1             |
|A1 |{"id":"22"}|1    |1             |
+---+-----------+-----+--------------+

when you do df5.dtypes当你做df5.dtypes

(CID,StringType), (item,StringType), (value,IntegerType), (marketplace_id,IntegerType)

the column item is of string type, is there a way this can be of json/object type(if that is a thing)?列项目是字符串类型,有没有办法可以是 json/object 类型(如果这是一个东西)?

EDIT 1: I will describe what I am trying to achieve here, the above two steps remains same.编辑1:我将在这里描述我想要实现的目标,上述两个步骤保持不变。

val w = Window.partitionBy("CID").orderBy(desc("value"))
val sorted_list = df5.withColumn("item", collect_list("item").over(w)).groupBy("CID").agg(max("item") as "item")

Output: Output:

+---+-------------------------+
|CID|item                     |
+---+-------------------------+
|A6 |[{"id":"9"}]             |
|A2 |[{"id":"1"}, {"id":"5"}] |
|A7 |[{"id":"7"}, {"id":"4"}] |
|A1 |[{"id":"5"}, {"id":"22"}]|
+---+-------------------------+

now whatever is inside [ ] is a string.现在[ ]里面的任何东西都是一个字符串。 which is causing a problem for one of the tools we are using.这导致我们正在使用的工具之一出现问题。

Sorry, pardon me I am new to scala, spark if this is a basic question.对不起,对不起,我是 scala 的新手,如果这是一个基本问题,请火花。

Store json data using struct type, check below code.使用struct类型存储json数据,检查下面的代码。

scala> dfa
.withColumn("item_without_json",struct($"cid".as("id")))
.withColumn("item_as_json",to_json($"item_without_json"))
.show(false)

+---+-----------+-----+--------------+-----------------+------------+
|CID|item       |value|marketplace_id|item_without_json|item_as_json|
+---+-----------+-----+--------------+-----------------+------------+
|A1 |{"id":"A1"}|90   |1             |[A1]             |{"id":"A1"} |
|A2 |{"id":"A2"}|120  |1             |[A2]             |{"id":"A2"} |
|A6 |{"id":"A6"}|450  |1             |[A6]             |{"id":"A6"} |
|A7 |{"id":"A7"}|333  |1             |[A7]             |{"id":"A7"} |
|A7 |{"id":"A7"}|320  |1             |[A7]             |{"id":"A7"} |
|A2 |{"id":"A2"}|22   |1             |[A2]             |{"id":"A2"} |
|A1 |{"id":"A1"}|1    |1             |[A1]             |{"id":"A1"} |
+---+-----------+-----+--------------+-----------------+------------+

Based on the comment you made to have the dataset converted to json you would use:根据您将数据集转换为 json 的评论,您将使用:

df4
  .select(collect_list(struct($"CID".as("id"))).as("items"))
  .write()
  .json(path)

The output will look like: output 将如下所示:

{"items":[{"id":"A1"},{"id":"A2"},{"id":"A6"},{"id":"A7"}, ...]}

If you need the thing in memory to pass down to a function, instead of write().json(...) use toJSON如果您需要 memory 中的内容传递给 function,而不是write().json(...)使用toJSON

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

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