数据库
首页 > 数据库> > SparkSQL_经典案例分析_读取Json字符串直接获取属性对应的值

SparkSQL_经典案例分析_读取Json字符串直接获取属性对应的值

作者:互联网

package examples

import org.apache.spark.sql.SparkSession

/**
 * @Author yqq
 * @Date 2021/12/14 22:18
 * @Version 1.0
 */
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)
  }
}

在这里插入图片描述

标签:info,name,get,age,object,json,Json,SparkSQL,读取
来源: https://blog.csdn.net/manba_yqq/article/details/121941904