简体   繁体   中英

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. I've found only Scala examples which were not very helpful to me.

However in Scala it looks like that:

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

Is it possible to do that in Java?

Yes, it is possible to do this in 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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