forked from alibaba/mdrill
/
aplus_text.java
491 lines (405 loc) · 10.9 KB
/
aplus_text.java
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package com.alimama.quanjingmonitor.mdrillImport.parse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.log4j.Logger;
import com.alimama.mdrillImport.InvalidEntryException;
public class aplus_text extends com.alimama.mdrillImport.DataParser{
private static final long serialVersionUID = 1L;
public volatile long groupCreateerror=0;
/**
* create stream table overwrite aplus_target_table (
0 version string,
1 ip string,
2 time string,
3 url string,
4 user_agent string,
5 linezing_session string,
6 cna string,
7 adid string,
8 amid string,
9 cmid string,
10 pmid string,
11 uid string,
12 sid string,
13 pre string,
14 cache_ string,
15 scr string,
16 nick string,
17 at_autype string,
18 bbid string,
19 at_isb string,
20 at_mall_pro_re string,
21 at_mall_re string,
22 at_shoptype string,
23 b2c_auction string,
24 b2c_brand string,
25 b2c_orid string,
26 at_type string,
27 category string,
28 marketinfo string,
29 atp_isdpp string,
30 at_bucketid string,
31 at_insid string,
32 at_jporid string,
33 upi_bi string,
34 rpi_bi string,
35 wm_pageid string,
36 wm_prototypeid string,
37 wm_sid string,
38 spm_cnt string,
39 title string,
40 url_type string,
41 ref_type string,
42 ref_shopid string,
43 parse_ip string,
44 parse_time string,
45 logkey string,
46 gmkey string,
47 gokey string,
48 logtype string,
49 atp_sid string,
50 userid string,
51 isbeta string,
52 spm_url string,
53 spm_pre string
) with (
input.type='tt',
galaxy.semantic.source.timetunnel.logname='aplus_text',
galaxy.semantic.source.timetunnel.accesskey='accesskey',
galaxy.semantic.source.timetunnel.subid='subid',
galaxy.semantic.source.timetunnel.checkpoint.name='ds_rt_pv_10',
galaxy.semantic.source.timetunnel.start.time='2013-10-23 00:00:00',
galaxy.semantic.source.max.batch.size=100,
galaxy.semantic.source.input.field.delimiter='\u0001',
galaxy.semantic.source.debug=false
);
galaxy.semantic.source.parser.classpath=com.alibaba.galaxy.semantic.source.component.DefaultParser
*/
private static Logger LOG = Logger.getLogger(aplus_text.class);
private volatile long lines=0;
private volatile long lines_sb=0;
private volatile long lines_sb2=0;
private static long TS_MAX=3600l*24*31;
private volatile long laststartts=System.currentTimeMillis()/1000-TS_MAX;
private volatile long lastendts=System.currentTimeMillis()/1000+TS_MAX;
private volatile long timediff=System.currentTimeMillis();
private volatile long timediff2=System.currentTimeMillis();
public String formatRows(String[] clicklog)
{
StringBuilder b = new StringBuilder();
for (int i = 0; i < clicklog.length; i++) {
b.append(i);
b.append("=");
b.append(String.valueOf(clicklog[i]));
b.append(",");
}
return b.toString();
}
@Override
public DataIter parseLine(String line) throws InvalidEntryException
{
try {
if(line==null)
{
return null;
}
this.lines++;
if(this.lines>100000)
{
this.laststartts=(System.currentTimeMillis()/1000)-TS_MAX;
this.lastendts=(System.currentTimeMillis()/1000)+TS_MAX;
this.lines=0;
}
String[] log =line.split("\001",-1);
if(log==null||log.length<46)
{
return null;
}
long ts = Long.parseLong(log[2]);
this.lines_sb++;
if(this.lines_sb>5000)
{
this.lines_sb=0;
long nowts=System.currentTimeMillis();
if(nowts-timediff>30000)
{
timediff=nowts;
LOG.info("parseLine_sb_"+formatDayMin.format(new Date(ts*1000))+" "+formatRows(log));
}
}
if(ts<laststartts||ts>lastendts)
{
return null;
}
// 0 version string,
// 1 ip string,
// 2 time string,
// 3 url string,
// 4 user_agent string,
// 5 linezing_session string,
// 6 cna string,
// 7 adid string,
// 8 amid string,
// 9 cmid string,
// 10 pmid string,
// 11 uid string,
// 12 sid string,
// 13 pre string,
// 14 cache_ string,
// 15 scr string,
// 16 nick string,
// 17 at_autype string,
// 18 bbid string,
// 19 at_isb string,
// 20 at_mall_pro_re string,
// 21 at_mall_re string,
// 22 at_shoptype string,
// 23 b2c_auction string,
// 24 b2c_brand string,
// 25 b2c_orid string,
// 26 at_type string,
// 27 category string,
// 28 marketinfo string,
// 29 atp_isdpp string,
// 30 at_bucketid string,
// 31 at_insid string,
// 32 at_jporid string,
// 33 upi_bi string,
// 34 rpi_bi string,
// 35 wm_pageid string,
// 36 wm_prototypeid string,
// 37 wm_sid string,
// 38 spm_cnt string,
// 39 title string,
// 40 url_type string,
// 41 ref_type string,
// 42 ref_shopid string,
// 43 parse_ip string,
// 44 parse_time string,
// 45 logkey string,
// 46 gmkey string,
// 47 gokey string,
// 48 logtype string,
// 49 atp_sid string,
// 50 userid string,
// 51 isbeta string,
// 52 spm_url string,
// 53 spm_pre string
String ad_id=log[7];
String url_ad_id=getName(log[3], "ad_id");
if((ad_id==null||ad_id.isEmpty())&&(url_ad_id==null||url_ad_id.isEmpty()))
{
return null;
}
DataIterParse rtn= new DataIterParse(ts,log,ad_id,url_ad_id);
if(!rtn.isvalidate())
{
this.lines_sb2++;
if(this.lines_sb2>5000)
{
this.lines_sb2=0;
long nowts=System.currentTimeMillis();
if(nowts-timediff2>30000)
{
timediff2=nowts;
LOG.info("parseLine_sb2_"+formatDayMin.format(new Date(ts*1000))+" "+formatRows(log));
}
}
return null;
}
return rtn;
} catch (Throwable nfe) {
if(groupCreateerror<100)
{
LOG.error("InvalidEntryException:"+line,nfe);
groupCreateerror++;
}
throw new InvalidEntryException("Invalid log `" + line + "'\n" , nfe);
}
}
public static class DataIterParse implements DataIter{
private long ts;
private String ad_id;
private String logkey="";
private boolean ispv2=false;
private boolean isclick_1=false;
private boolean isclick_2=false;
private String url_ad_id;
private String pid=null;
private String pid_Url=null;
private Map<String, String> map=FetchAdid2Pid.fetch();
private String fetch(String adid,String strday)
{
if(adid==null||adid.isEmpty())
{
return null;
}
String ad_id_cut=adid.substring(0, Math.min(10,adid.length()));
return map.get(strday+"@"+String.valueOf(ad_id_cut));
}
int index=0;
public DataIterParse(long ts,String[] pvlog,String ad_id,String url_ad_id) {
this.ts=ts;
this.ad_id=ad_id;
this.url_ad_id=url_ad_id;
String Url=decodeString(pvlog[3]);
String pre=decodeString(pvlog[13]);
this.logkey=pvlog[45];
String strday=formatDay.format(new Date(ts*1000));
this.pid=this.fetch(this.ad_id, strday);
this.pid_Url=this.fetch(this.url_ad_id, strday);
String jlogid=getName(Url, "jlogid");
String jlogid_pre=getName(pre, "jlogid");
boolean isMathchUrl=Url.indexOf("38.tmall.com")>=0&&jlogid!=null&&!jlogid.isEmpty();
boolean isMathchPre=pre.indexOf("38.tmall.com")>=0&&jlogid_pre!=null&&!jlogid_pre.isEmpty();
this.ispv2=this.pid!=null&&(!this.logkey.equals("/"))&&this.ad_id.startsWith("10")&&isMathchUrl;
this.isclick_2=this.pid!=null&&(!this.logkey.equals("/"))&&this.ad_id.startsWith("10")&&isMathchPre;
this.isclick_1=this.pid_Url!=null&&this.logkey.equals("/")&&this.url_ad_id.length()>10&&Url.indexOf("ju.mmstat.com")>=0;//from url
}
public boolean isvalidate()
{
return this.ispv2||this.isclick_2||this.isclick_1;
}
@Override
public boolean next() {
index++;
if(this.ispv2||this.isclick_2)
{
this.ispv2=false;
this.isclick_2=false;
return this.isclick_1;
}
return false;
}
@Override
public Number[] getSum() {
if(this.ispv2||this.isclick_2)
{
return new Number[]{
this.ispv2?1:0
,0
,this.isclick_2?1:0
,0
,0
,0
,0
,0
};
}
return new Number[]{
0
,this.isclick_1?1:0
,0
,0
,0
,0
,0
,0
};
}
@Override
public long getTs() {
return (ts/10)*10000;
}
@Override
public Object[] getGroup() {
long ts300=(this.ts/300)*300000;
Date d= new Date(ts300);
if(this.ispv2||this.isclick_2)
{
return new String[] {
String.valueOf(formatDay.format(d)),
String.valueOf(formatMin.format(d)),
"pc",
"aplus_text",
"pc",
this.pid
, ""
,DebugVersion.version+","+index// String.valueOf(actname)
} ;
}
return new String[] {
String.valueOf(formatDay.format(d)),
String.valueOf(formatMin.format(d)),
"pc",
"aplus_text",
"pc",
this.pid_Url
, ""
,DebugVersion.version+","+index// String.valueOf(actname)
} ;
}
}
private static String[] colSumName={
"pv_2"
,"click_1"
,"click_2"
,"promise_click"
,"pc_2_wap"
,"weakup"
,"backup_1"
,"backup_2"
};
private static String[] colname={
"thedate"
,"miniute_5"
,"source"
,"sub_source"
,"media_name"
,"media_pid"
,"channel"
,"o2o"
};
private static String decodeString(String args) {
try {
return new String(java.net.URLDecoder.decode(args,"UTF-8") .getBytes("UTF-8"), "UTF-8");
} catch (Throwable e) {
try {
return new String(java.net.URLDecoder.decode(args,"GBK") .getBytes("UTF-8"), "UTF-8");
} catch (Throwable e2) {
return args;
}
}
}
public static String getName(String url,String keyname)
{
if(url==null)
{
return null;
}
try{
String[] tem = decodeString(url).split("\\?", 2);
String params=tem[0];
if (tem.length >= 2){
params=tem[1];
}
for (String s: params.split("&", -1)) {
String[] tem1 = s.split("=", -1);
String key = decodeString(tem1[0]);
if(key.equals(keyname))
{
String value = (tem1.length < 2
? "" : decodeString(tem1[1]));
return value;
}
}
}catch(Throwable e){}
return null;
}
@Override
public String[] getSumName() {
return colSumName;
}
@Override
public String getTableName() {
return "rpt_adpmp_3_8_online";
}
private static SimpleDateFormat formatDay = new SimpleDateFormat("yyyyMMdd");
private static SimpleDateFormat formatMin = new SimpleDateFormat("HHmm");
private static SimpleDateFormat formatDayMin = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public String[] getGroupName() {
return colname;
}
}