1、项目准备
①.将hive-site.xml hdfs-site.xml配置文件导入项目resources中。
②.导入hive、spark(spark-sql、spark-hive、spark-sql、spark-core)、hadoop、mysql、scala相关依赖。
之所以要mysql和hive依赖,因为结果最终想实现到mysql和hive中。
③修改hive-site.xml配置文件。允许分区,使用非严格模式。
<property>
<name>hive.exec.dynamic.partition</name>
<value>true</value>
</property>
<property>
<name>hive.exec.dynamic.partition.mode</name>
<value>nonstrict</value>
</property>
2、创建spark对象,支持hive。
val spark = SparkSession.builder().master("local[*]").appName("spark")
.enableHiveSupport().getOrCreate()//支持hive
3、从hdfs读取文件,关注parquet文件读取。
val allData: DataFrame = spark.read.parquet("hdfs://master:9000/data/release/ods/session/bdp_day=20191126").cache()
4、导入架包,隐式转换。
import org.apache.spark.sql.functions._
import spark.implicits._
5、DSL语句
5.1主要关注parquet自动对应到DataFrame。
val logData: DataFrame = allData.select("session","status","device_type","sources","channels")
5.2关注元数据其中某个字段是json的解析方法(exts)。select中有方法时使用selectExpr代替select。
val extsData: DataFrame= allData.select("exts").selectExpr("json_tuple(exts,'idcard','longitude','latitude','area_code','matter_id',,'aid')").filter(!_.anyNull)
5.3自定义udf,注册之后可以使用
spark.udf.register("getdate",(date:Long)=>{
val simple = new SimpleDateFormat("yyyy-MM-dd")
val newdate: Date = new Date(date)
simple.format(newdate)
})
5.4
自定义的udf使用,同理要使用selectExpr。
这里要关注将exts字段里的json一并解析出来,这里提供另一种json的解析方式。
自定义的udf(getdate)是将ct转换过来,主要用于分区。ct是时间戳。
val bidding: DataFrame = allData
.selectExpr("session",
"status",
"device_num",
"get_json_object(exts,'$.price')",
"get_json_object(exts,'$.result')",
"get_json_object(exts,'$.aid')",
"ct",
"getdate(ct)"
)
.filter("bidding_type is not null")
.toDF("session", "status", "sources", "channels"
, "type","status","ct","bdp_day")
6、将中间数据写入DW层。
需要提前建立库表。表要是分区表。分区字段是bdp_day(字段来源见上一步)
bidding.write.insertInto("dw_release.dw_release_show")
7、这里主要看一下when的使用。一组when要有“.”连接。否则变成多个when判断。
val biddingRes: DataFrame = bidding.select(
col("session"),
col("status"),
col("status"),
col("price")+0,
when (bidding.col("bidding_type").equalTo("PMP"),$"bidding_price"+0)
.when (bidding.col("bidding_status").equalTo("02") && bidding.col("bidding_type").equalTo("RTB"),$"bidding_price"+0)
.otherwise (0) as("cost_price"),
when (bidding.col("bidding_type").equalTo("PMP"),1)
.when (bidding.col("bidding_status").equalTo("02") && bidding.col("bidding_type").equalTo("RTB"),1)
.otherwise (0) as("bidding_result"),
col("ct"),
col("bdp_day")
)
.toDF("session", "status", "sources", "channels"
, "status","price", "bidding_result", "aid","ct","bdp_day")
biddingRes.show(20)
8、这里要看一下“.agg”的使用。sum、min等函数要放在其中执行。
val allValue: DataFrame = biddingRes.select("bdp_day", "sources", "price", "bidding_price")
.groupBy("bdp_day", "sources")
.agg(sum("cost_price") as "totalCost", sum("bidding_price") as "totalBidding").cache()
9、这是将最终指标写入到mysql数据库落地。
// 结果落地到mysql数据库
val connectionProperties = new Properties()
connectionProperties.put("user", "root")
connectionProperties.put("password", "root")
aim2.write.mode(SaveMode.Append).jdbc("jdbc:mysql://master:3306/test", "advtage",connectionProperties)
10、关闭spark。代码亲测可以使用。主要记录一下DSL风格代码的使用。仅供参考。
spark.close()