简体   繁体   English

Spark将JSON字符串转换为JSON对象(Java)

[英]Spark convert JSON string to JSON object (Java)

I've been looking through examples of conversion from JSON string which was received from Kafka to JSON object. 我一直在研究从Kafka接收到的从JSON字符串转换为JSON对象的示例。 I've found only Scala examples which were not very helpful to me. 我发现只有Scala示例对我不是很有帮助。

However in Scala it looks like that: 但是在Scala中,它看起来像这样:

val schema = new StructType().add("b", IntegerType)
events.select(from_json('a, schema) as 'c)

Is it possible to do that in Java? 用Java可以做到吗?

Yes, it is possible to do this in Java. 是的,可以用Java做到这一点。 But with a slightly different syntax. 但是语法略有不同。 Like this - 像这样 -

StructType schema = new StructType().add("b", DataTypes.IntegerType);
events.select(from_json(col("a"), schema).as("c"));

I hope it helps! 希望对您有所帮助!

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

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