本文主要是介绍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)
}
}
这篇关于SparkSQL_经典案例分析_读取Json字符串直接获取属性对应的值的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!