package examples
import org.apache.spark.sql.SparkSession
object ReadJsonStringEXP {
def main(args: Array[String]): Unit = {
val session = SparkSession.builder().master("local").appName("t5").getOrCreate()
val context = session.sparkContext
context.setLogLevel("Error")
val list = List[String](
"{\"name\":\"kobe\",\"age\":24}",
"{\"name\":\"james\",\"age\":21}",
"{\"name\":\"durant\",\"age\":22}",
"{\"name\":\"jordan\",\"age\":23}",
"{\"name\":\"yi\",\"age\":24}"
)
import session.implicits._
import org.apache.spark.sql.functions.get_json_object
val df = list.toDF("info")
df.createTempView("t")
df.select($"info",get_json_object($"info","$.name").as("name"),get_json_object($"info","$.age").as("age"))
.show(false)
session.sql(
"""
|select info,get_json_object(info,"$.name") as name,get_json_object(info,"$.age") as age
|from t
|""".stripMargin).show(false)
}
}
|