Repository navigation
Expand file tree
/
Copy pathBulkLoader.java
More file actions
199 lines (183 loc) · 8.64 KB
/
Copy pathBulkLoader.java
File metadata and controls
199 lines (183 loc) · 8.64 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
import java.io.*;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import com.opencsv.CSVReader;
import org.apache.cassandra.config.Config;
import org.apache.cassandra.dht.Murmur3Partitioner;
import org.apache.cassandra.exceptions.InvalidRequestException;
import org.apache.cassandra.io.sstable.CQLSSTableWriter;
public class BulkLoader {
static String filename;
static String outdir;
static String schema;
static String insert;
public static void main(String[] args) throws IOException {
if (args.length < 4) {
String concat = Arrays.toString(args);
System.out.println("Expecting 4 arguments - <keyspace>, <table>, <csv_file>, <output dir>");
System.out.println("Got: " + concat.substring(1, concat.length() -1));
System.exit(1);
}
int lineNumber = 0;
long start = System.currentTimeMillis();
String keyspace = args[0];
String table = args[1];
filename = args[2];
outdir = args[3];
System.out.println("keyspace: "+keyspace);
System.out.println("table: "+table);
System.out.println("filename: "+filename);
System.out.println("outdir: "+outdir);
// Schema and Insert for bulk load
if (table.equals("id_annotation")) {
schema = String.format("CREATE TABLE %s.%s (" +
"id int, " +
"source text, " +
"md5 text, " +
"is_protein boolean, " +
"single text, " +
"lca list<text>, " +
"accession list<text>, " +
"function list<text>, " +
"organism list<text>, " +
"PRIMARY KEY (id, source) " +
")", keyspace, table);
insert = String.format("INSERT INTO %s.%s (" +
"id, source, md5, is_protein, single, lca, accession, function, organism" +
") VALUES (" +
"?, ?, ?, ?, ?, ?, ?, ?, ?" +
")", keyspace, table);
} else if (table.equals("md5_annotation")) {
schema = String.format("CREATE TABLE %s.%s (" +
"md5 text, " +
"source text, " +
"is_protein boolean, " +
"single text, " +
"lca list<text>, " +
"accession list<text>, " +
"function list<text>, " +
"organism list<text>, " +
"PRIMARY KEY (md5, source) " +
")", keyspace, table);
insert = String.format("INSERT INTO %s.%s (" +
"md5, source, is_protein, single, lca, accession, function, organism" +
") VALUES (" +
"?, ?, ?, ?, ?, ?, ?, ?" +
")", keyspace, table);
} else if (table.equals("job_md5s")) {
schema = String.format("CREATE TABLE %s.%s (" +
"version int, " +
"job int, " +
"source text, " +
"md5 text, " +
"data frozen<md5_info>, " +
"accession list<text>, " +
"function list<text>, " +
"organism list<text>, " +
"PRIMARY KEY ((version, job), source, md5) " +
")", keyspace, table);
insert = String.format("INSERT INTO %s.%s (" +
"version, job, source, md5, data, accession, function, organism" +
") VALUES (" +
"?, ?, ?, ?, ?, ?, ?, ?" +
")", keyspace, table);
} else {
System.out.println("Unsupported table type: " + table);
System.exit(1);
}
// magic!
Config.setClientMode(true);
// Create output directory that has keyspace and table name in the path
File outputDir = new File(outdir + File.separator + keyspace + File.separator + table);
if (!outputDir.exists() && !outputDir.mkdirs()) {
throw new RuntimeException("Cannot create output directory: " + outputDir);
}
// Prepare SSTable writer
CQLSSTableWriter.Builder builder = CQLSSTableWriter.builder();
// set output directory
builder.inDirectory(outputDir)
// set target schema
.forTable(schema)
// set CQL statement to put data
.using(insert)
// set partitioner if needed - default is Murmur3Partitioner so set if you use different one.
.withPartitioner(new Murmur3Partitioner());
CQLSSTableWriter writer = builder.build();
// set cvs reader / parser
try (
CSVReader csvReader = new CSVReader(new FileReader(filename));
) {
// Write to SSTable while reading data
String[] line;
while ((line = csvReader.readNext()) != null) {
// We use Java types here based on
// http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/DataType.Name.html#asJavaClass%28%29
if (table.equals("id_annotation")) {
writer.addRow(Integer.parseInt(line[0]),
line[1],
line[2],
Boolean.valueOf(line[3]),
line[4],
parseList(line[5]),
parseList(line[6]),
parseList(line[7]),
parseList(line[8]));
} else if (table.equals("md5_annotation")) {
writer.addRow(line[0],
line[1],
Boolean.valueOf(line[2]),
line[3],
parseList(line[4]),
parseList(line[5]),
parseList(line[6]),
parseList(line[7]));
}
// Print nK
lineNumber += 1;
if (lineNumber % 10000 == 0) {
System.out.println((lineNumber / 1000) + "K");
}
}
} catch (InvalidRequestException | IOException e) {
e.printStackTrace();
}
try {
writer.close();
} catch (IOException ignore) {}
// done
long end = System.currentTimeMillis();
System.out.println("Successfully parsed " + lineNumber + " lines.");
System.out.println("Execution time was " + ((end-start) / 1000) + " seconds.");
System.exit(0);
}
public static List<String> parseList (String listStr) {
List<String> aList = new ArrayList<String>();
listStr = listStr.trim();
if (listStr.isEmpty()) {
return aList;
}
// remove leading and trailing brackets
listStr = listStr.substring(1, listStr.length() - 1).trim();
if (listStr.isEmpty()) {
return aList;
}
// split by comma
String[] parts = listStr.split(",");
for (int i=0; i<parts.length; i++) {
// add to list, remove leading and trailing single-quotes
String item = parts[i].trim();
if (item.length() > 1) {
aList.add( item.substring(1, item.length() - 1).trim() );
} else {
aList.add( new String() );
}
}
return aList;
}
}