-
Notifications
You must be signed in to change notification settings - Fork 77
/
01.DateTutorials.java
135 lines (122 loc) · 4.5 KB
/
01.DateTutorials.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
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import com.xxdb.DBConnection;
import com.xxdb.comm.ErrorCodeInfo;
import com.xxdb.data.*;
import com.xxdb.multithreadedtablewriter.MultithreadedTableWriter;
public class DateTest {
private static final String CSV = "./lib/taq.csv";
private static final DBConnection conn = new DBConnection();
static {
try {
conn.connect("localhost", 8848, "admin", "123456");
conn.run("t = table(100:0,`symbol`datetime`bid`ofr`bidsize`ofrsize`mode`ex`mmid,[SYMBOL,DATETIME,DOUBLE,DOUBLE,LONG,LONG,INT,CHAR,SYMBOL])\n" +
"share t as timeTest;");
} catch (IOException e) {
e.printStackTrace();
}
}
public static int getTime(String timeStr){
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy.MM.dd H:mm:ss");
LocalDateTime ldt = LocalDateTime.parse(timeStr,df);
return Utils.countSeconds(ldt);
}
public static LocalDateTime getDateTime(String timeStr){
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy.MM.dd H:mm:ss");
LocalDateTime ldt = LocalDateTime.parse(timeStr,df);
return ldt;
}
public static void write() throws IOException {
LinkedList<String> symbolList = new LinkedList<>();// symbol
LinkedList<Integer> dtList = new LinkedList<>();// datetime
LinkedList<Double> bidList = new LinkedList<>();// bid
LinkedList<Double> ofrList = new LinkedList<>();// ofr
LinkedList<Long> bidSizeList = new LinkedList<>();// bidSize
LinkedList<Long> ofrSizeList = new LinkedList<>();// ofrSize
LinkedList<Integer> modeList = new LinkedList<>();// mode
LinkedList<Byte> exList = new LinkedList<>();// ex
LinkedList<String> mmidList = new LinkedList<>();// mmid
try (Reader reader = Files.newBufferedReader(Paths.get(CSV));
CSVReader csvReader = new CSVReader(reader)) {
String[] record;
csvReader.readNext();// skip first line
while ((record = csvReader.readNext()) != null) {
symbolList.add(record[0]);
dtList.add(getTime(record[1]+" "+record[2]));
bidList.add(Double.parseDouble(record[3]));
ofrList.add(Double.parseDouble(record[4]));
bidSizeList.add(Long.parseLong(record[5]));
ofrSizeList.add(Long.parseLong(record[6]));
modeList.add(Integer.parseInt(record[7]));
exList.add((byte)record[8].charAt(1));
mmidList.add(record[9]);
}
} catch (IOException | CsvValidationException ex) {
ex.printStackTrace();
}
List<Entity> data = Arrays.asList(
new BasicSymbolVector(symbolList),
new BasicDateTimeVector(dtList),
new BasicDoubleVector(bidList),
new BasicDoubleVector(ofrList),
new BasicLongVector(bidSizeList),
new BasicLongVector(ofrSizeList),
new BasicIntVector(modeList),
new BasicByteVector(exList),
new BasicSymbolVector(mmidList)
);
conn.run("tableInsert{\"timeTest\"}", data);
}
public static void mtwWrite(){
final int batchSize = 10000;
final int throttle = 1;
final int threadCount = 1;
final String partitionCol = "datetime";
ErrorCodeInfo pErrorInfo=new ErrorCodeInfo();
MultithreadedTableWriter multithreadedTableWriter = null;
try {
multithreadedTableWriter = new MultithreadedTableWriter("localhost", 8848, "admin", "123456", "timeTest", "",
false, false, null, batchSize, throttle, threadCount, partitionCol);
} catch (Exception e) {
e.printStackTrace();
}
try (Reader reader = Files.newBufferedReader(Paths.get(CSV));
CSVReader csvReader = new CSVReader(reader)) {
String[] record;
csvReader.readNext();// skip first line
while ((record = csvReader.readNext()) != null) {
multithreadedTableWriter.insert(pErrorInfo,
record[0],
getDateTime(record[1]+" "+record[2]),
Double.parseDouble(record[3]),
Double.parseDouble(record[4]),
Long.parseLong(record[5]),
Long.parseLong(record[6]),
Integer.parseInt(record[7]),
(byte)record[8].charAt(1),
record[9]
);
}
multithreadedTableWriter.waitForThreadCompletion();
} catch (IOException | CsvValidationException | InterruptedException ex) {
ex.printStackTrace();
}
}
public static void select() throws IOException {
BasicTable res = (BasicTable)conn.run("select * from timeTest");
System.out.println(res.getString());
}
public static void main(String[] args) throws IOException {
// mtwWrite();
select();
}
}