forked from dolphindb/api-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFSWritingWithMultiThread.java
More file actions
225 lines (191 loc) · 7.58 KB
/
DFSWritingWithMultiThread.java
File metadata and controls
225 lines (191 loc) · 7.58 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
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
package com.dolphindb;
import com.xxdb.DBConnection;
import com.xxdb.data.*;
import com.xxdb.data.Vector;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.*;
public class DFSWritingWithMultiThread {
static Hashtable<String, DBTaskItem> tables;
static Hashtable<String, List<Integer>> groups;
static String HOST;
static int PORT;
static String DBPATH;
static String TBNAME;
static int FREQ;
static int BATCHSIZE;
static int insertRowCount = 0;
public static void main(String[] args) throws Exception {
groups = new Hashtable<>();
groups.put("group1", Arrays.asList(0, 1, 2));
groups.put("group2", Arrays.asList(3, 4, 5));
groups.put("group3", Arrays.asList(6, 7));
groups.put("group4", Arrays.asList(8, 9));
tables = new Hashtable<>();
if (args.length == 0) {
BATCHSIZE = 2000;
FREQ = 50;
HOST = "localhost"; //Your DolphinDB server HOST
PORT = 8848; //Your DolphinDB server PORT
} else if (args.length != 4) {
try {
FREQ = Integer.parseInt(args[0]);
BATCHSIZE = Integer.parseInt(args[1]);
HOST = args[2];
PORT = Integer.parseInt(args[3]);
} catch (Exception e) {
System.out.println("Wrong arguments");
}
} else {
System.out.println("Wrong arguments");
return;
}
DBPATH = "dfs://DolphinDBUUID";
TBNAME = "device_status";
new com.dolphindb.DFSWritingWithMultiThread().generateData(tables, BATCHSIZE, FREQ);
}
private BasicTable createBasicTable(int n) throws Exception {
List<String> colNames = new ArrayList<String>();
colNames.add("time");
colNames.add("areaId");
colNames.add("deviceId");
colNames.add("value");
List<Vector> cols = new ArrayList<Vector>() {
};
//Timnstamp
BasicTimestampVector timestamp = new BasicTimestampVector(n);
BasicUuidVector areaId = new BasicUuidVector(n);
BasicUuidVector deviceId = new BasicUuidVector(n);
BasicDoubleVector value = new BasicDoubleVector(n);
cols.add(timestamp);
cols.add(areaId);
cols.add(deviceId);
cols.add(value);
return new BasicTable(colNames, cols);
}
public void generateData(Hashtable<String, DBTaskItem> tbs, int batchSize, int freq) throws Exception {
for (String key : groups.keySet()) {
tables.put(key, new DBTaskItem(null, null));
}
List<BasicUuid> areas = new ArrayList<>();
for (int t = 0; t < 100; t++) {
areas.add(BasicUuid.fromString(getRandomUUID()));
}
List<BasicUuid> devices = new ArrayList<>();
for (int t = 0; t < 100; t++) {
devices.add(BasicUuid.fromString(getRandomUUID()));
}
new Thread(new TaskConsumer()).start();
long st = System.currentTimeMillis();
for (int j = 0; j < batchSize * freq; j++) {
generateOneRow(tbs, areas, devices, BATCHSIZE);
}
long ed = System.currentTimeMillis();
System.out.println("create data cost : " + (ed - st));
}
public String getGroupId(int hashCode) {
for (String key : groups.keySet()) {
List<Integer> hashCodeList = groups.get(key);
if (hashCodeList.contains(hashCode))
return key;
}
return null;
}
public void generateOneRow(Hashtable<String, DBTaskItem> tbs, List<BasicUuid> areas, List<BasicUuid> devices, int n) throws Exception {
Random rand = new Random();
BasicUuid areaUUID = areas.get(rand.nextInt(areas.size()));
BasicUuid deviceUUID = devices.get(rand.nextInt(devices.size()));
int key = areaUUID.hashBucket(10);
String groupId = getGroupId(key);
if (tbs.containsKey(groupId)) {
if (tbs.get(groupId).currentTable == null) {
tbs.get(groupId).currentTable = new BasicTableEx(createBasicTable(n));
}
} else {
throw new Exception("group out of range");
}
if (tbs.get(groupId).currentTable.add(LocalDateTime.of(2020, 1, 1, 1, 1, 1, 1), areaUUID, deviceUUID, (double) (50 * rand.nextDouble()))) {
if (tbs.get(groupId).currentTable.isFull()) {
tbs.get(groupId).pushToQueue();
}
}
}
public static String getRandomUUID() {
UUID uuid = UUID.randomUUID();
return uuid.toString();
}
public class TaskConsumer implements Runnable {
public void run() {
for (String key : groups.keySet()) {
new Thread(new DDBProxy(key)).start();
}
}
}
public class DDBProxy implements Runnable {
String _key = null;
public DDBProxy(String key) {
_key = key;
}
public void run() {
do {
if (tables.get(_key).TaskQueue.size() > 0) {
BasicTable data = tables.get(_key).TaskQueue.poll();
saveDataToDDB(data);
insertRowCount += data.rows();
if (insertRowCount < 2000 || insertRowCount > 90000)
System.out.println(String.format("insertRowCount = %s ; now is %s ", insertRowCount, LocalDateTime.now()));
}
} while (true);
}
}
public static void saveDataToDDB(BasicTable data) {
DBConnection conn = new DBConnection();
try {
conn.connect(HOST, PORT, String.format("def saveData(data){ loadTable('%s','%s').tableInsert(data)}", DBPATH, TBNAME));
conn.login("admin", "123456", false);
List<Entity> arg = new ArrayList<Entity>(1);
arg.add(data);
long st = System.currentTimeMillis();
conn.run("saveData", arg);
long ed = System.currentTimeMillis();
System.out.println(String.format("insert %s rows, cost %s ms", data.rows(), ed - st));
} catch (IOException ex) {
System.out.println(data.getColumn(1).getString());
ex.printStackTrace();
}
conn.close();
}
public class BasicTableEx {
int _currentAddIndex = 0;
BasicTable _bt = null;
public BasicTableEx(BasicTable table) {
_bt = table;
}
public boolean add(LocalDateTime timestamp, BasicUuid areaId, BasicUuid deviceId, double value) throws Exception {
if (_currentAddIndex >= _bt.rows()) return false;
((BasicTimestampVector) _bt.getColumn(0)).setTimestamp(_currentAddIndex, timestamp);
((BasicUuidVector) _bt.getColumn(1)).set(_currentAddIndex, areaId);
((BasicUuidVector) _bt.getColumn(2)).set(_currentAddIndex, deviceId);
((BasicDoubleVector) _bt.getColumn(3)).setDouble(_currentAddIndex, value);
_currentAddIndex++;
return true;
}
public boolean isFull() {
return _bt.rows() == _currentAddIndex;
}
}
public class DBTaskItem {
public BasicTableEx currentTable;
public ConcurrentLinkedQueue<BasicTable> TaskQueue = new ConcurrentLinkedQueue<>();
private Thread _thread;
public DBTaskItem(BasicTableEx currTable, Thread thread) {
currentTable = currTable;
this._thread = thread;
}
public void pushToQueue() {
TaskQueue.add(currentTable._bt);
currentTable = null;
}
}
}