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
package mobvista.dmp.datasource.toutiao
import java.io.ByteArrayOutputStream
import java.util
import java.util.Base64
import org.apache.spark.sql.Row
import org.apache.spark.sql.types.{StringType, StructField, StructType}
import scala.collection.mutable.ArrayBuffer
import collection.JavaConverters._
/**
* @package: mobvista.dmp.datasource.toutiao
* @author: wangjf
* @date: 2018/12/11
* @time: 下午7:42
* @email: jinfeng.wang@mobvista.com
* @phone: 152-1062-7698
*/
object Constant {
def schema: StructType = {
StructType(StructField("tag_code", StringType) ::
StructField("tag_type", StringType) ::
StructField("tag_id", StringType) ::
Nil)
}
def mapPart(iters: Iterator[Row]): Iterator[(String, (String, String))] = {
val res = new util.ArrayList[(String, (String, String))]()
while (iters.hasNext) {
val ir = iters.next
val device_id = ir.getAs(fieldName = "device_id").toString
val tag_type = ir.getAs(fieldName = "tag_type").toString
val tag_id = ir.getAs(fieldName = "tag_id").toString
val active_id = ir.getAs(fieldName = "active_id").toString
res.add((device_id + "\001" + active_id, (tag_type, tag_id)))
}
res.asScala.iterator
}
def flatMapCore(ir: (String, Iterable[(String, String)])): ArrayBuffer[(String, String)] = {
val arrayBuffer = new ArrayBuffer[(String, String)]()
val array = ir._1.split("\001")
val device_id = array(0)
val active_id = array(1)
val categorySet = new util.HashSet[String]()
val styleSet = new util.HashSet[String]()
val ruleSet = new util.HashSet[String]()
val vals = ir._2.iterator
while (vals.hasNext) {
val value = vals.next
if ("01".equals(value._1)) {
categorySet.add(value._2)
} else if ("02".equals(value._1)) {
styleSet.add(value._2)
} else {
ruleSet.add(value._2)
}
}
var categoryCode = "00"
var styleCode = "00"
var ruleCode = "00"
if (categorySet.isEmpty) {
if (styleSet.isEmpty) {
val ruleIter = ruleSet.iterator
while (ruleIter.hasNext) {
ruleCode = ruleIter.next
arrayBuffer += ((device_id, categoryCode + styleCode + ruleCode + active_id))
}
} else {
val styleIter = styleSet.iterator
while (styleIter.hasNext) {
styleCode = styleIter.next
if (ruleSet.isEmpty) {
arrayBuffer += ((device_id, categoryCode + styleCode + ruleCode + active_id))
} else {
val ruleIter = ruleSet.iterator
while (ruleIter.hasNext) {
ruleCode = ruleIter.next
arrayBuffer += ((device_id, categoryCode + styleCode + ruleCode + active_id))
}
}
}
}
} else {
val categoryIter = categorySet.iterator
while (categoryIter.hasNext) {
categoryCode = categoryIter.next
if (styleSet.isEmpty) {
if (ruleSet.isEmpty) {
arrayBuffer += ((device_id, categoryCode + styleCode + ruleCode + active_id))
} else {
val ruleIter = ruleSet.iterator
while (ruleIter.hasNext) {
ruleCode = ruleIter.next
arrayBuffer += ((device_id, categoryCode + styleCode + ruleCode + active_id))
}
}
} else {
val styleIter = styleSet.iterator
while (styleIter.hasNext) {
styleCode = styleIter.next
if (ruleSet.isEmpty) {
arrayBuffer += ((device_id, categoryCode + styleCode + ruleCode + active_id))
} else {
val ruleIter = ruleSet.iterator
while (ruleIter.hasNext) {
ruleCode = ruleIter.next
arrayBuffer += ((device_id, categoryCode + styleCode + ruleCode + active_id))
}
}
}
}
}
}
arrayBuffer
}
def mapPartCore(iters: Iterator[(String, Iterable[(String, String)])]): Iterator[(String, String)] = {
val res = new util.ArrayList[(String, String)]()
while (iters.hasNext) {
val ir = iters.next()
val array = ir._1.split("\001")
val device_id = array(0)
val active_id = array(1)
val categorySet = new util.HashSet[String]()
val styleSet = new util.HashSet[String]()
val ruleSet = new util.HashSet[String]()
val vals = ir._2.iterator
while (vals.hasNext) {
val value = vals.next
if ("01".equals(value._1)) {
categorySet.add(value._2)
} else if ("02".equals(value._1)) {
styleSet.add(value._2)
} else {
ruleSet.add(value._2)
}
}
var categoryCode = "00"
var styleCode = "00"
var ruleCode = "00"
if (categorySet.isEmpty) {
if (styleSet.isEmpty) {
val ruleIter = ruleSet.iterator
while (ruleIter.hasNext) {
ruleCode = ruleIter.next
res.add((device_id, categoryCode + styleCode + ruleCode + active_id))
}
} else {
val styleIter = styleSet.iterator
while (styleIter.hasNext) {
styleCode = styleIter.next
if (ruleSet.isEmpty) {
res.add((device_id, categoryCode + styleCode + ruleCode + active_id))
} else {
val ruleIter = ruleSet.iterator
while (ruleIter.hasNext) {
ruleCode = ruleIter.next
res.add((device_id, categoryCode + styleCode + ruleCode + active_id))
}
}
}
}
} else {
val categoryIter = categorySet.iterator
while (categoryIter.hasNext) {
categoryCode = categoryIter.next
if (styleSet.isEmpty) {
if (ruleSet.isEmpty) {
res.add((device_id, categoryCode + styleCode + ruleCode + active_id))
} else {
val ruleIter = ruleSet.iterator
while (ruleIter.hasNext) {
ruleCode = ruleIter.next
res.add((device_id, categoryCode + styleCode + ruleCode + active_id))
}
}
} else {
val styleIter = styleSet.iterator
while (styleIter.hasNext) {
styleCode = styleIter.next
if (ruleSet.isEmpty) {
res.add((device_id, categoryCode + styleCode + ruleCode + active_id))
} else {
val ruleIter = ruleSet.iterator
while (ruleIter.hasNext) {
ruleCode = ruleIter.next
res.add((device_id, categoryCode + styleCode + ruleCode + active_id))
}
}
}
}
}
}
}
res.asScala.iterator
}
def mapProtoPart(iters: Iterator[(String, Iterable[String])]): Iterator[String] = {
val res = new util.ArrayList[String]()
var dmpData = DmpDataProto.DmpData.newBuilder()
while (iters.hasNext) {
val ir = iters.next()
val id_item = dmpData.addIdListBuilder()
id_item.setDataType(DmpDataProto.IdItem.DataType.IDFA_MD5)
id_item.setId(ir._1)
val tags = ir._2.iterator
while (tags.hasNext) {
val tag = tags.next
id_item.addTags(tag)
}
id_item.build()
if (dmpData.getIdListCount >= 10000) { // 每行数据包的idList大小不能超过 10000
val dmpDataProto = dmpData.build()
val output = new ByteArrayOutputStream()
dmpDataProto.writeTo(output)
res.add(Base64.getEncoder.encodeToString(output.toByteArray))
dmpData = DmpDataProto.DmpData.newBuilder()
} else if (dmpData.getIdListCount > 0 && dmpData.getIdListCount < 10000 && !iters.hasNext) {
val dmpDataProto = dmpData.build()
val output = new ByteArrayOutputStream()
dmpDataProto.writeTo(output)
res.add(Base64.getEncoder.encodeToString(output.toByteArray))
}
}
res.asScala.iterator
}
}