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
package mobvista.dmp.datasource.personagraph;
import com.google.gson.JsonObject;
import mobvista.dmp.util.DelayUtil;
import mobvista.prd.datasource.util.GsonUtil;
import mobvista.prd.datasource.util.HttpUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.util.ConfigUtil;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import java.io.*;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
/**
*
*/
public class MessageSender implements Runnable {
private static final Logger logger = Logger.getLogger(MessageSender.class);
private static final String reqURL = "https://api.personagraph.com/pgedge/ingestion/events/1.0";
private static final String requestBody = "{\"source\":{\"name\":\"com.mobvista.dataplatform\",\"type\":\"UNKNOWN\"},\"identifiers\":{\"primary\":{\"id\":\"%s\",\"type\":\"DEVICE_ID\",\"pii\":\"FALSE\"}}}";
private String token;
private String filePath;
private Boolean stop = false;
private Boolean pause = false;
private BufferedWriter writer;
private CountDownLatch downLatch;
private BlockingQueue blockingQueue;
public MessageSender(String token, String filePath, BlockingQueue blockingQueue, CountDownLatch downLatch) throws Exception {
this.token = token;
this.filePath = filePath;
this.downLatch = downLatch;
this.blockingQueue = blockingQueue;
buildWriter();
}
@Override
public void run() {
DelayUtil du = new DelayUtil();
Map header = new HashMap<String, String>();
header.put("Content-Type", "application/json; charset=utf-8;");
header.put("Authorization", " Bearer " + token);
try {
while (true) {
if (pause || stop) {
if (pause) {
Thread.sleep(2000);
} else {
break;
}
} else {
String deviceId = String.valueOf(blockingQueue.take());
du.setStart(System.currentTimeMillis());
JsonObject reqBoday = GsonUtil.String2JsonObject(String.format(requestBody, deviceId));
String response = HttpUtil.postRequestJson(reqURL, reqBoday, header, logger);
writer.write(deviceId);
writer.newLine();
du.setEnd(System.currentTimeMillis());
du.printEnd("Finish upload " + deviceId);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (downLatch != null) {
downLatch.countDown();
}
}
}
public void buildWriter() throws Exception {
Path path = new Path(filePath);
FileSystem fs = FileSystem.get(URI.create(filePath), new Configuration());
OutputStream out = fs.create(path);
Writer writer = new OutputStreamWriter(out);
BufferedWriter fileWriter = new BufferedWriter(writer);
this.writer = fileWriter;
}
}