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
package mobvista.dmp.common
import java.net.URI
import java.util
import com.alibaba.fastjson.JSONObject
import com.google.gson.{JsonArray, JsonElement, JsonObject}
import mobvista.dmp.datasource.mpsdk.InstallInfo
import mobvista.prd.datasource.util.GsonUtil
import org.apache.commons.lang.StringUtils
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.hadoop.io.compress.GzipCodec
import org.apache.spark.SparkContext
import org.apache.spark.sql.{Row, SparkSession}
import org.joda.time.format.DateTimeFormat
import scala.collection.JavaConversions._
import scala.collection.mutable.ArrayBuffer
/**
* 填数据合并到安装列表通用程序,
* 记成该类后,需实现数据处理方法即可
* fengliang
*/
abstract class CommonInstallListV2 extends CommonSparkJob with Serializable {
protected val FIELD_SPLIT = "\t"
protected var DAILY_STORE_FORMAT = "ORC"
def run(args: Array[String]): Int = {
var sc: SparkContext = null
try {
options.addOption("date", true, "[must] today")
options.addOption("oldInput", true, "[must] yestoday install data path")
val commandLine = commParser.parse(options, args)
if (!checkMustOption(commandLine)) {
printUsage(options)
return 1
} else {
printOptions(commandLine)
}
val date = commandLine.getOptionValue("date")
val input = commandLine.getOptionValue("input")
val output = commandLine.getOptionValue("output")
val oldInput = commandLine.getOptionValue("oldInput")
val parallelism = commandLine.getOptionValue("parallelism").toInt
val coalesce = commandLine.getOptionValue("coalesce").toInt
val expireDate = DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(date).minusMonths(12).toString("yyyy-MM-dd")
val spark = SparkSession
.builder()
.appName("MPSDKTotal")
.config("spark.rdd.compress", "true")
.config("spark.default.parallelism", parallelism)
.config("spark.sql.warehouse.dir", "s3://mob-emr-test/spark-warehouse")
.config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
.getOrCreate()
// 任务重试过程中,路径已经存在造成无法写入的bug
FileSystem.get(new URI(s"s3://mob-emr-test"), spark.sparkContext.hadoopConfiguration).delete(new Path(output), true)
// FileSystem.get(spark.sparkContext.hadoopConfiguration).delete(new Path(output), true)
val dailyDF = spark.read.format(DAILY_STORE_FORMAT).load(input)
// val dailyData = dailyDF.rdd.map(convertDayRow(_))
val dailyRDD = dailyDF.rdd.mapPartitions(processDailyData(_, date)).filter(tuple => {
val array = splitFun(tuple._1)
array(0).matches(didPtn) && !allZero.equals(array(0)) || array(0).matches(imeiPtn)
})
.groupByKey()
.map(tuple => {
val set = new util.HashSet[InstallInfo]()
tuple._2.foreach(info => {
set.add(info)
})
(tuple._1, set)
})
sc = spark.sparkContext
val installRDD = sc.textFile(oldInput)
.map(splitFun(_))
.filter(array => (array(0).matches(didPtn) && !allZero.equals(array(0)) || array(0).matches(imeiPtn)))
.map(splits => {
(s"${splits(0)}$DATA_SPLIT${splits(1)}$DATA_SPLIT${splits(2)}", splits(3))
})
dailyRDD.cogroup(installRDD).map(
tuple => {
val key = tuple._1
val valTuple = tuple._2
val dailyIter = valTuple._1
val totalIter = valTuple._2
var tags = ""
if (dailyIter.isEmpty && totalIter.nonEmpty) {
// 需删除过期的安装信息
val installArray = new JsonArray
totalIter.foreach(str => {
GsonUtil.String2JsonArray(str).foreach(element => {
val installDate = getJsonValue(element, "date")
// 验证安装信息是否已超出期限
if (StringUtils.isNotBlank(installDate)) {
if (installDate.compareTo(expireDate) >= 0) {
installArray.add(element)
}
} else {
installArray.add(element)
}
})
})
tags = installArray.toString
} else if (dailyIter.nonEmpty && totalIter.isEmpty) {
val jsonArray = new JsonArray
dailyIter.foreach(installSet => {
installSet.foreach(installInfo => {
var installDate = installInfo.getDate()
if (installDate.compareTo(date) > 0) {
installDate = date
}
if (jsonArray.size() < 1000) {
val json = new JsonObject
json.addProperty("date", installDate)
json.addProperty("package_name", installInfo.getPackage_name().replaceAll("[^0-9a-zA-Z\\.\\_]+", ""))
jsonArray.add(json)
}
})
})
tags = jsonArray.toString
} else if (dailyIter.nonEmpty && totalIter.nonEmpty) {
val map = new JSONObject()
totalIter.foreach(str => {
GsonUtil.String2JsonArray(str).foreach(element => {
val installDate = getJsonValue(element, "date")
val installPackage = getJsonValue(element, "package_name")
// 验证安装信息是否已超出期限
if (StringUtils.isNotBlank(installDate)) {
if (installDate.compareTo(expireDate) >= 0) {
map.put(installPackage, installDate)
}
} else {
map.put(installPackage, installDate)
}
})
})
dailyIter.foreach(installSet => {
installSet.foreach(installInfo => {
var installDate = installInfo.getDate()
if (installDate.compareTo(date) > 0) {
installDate = date
}
val packageName = installInfo.getPackage_name().replaceAll("[^0-9a-zA-Z\\.\\_]+", "")
val tmpDate = map.get(packageName).toString
if (StringUtils.isNotBlank(tmpDate)) {
if (tmpDate.compareTo(installDate) < 0) {
map.put(packageName, installDate)
}
} else {
map.put(packageName, installDate)
}
})
})
val jsonArray = new JsonArray
map.entrySet().foreach(entry => {
if (jsonArray.size() < 1000) {
val json = new JsonObject
if (StringUtils.isNotEmpty(entry.getValue.toString)) {
json.addProperty("date", entry.getValue.toString)
}
json.addProperty("package_name", entry.getKey)
jsonArray.add(json)
}
})
tags = jsonArray.toString
}
s"${key}${DATA_SPLIT}${tags}"
}).repartition(coalesce).saveAsTextFile(output, classOf[GzipCodec])
} finally {
if (sc != null) {
sc.stop()
}
}
0
}
def getJsonValue(element: JsonElement, key: String): String = {
if (element != null && !element.isJsonNull) {
element.getAsJsonObject.get(key).getAsString
} else {
""
}
}
/**
* 将row转化为Array[String]
*
* @param row
* @return
*/
def convertDayRow(row: Row): Array[String] = {
val buffer = new ArrayBuffer[String]()
for (i <- 0 until row.size) {
buffer += row.getString(i)
}
buffer.toArray
}
/**
* 解析天处理结果数据
*
* @param array
* @param date
* @return
*/
def processDailyData(array: Iterator[Row], date: String): Iterator[Tuple2[String, InstallInfo]]
}
// case class InstallListVO(key: String, package_name: String, date: String)