1.spark根据schema读取json数据进行sparksql操作
注意:读取json数据,特别是数据量比较大的json数据,需要定义schema,减少读取的数据量,不然加载太多数据浪费集群资源,而且太浪费时间,吃力不讨好。
1)定义schema
import
org.apache.spark.sql.types.{StructField, _}
val
logSchema = StructType(
Array
(
StructField
(
"data"
,StructType(
Array
(
StructField
(
"org_token"
,StringType,
true
),
StructField
(
"text"
,StringType,
true
)
)),
true
),
StructField
(
"result"
,StructType(
Array
(
StructField
(
"rule-engine"
,StructType(
Array
(
StructField
(
"riskLevel"
,StringType,

本文介绍了如何使用SparkSQL结合schema高效读取大规模JSON数据,强调了定义schema的重要性,以避免资源浪费。还讨论了在Spark中读取不同数据源(如HDFS上的JSON和Parquet文件,以及HBase表)的方法,并阐述了在SparkSQL中使用Hive UDF的注意事项,包括在spark-shell和Scala工程中的不同操作。
最低0.47元/天 解锁文章
819

被折叠的 条评论
为什么被折叠?



