-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSSTableWriterSplitAll.java
More file actions
150 lines (128 loc) · 4.53 KB
/
Copy pathTestSSTableWriterSplitAll.java
File metadata and controls
150 lines (128 loc) · 4.53 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
import java.nio.ByteBuffer;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.net.InetAddress;
import org.apache.cassandra.io.sstable.CQLSSTableWriter;
import org.apache.cassandra.exceptions.InvalidRequestException;
import org.apache.cassandra.config.Config;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.Host;
public class TestSSTableWriterSplitAll {
static String filename;
static String outdir;
static String ip = "127.0.0.1";
static String keyspace = "";
static String table = "";
static String delimiter = ",";
static String schema = "";
static String insert = "";
static String dummyString = "";
static long dummyLong = 0;
static Cluster cluster;
static Session session;
static Metadata metadata;
static PreparedStatement stmt;
private static void init() {
cluster = Cluster.builder().addContactPoint(ip).build();
metadata = cluster.getMetadata();
session = cluster.newSession();
stmt = session.prepare(insert);
}
private static void cleanup() {
session.close();
cluster.close();
}
public static void main(String[] args) throws IOException {
if (args.length != 5) {
System.err.println("Expecting 5 arguments: <filename> <output directory> <keyspace> <tablename> <IP address>");
System.exit(1);
}
filename = args[0];
outdir = args[1];
keyspace = args[2];
table = args[3];
ip = args[4];
CsvParser parser = new CsvParser();
schema = "CREATE TABLE IF NOT EXISTS " + keyspace + "." + table + " (pkey TEXT, ccol BIGINT, data TEXT, PRIMARY KEY ((pkey), ccol));";
insert = "INSERT INTO " + keyspace + "." + table + " (pkey, ccol, data) VALUES (?, ?, ?);";
System.err.println("*******A");
init();
System.err.println("*******b");
Config.setClientMode(true);
BufferedReader reader = new BufferedReader(new FileReader(filename));
Map<String,CQLSSTableWriter> map = new HashMap<String,CQLSSTableWriter>();
System.err.println("*******C");
System.err.println("Hosts: " + metadata.getAllHosts());
for (Host h : metadata.getAllHosts()) {
String sip = h.getAddress().getHostAddress();
String sipdir = outdir + "/" + sip;
System.err.println("sipdir = " + sipdir);
File directory = new File(sipdir);
if (!directory.exists())
directory.mkdirs();
CQLSSTableWriter writer = CQLSSTableWriter.builder().inDirectory(sipdir).forTable(schema).using(insert).build();
map.put(sip, writer);
}
System.err.println("*******D");
String line;
int lineNumber = 1;
long timestamp = System.currentTimeMillis() * 1000;
while ((line = reader.readLine()) != null) {
if (parser.parse(line, delimiter, lineNumber)) {
// if (0 == lineNumber % 10000)
// System.err.println("Got to line " + lineNumber);
BoundStatement bind = stmt.bind(parser.pkey, dummyLong, dummyString);
ByteBuffer bb = bind.getBytesUnsafe(0);
Set<Host> endpts = metadata.getReplicas(keyspace, bb);
for (Host h : endpts) {
parser.addRow(map.get(h.getAddress().getHostAddress()));
}
}
lineNumber++;
}
cleanup();
for (CQLSSTableWriter w : map.values())
w.close();
System.exit(0);
}
static class CsvParser {
public String pkey;
public long ccol;
public String data;
boolean parse(String line, String delimiter, int lineNumber) {
String[] columns = line.split(delimiter);
if (3 != columns.length) {
System.err.println(String.format("Invalid input '%s' at line %d of %s", line, lineNumber, filename));
return false;
}
try {
pkey = columns[0].trim();
ccol = Long.parseLong(columns[1].trim());
data = columns[2].trim();
return true;
}
catch (NumberFormatException e) {
System.err.println(String.format("Invalid number in input '%s' at line %d of %s", line, lineNumber, filename));
return false;
}
}
void addRow(CQLSSTableWriter writer) {
try {
writer.addRow(pkey, ccol, data);
}
catch (Exception e) {
System.err.println("Couldn't addRow: " + e.getMessage());
}
}
}
// public static void main(String [] args) throws IOException {
// System.err.println("++++++");
// TestSSTableWriterSplit n = new TestSSTableWriterSplit();
// n.run(args);
// }
}