AnalyzeHandleFactory.java
5.91 KB
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
package com.yoho.trace.offline;
import com.yoho.trace.offline.handler.ApiAnalyzeHandler;
import com.yoho.trace.anaylzer.model.SpanInfo;
import com.yoho.trace.offline.handler.IAnalyzeHandler;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableInputFormat;
import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
import org.apache.hadoop.hbase.util.Base64;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.PairFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import properties.PropertiesFactory;
import scala.Tuple2;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Created by xjipeng on 2017/10/12.
*/
public class AnalyzeHandleFactory {
private static List<IAnalyzeHandler> analyzeHandlers;
private static final Logger logger = LoggerFactory.getLogger(AnalyzeHandleFactory.class) ;
static {
analyzeHandlers = new ArrayList<>();
analyzeHandlers.add(new ApiAnalyzeHandler()) ;
//other
//
}
public static void start(JavaSparkContext jsc){
//load trace link from hbase
JavaPairRDD<ImmutableBytesWritable, Result> hBaseTraceRDD = load(jsc) ;
hBaseTraceRDD.cache() ;
//logger.info("load data finish, count {}", hBaseTraceRDD.count());
//key is traceid ,value is span info
JavaPairRDD<String, SpanInfo> hBaseTracePairRDD = hBaseTraceRDD.mapToPair(new PairFunction<Tuple2<ImmutableBytesWritable, Result>, String, SpanInfo>() {
@Override
public Tuple2<String, SpanInfo> call(Tuple2<ImmutableBytesWritable, Result> tuple) throws Exception {
return new Tuple2<String, SpanInfo>(
Bytes.toString(tuple._2().getValue(Bytes.toBytes("span"), Bytes.toBytes("traceid"))), parseSpanInfo(tuple._2())
);
}
}) ;
Iterator itor = analyzeHandlers.iterator() ;
while(itor.hasNext()){
((IAnalyzeHandler)itor.next()).handle(hBaseTracePairRDD);
}
}
/**
*
* @param jsc
* @return
*/
private static JavaPairRDD<ImmutableBytesWritable, Result> load(JavaSparkContext jsc){
logger.info("begin load data");
try {
Configuration hbaseConf = HBaseConfiguration.create();
hbaseConf.set("hbase.zookeeper.quorum", PropertiesFactory.hbase().getZkquorum() );
hbaseConf.set("hbase.zookeeper.property.clientPort", "2181");
hbaseConf.set("hbase.defaults.for.version.skip", "true");
hbaseConf.set(TableInputFormat.INPUT_TABLE, "trace");
long current = System.currentTimeMillis()/1000 ;
long day = current/3600/24 ;
long dayBegin = day*24*3600 - 8*3600;
Scan scan = new Scan();
//只扫描今天的
scan.setStartRow(Bytes.toBytes(String.valueOf(dayBegin))) ;
scan.setStopRow(Bytes.toBytes(String.valueOf(current)));
scan.addColumn(Bytes.toBytes("span"), Bytes.toBytes("name"));
scan.addColumn(Bytes.toBytes("span"), Bytes.toBytes("spanid"));
scan.addColumn(Bytes.toBytes("span"), Bytes.toBytes("traceid"));
scan.addColumn(Bytes.toBytes("span"), Bytes.toBytes("begin"));
scan.addColumn(Bytes.toBytes("span"), Bytes.toBytes("end"));
scan.addColumn(Bytes.toBytes("span"), Bytes.toBytes("parent"));
scan.addColumn(Bytes.toBytes("span"), Bytes.toBytes("service"));
scan.addColumn(Bytes.toBytes("span"), Bytes.toBytes("ip"));
//analyze call trace by time range
//scan.setTimeRange()
scan.setCaching(10000);
scan.setCacheBlocks(false);
ClientProtos.Scan proto = ProtobufUtil.toScan(scan);
String ScanToString = Base64.encodeBytes(proto.toByteArray());
hbaseConf.set(TableInputFormat.SCAN, ScanToString);
HBaseAdmin.checkHBaseAvailable(hbaseConf);
return jsc.newAPIHadoopRDD(hbaseConf, TableInputFormat.class, ImmutableBytesWritable.class, Result.class);
}catch (Exception e){
e.printStackTrace();
}
return null ;
}
public static void main(String args[]){
long current = System.currentTimeMillis()/1000 ;
long day = current/3600/24 ;
long dayBegin = day*24*3600 - 8*3600;
System.out.println("current:" + current + " day begin:" + dayBegin );
}
/**
*
* @param result
* @return
*/
static SpanInfo parseSpanInfo(Result result) {
SpanInfo span = new SpanInfo();
span.setName(Bytes.toString(result.getValue(Bytes.toBytes("span"), Bytes.toBytes("name"))));
span.setTraceid(Bytes.toString(result.getValue(Bytes.toBytes("span"), Bytes.toBytes("traceid"))));
span.setSpanid(Bytes.toString(result.getValue(Bytes.toBytes("span"), Bytes.toBytes("spanid"))));
span.setParent(Bytes.toString(result.getValue(Bytes.toBytes("span"), Bytes.toBytes("parent"))));
span.setBegin(Bytes.toLong(result.getValue(Bytes.toBytes("span"), Bytes.toBytes("begin"))));
span.setEnd(Bytes.toLong(result.getValue(Bytes.toBytes("span"), Bytes.toBytes("end"))));
span.setEndpoint(Bytes.toString(result.getRow()).split(":")[3]);
span.setService(Bytes.toString(result.getValue(Bytes.toBytes("span"), Bytes.toBytes("service"))));
span.setIp(Bytes.toString(result.getValue(Bytes.toBytes("span"), Bytes.toBytes("ip"))));
return span ;
}
}