1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package mobvista.dmp.datasource.dm
import mobvista.dmp.common.CommonSparkJob
import mobvista.dmp.format.RDDMultipleOutputFormat
import org.apache.commons.cli.Options
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.hadoop.io.SequenceFile.CompressionType
import org.apache.hadoop.io.Text
import org.apache.hadoop.io.compress.{CompressionCodec, GzipCodec}
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.functions.{concat_ws, lit}
import org.apache.spark.sql.types.{StringType, StructField, StructType}
import org.apache.spark.sql.{DataFrame, Row, SparkSession}
import org.apache.spark.storage.StorageLevel
import java.net.URI
import scala.collection.mutable.ArrayBuffer
/**
* @author jiangfan
* @date 2021/9/9 17:42
*/
class CnGoodChannel extends CommonSparkJob with Serializable {
override protected def buildOptions(): Options = {
val options = new Options
options.addOption("coalesce", true, "[must] coalesce")
options.addOption("old_data_path", true, "[must] old_data_path")
options.addOption("output1", true, "[must] output1")
options.addOption("output2", true, "[must] output2")
options.addOption("output3", true, "[must] output3")
options.addOption("output4", true, "[must] output4")
options.addOption("output5", true, "[must] output5")
options.addOption("package_name1", true, "[must] package_name1")
options.addOption("package_name2", true, "[must] package_name2")
options.addOption("package_name3", true, "[must] package_name3")
options
}
override protected def run(args: Array[String]): Int = {
val commandLine = commParser.parse(options, args)
if (!checkMustOption(commandLine)) {
printUsage(options)
return -1
} else printOptions(commandLine)
val coalesce = commandLine.getOptionValue("coalesce")
val old_data_path = commandLine.getOptionValue("old_data_path")
val output1 = commandLine.getOptionValue("output1")
val output2 = commandLine.getOptionValue("output2")
val output3 = commandLine.getOptionValue("output3")
val output4 = commandLine.getOptionValue("output4")
val output5 = commandLine.getOptionValue("output5")
val package_name1 = commandLine.getOptionValue("package_name1")
val package_name2 = commandLine.getOptionValue("package_name2")
val package_name3 = commandLine.getOptionValue("package_name3")
val spark = SparkSession.builder()
.appName("CnGoodChannel")
.config("spark.rdd.compress", "true")
.config("spark.io.compression.codec", "snappy")
.config("spark.sql.orc.filterPushdown", "true")
.config("spark.sql.warehouse.dir", "s3://mob-emr-test/spark-warehouse")
.config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
.enableHiveSupport()
.getOrCreate()
val sc = spark.sparkContext
import spark.implicits._
val conf = spark.sparkContext.hadoopConfiguration
conf.set("mapreduce.output.compress", "true")
conf.set("mapreduce.output.compression.codec", "org.apache.hadoop.io.compress.GzipCodec")
conf.setBoolean("mapreduce.output.fileoutputformat.compress", true)
conf.set("mapreduce.output.fileoutputformat.compress.type", CompressionType.BLOCK.toString)
conf.setClass("mapreduce.output.fileoutputformat.compress.codec", classOf[GzipCodec], classOf[CompressionCodec])
FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output1), true)
FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output2), true)
FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output3), true)
FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output4), true)
FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output5), true)
try {
val old_data: RDD[Row] = sc.textFile(old_data_path).map(row => {
val package_name=row.split("\t", -1)(3)
Row(row.split("\t", -1)(0), row.split("\t", -1)(1), package_name.substring(2,package_name.lastIndexOf("\"")))
})
val schema: StructType = StructType(Array(
StructField("device_id", StringType),
StructField("device_type", StringType),
StructField("package_name", StringType)
))
val old_data_df = spark.createDataFrame(old_data, schema)
old_data_df.createOrReplaceTempView("yesterday_data")
// val sql1=
// s"""
// |select device_id,device_type
// |from
// | (select split(onedata,"\\\\|")[0] as device_id,split(onedata,"\\\\|")[1] as device_type
// | from
// | (select onedata
// | from
// | (
// | select concat_ws(",",v1,v2,v3,v4) data
// | from
// | (
// | select concat(imei,"|imei") v1 ,concat(oaid,"|oaid") v2,concat(imeimd5,"|imeimd5") v3 ,concat(oaidmd5,"|oaidmd5") v4
// | from
// | (select
// | ( case when imei not in ('0','00000000-0000-0000-0000-000000000000','',' ','00000000','00000000000000','000000000000000','0000000000000000') then imei else null end) as imei,
// | ( case when ext_oaid not in ('00000000-0000-0000-0000-000000000000','0000000000000000','0','',' ') then ext_oaid else null end ) as oaid,
// | md5(case when imei not in ('0','00000000-0000-0000-0000-000000000000','','00000000','00000000000000','000000000000000','0000000000000000') then imei else null end) as imeimd5,
// | md5(case when ext_oaid not in ('00000000-0000-0000-0000-000000000000','0000000000000000','0','',' ') then ext_oaid else null end ) as oaidmd5
// | FROM
// | dwh.ods_adn_trackingnew_request
// | where country_code = 'CN' and platform = 'android' and publisher_id in('11386','20284','16227','23652','18721','20781','22193','21522','21915') and app_id not in ('122317')
// | and concat(yyyy,'-',mm,'-',dd)=cast(date_sub(CURRENT_DATE, 1) as string)
// | ) tmpdata where imei is not null and oaid is not null and imeimd5 is not null and oaidmd5 is not null
// | ) t
// | ) tt
// | lateral view explode(split(data, ",")) num as onedata) ttt
// | union
// | select device_id,device_type
// | from
// | yesterday_data
// | where package_name='${package_name1}' ) tttt
// | group by device_id,device_type
// |""".stripMargin
val sql1=
s"""
|select device_id,device_type
|from
| (select split(onedata,"\\\\|")[0] as device_id,split(onedata,"\\\\|")[1] as device_type
| from
| (select onedata
| from
| (
| select data
| from
| (
| select concat_ws(",",v1,v2,v3,v4) data
| from
| (
| select concat(imei,"|imei") v1 ,concat(oaid,"|oaid") v2,concat(imeimd5,"|imeimd5") v3 ,concat(oaidmd5,"|oaidmd5") v4
| from
| (select
| ( case when imei not in ('0','00000000-0000-0000-0000-000000000000','',' ','00000000','00000000000000','000000000000000','0000000000000000') then imei else null end) as imei,
| ( case when ext_oaid not in ('00000000-0000-0000-0000-000000000000','0000000000000000','0','',' ') then ext_oaid else null end ) as oaid,
| md5(case when imei not in ('0','00000000-0000-0000-0000-000000000000','','00000000','00000000000000','000000000000000','0000000000000000') then imei else null end) as imeimd5,
| md5(case when ext_oaid not in ('00000000-0000-0000-0000-000000000000','0000000000000000','0','',' ') then ext_oaid else null end ) as oaidmd5
| FROM
| dwh.ods_adn_trackingnew_request
| where country_code = 'CN' and platform = 'android' and publisher_id in('11386','20284','16227','23652','18721','20781','22193','21522','21915') and app_id not in ('122317')
| and concat(yyyy,'-',mm,'-',dd)=cast(date_sub(CURRENT_DATE, 1) as string)
| ) tmpdata
| ) t01
| ) t02 where data!='' ) t03
| lateral view explode(split(data, ",")) num as onedata) t04
| union
| select device_id,device_type
| from
| yesterday_data
| where package_name='${package_name1}' ) t05
| group by device_id,device_type
|""".stripMargin
val df01: DataFrame = spark.sql(sql1).persist(StorageLevel.MEMORY_AND_DISK_SER)
spark.sql(sql1).rdd.coalesce(coalesce.toInt).map(r => {
val device_id = r.getAs[String]("device_id")
val device_type = r.getAs[String]("device_type")
(new Text(s"${output1}/${device_type}"), new Text(device_id))
}).saveAsNewAPIHadoopFile(output1, classOf[Text], classOf[Text], classOf[RDDMultipleOutputFormat[_, _]], conf)
// val sql2=
// s"""
// |select device_id,device_type
// |from
// | (select split(onedata,"\\\\|")[0] as device_id,split(onedata,"\\\\|")[1] as device_type
// | from
// | (select onedata
// | from
// | (
// | select concat_ws(",",v1,v2,v3,v4) data
// | from
// | (
// | select concat(imei,"|imei") v1 ,concat(oaid,"|oaid") v2,concat(imeimd5,"|imeimd5") v3 ,concat(oaidmd5,"|oaidmd5") v4
// | from
// | (select
// | ( case when imei not in ('0','00000000-0000-0000-0000-000000000000','',' ','00000000','00000000000000','000000000000000','0000000000000000') then imei else null end) as imei,
// | ( case when ext_oaid not in ('00000000-0000-0000-0000-000000000000','0000000000000000','0','',' ') then ext_oaid else null end ) as oaid,
// | md5(case when imei not in ('0','00000000-0000-0000-0000-000000000000','','00000000','00000000000000','000000000000000','0000000000000000') then imei else null end) as imeimd5,
// | md5(case when ext_oaid not in ('00000000-0000-0000-0000-000000000000','0000000000000000','0','',' ') then ext_oaid else null end ) as oaidmd5
// | FROM
// | dwh.ods_adn_trackingnew_request
// | where country_code = 'CN' and platform = 'android' and publisher_id in('18590','21933','13566','19672','26101','21386')
// | and concat(yyyy,'-',mm,'-',dd)=cast(date_sub(CURRENT_DATE, 1) as string)
// | ) tmpdata where imei is not null and oaid is not null and imeimd5 is not null and oaidmd5 is not null
// | ) t
// | ) tt
// | lateral view explode(split(data, ",")) num as onedata) ttt
// | union
// | select device_id,device_type
// | from
// | yesterday_data
// | where package_name='${package_name2}' ) tttt
// | group by device_id,device_type
// |""".stripMargin
val sql2=
s"""
|select device_id,device_type
|from
| (select split(onedata,"\\\\|")[0] as device_id,split(onedata,"\\\\|")[1] as device_type
| from
| (select onedata
| from
| (
| select data
| from
| (
| select concat_ws(",",v1,v2,v3,v4) data
| from
| (
| select concat(imei,"|imei") v1 ,concat(oaid,"|oaid") v2,concat(imeimd5,"|imeimd5") v3 ,concat(oaidmd5,"|oaidmd5") v4
| from
| (select
| ( case when imei not in ('0','00000000-0000-0000-0000-000000000000','',' ','00000000','00000000000000','000000000000000','0000000000000000') then imei else null end) as imei,
| ( case when ext_oaid not in ('00000000-0000-0000-0000-000000000000','0000000000000000','0','',' ') then ext_oaid else null end ) as oaid,
| md5(case when imei not in ('0','00000000-0000-0000-0000-000000000000','','00000000','00000000000000','000000000000000','0000000000000000') then imei else null end) as imeimd5,
| md5(case when ext_oaid not in ('00000000-0000-0000-0000-000000000000','0000000000000000','0','',' ') then ext_oaid else null end ) as oaidmd5
| FROM
| dwh.ods_adn_trackingnew_request
| where country_code = 'CN' and platform = 'android' and publisher_id in('18590','21933','13566','19672','26101','21386')
| and concat(yyyy,'-',mm,'-',dd)=cast(date_sub(CURRENT_DATE, 1) as string)
| ) tmpdata
| ) t01
| ) t02 where data!='' ) t03
| lateral view explode(split(data, ",")) num as onedata) t04
| union
| select device_id,device_type
| from
| yesterday_data
| where package_name='${package_name2}' ) t05
| group by device_id,device_type
|""".stripMargin
val df02: DataFrame = spark.sql(sql2).persist(StorageLevel.MEMORY_AND_DISK_SER)
spark.sql(sql2).rdd.coalesce(coalesce.toInt).map(r => {
val device_id = r.getAs[String]("device_id")
val device_type = r.getAs[String]("device_type")
(new Text(s"${output2}/${device_type}"), new Text(device_id))
}).saveAsNewAPIHadoopFile(output2, classOf[Text], classOf[Text], classOf[RDDMultipleOutputFormat[_, _]], conf)
// 第三个sql需求原本是从adn_dsp.log_adn_dsp_request_orc_hour取数据,但这个表计算量大,发现dwh.etl_dsp_request_daily_hours 保存了adn_dsp.log_adn_dsp_request_orc_hour相关数据,读取该表,减小计算量
//
val sql3=
s"""
|select device_id,device_type
|from
| (select split(onedata,"\\\\|")[0] as device_id,split(onedata,"\\\\|")[1] as device_type
| from
| (select onedata
| from
| (
| select concat_ws(",",v1,v2,v3,v4) data
| from
| (
| select concat(imei,"|imei") v1 ,concat(oaid,"|oaid") v2,concat(imeimd5,"|imeimd5") v3 ,concat(oaidmd5,"|oaidmd5") v4
| from
| (select
| split(exitid,',')[4] as imei,
| split(exitid,',')[12] as oaid,
| split(exitid,',')[5] as imeimd5,
| split(exitid,',')[13] as oaidmd5
| from
| (select
| packagename,exitid
| FROM
| dwh.etl_dsp_request_daily_hours
| where dt=replace(cast(date_sub(CURRENT_DATE, 1) as string),'-','')
| and country = 'CN' and platform = 'android' and exchanges in ('iqiyi','wax')
| ) tmpdata lateral view explode(split(packagename, "#")) num as appid
| where appid in ('com.qiyi.video','com.sina.weibo','tv.pps.mobile')
| ) t01
| ) t02
| ) t03
| lateral view explode(split(data, ",")) num as onedata where data!='' and split(onedata,"\\\\|")[0]!='') t04
| union
| select device_id,device_type
| from
| yesterday_data
| where package_name='${package_name3}' ) t05
| group by device_id,device_type
|""".stripMargin
// val sql3=
// s"""
// |select device_id,device_type
// |from
// | (select split(onedata,"\\\\|")[0] as device_id,split(onedata,"\\\\|")[1] as device_type
// | from
// | (select onedata
// | from
// | (
// | select concat_ws(",",v1,v2,v3,v4) data
// | from
// | (
// | select concat(imei,"|imei") v1 ,concat(oaid,"|oaid") v2,concat(imeimd5,"|imeimd5") v3 ,concat(oaidmd5,"|oaidmd5") v4
// | from
// | (select
// | split(ext5,',')[4] as imei,
// | split(ext5,',')[12] as oaid,
// | split(ext5,',')[5] as imeimd5,
// | split(ext5,',')[13] as oaidmd5
// | FROM
// | adn_dsp.log_adn_dsp_request_orc_hour
// | where countrycode = 'CN' and os = 'android' and exchanges in ('iqiyi','wax') and appid in ('com.qiyi.video','com.sina.weibo','tv.pps.mobile')
// | and concat(yr,'-',mt,'-',dt)=cast(date_sub(CURRENT_DATE, 1) as string)
// | ) tmpdata where imei is not null and oaid is not null and imeimd5 is not null and oaidmd5 is not null
// | ) t
// | ) tt
// | lateral view explode(split(data, ",")) num as onedata) ttt
// | union
// | select device_id,device_type
// | from
// | yesterday_data
// | where package_name='${package_name3}' ) tttt
// | group by device_id,device_type
// |""".stripMargin
val df03: DataFrame = spark.sql(sql3).persist(StorageLevel.MEMORY_AND_DISK_SER)
spark.sql(sql3).rdd.coalesce(coalesce.toInt).map(r => {
val device_id = r.getAs[String]("device_id")
val device_type = r.getAs[String]("device_type")
(new Text(s"${output3}/${device_type}"), new Text(device_id))
}).saveAsNewAPIHadoopFile(output3, classOf[Text], classOf[Text], classOf[RDDMultipleOutputFormat[_, _]], conf)
val data01 = df01.select(concat_ws("\t", df01.col("device_id"), df01.col("device_type"), lit("android"),lit("[\""+package_name1+"\"]")))
val data01_with_country = df01.select(concat_ws("\t", df01.col("device_id"), df01.col("device_type"), lit("android"),lit("CN")))
val data02 = df02.select(concat_ws("\t", df02.col("device_id"), df02.col("device_type"), lit("android"),lit("[\""+package_name2+"\"]")))
val data02_with_country = df02.select(concat_ws("\t", df02.col("device_id"), df02.col("device_type"), lit("android"),lit("CN")))
val data03 = df03.select(concat_ws("\t", df03.col("device_id"), df03.col("device_type"), lit("android"),lit("[\""+package_name3+"\"]")))
val data03_with_country = df03.select(concat_ws("\t", df03.col("device_id"), df03.col("device_type"), lit("android"),lit("CN")))
data01.union(data02).union(data03).coalesce(coalesce.toInt).write.format("text").mode("overwrite").save(output4)
data01_with_country.union(data02_with_country).union(data03_with_country).coalesce(coalesce.toInt).write.format("text").mode("overwrite").save(output5)
} finally {
spark.stop()
}
0
}
}
object CnGoodChannel {
def main(args: Array[String]): Unit = {
new CnGoodChannel().run(args)
}
}