Skip to content

Commit c47a950

Browse files
building out raw CQL generation
1 parent bf90f64 commit c47a950

File tree

6 files changed

+75
-41
lines changed

6 files changed

+75
-41
lines changed

cdm.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ keyspace: cdmtest
22
tables:
33
- users
44
- alltypes
5+
- maptest
56
version: 2.2

data/maptest.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1,{'name': 'O'Hare'}

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@
6666
<version>1.3</version>
6767
</dependency>
6868

69+
<dependency>
70+
<groupId>junit</groupId>
71+
<artifactId>junit</artifactId>
72+
<version>4.12</version>
73+
<scope>test</scope>
74+
</dependency>
75+
6976

7077
</dependencies>
7178

schema.cql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@ CREATE TABLE alltypes (
88
num int,
99
ts timeuuid
1010
);
11+
12+
create table maptest (
13+
id int,
14+
m map<text, text>,
15+
primary key (id)
16+
);

src/main/java/com/datastax/cdm/CassandraDatasetManager.java

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.datastax.cdm;
22

33
import com.datastax.driver.core.*;
4-
import com.datastax.driver.core.querybuilder.Insert;
5-
import com.datastax.driver.core.querybuilder.QueryBuilder;
64
import com.fasterxml.jackson.core.type.TypeReference;
75
import com.fasterxml.jackson.databind.ObjectMapper;
86
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
@@ -15,6 +13,7 @@
1513
import org.eclipse.jgit.api.errors.InvalidRemoteException;
1614
import org.eclipse.jgit.api.errors.TransportException;
1715

16+
1817
import javax.management.Query;
1918
import java.lang.StringBuilder;
2019

@@ -24,8 +23,7 @@
2423
import java.math.BigDecimal;
2524
import java.net.MalformedURLException;
2625
import java.net.URL;
27-
import java.util.List;
28-
import java.util.Map;
26+
import java.util.*;
2927

3028
/**
3129
* Created by jhaddad on 6/29/16.
@@ -219,20 +217,22 @@ void install(String name) throws IOException, InterruptedException, GitAPIExcept
219217
Config config = mapper.readValue(configFile, Config.class);
220218

221219
Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
222-
Session session = cluster.connect();
220+
{
221+
Session session = cluster.connect();
223222

224223
// String createKeyspace = "DROP KEYSPACE IF EXISTS " + config.keyspace +
225224
// "; CREATE KEYSPACE " + config.keyspace +
226225
// " WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}";
227226

228-
StringBuilder createKeyspace = new StringBuilder();
229-
createKeyspace.append(" CREATE KEYSPACE " )
230-
.append(config.keyspace)
231-
.append( " WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}");
227+
StringBuilder createKeyspace = new StringBuilder();
228+
createKeyspace.append(" CREATE KEYSPACE ")
229+
.append(config.keyspace)
230+
.append(" WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}");
232231

233-
System.out.println(createKeyspace);
234-
session.execute("DROP KEYSPACE IF EXISTS " + config.keyspace);
235-
session.execute(createKeyspace.toString());
232+
System.out.println(createKeyspace);
233+
session.execute("DROP KEYSPACE IF EXISTS " + config.keyspace);
234+
session.execute(createKeyspace.toString());
235+
}
236236

237237
System.out.println("Schema: " + schema);
238238
String loadSchema = "cqlsh -k " + config.keyspace + " -f " + schema;
@@ -244,53 +244,54 @@ void install(String name) throws IOException, InterruptedException, GitAPIExcept
244244
.addContactPoint("127.0.0.1")
245245
.build();
246246

247+
Session session = cluster2.connect(config.keyspace);
248+
247249
for(String table: config.tables) {
248250
String dataFile = dataPath + table + ".csv";
251+
Iterable<CSVRecord> records = openCSV(dataFile);
249252

250-
Reader in = new FileReader(dataFile);
251-
Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(in);
252253
System.out.println("Importing " + table);
253254
KeyspaceMetadata keyspaceMetadata = cluster2.getMetadata()
254255
.getKeyspace(config.keyspace);
255256
TableMetadata tableMetadata = keyspaceMetadata.getTable(table);
256-
// PreparedStatement p = session.prepare();
257+
257258
List<ColumnMetadata> columns = tableMetadata.getColumns();
258259

260+
StringJoiner fields = new StringJoiner(", ");
261+
StringJoiner values = new StringJoiner(", ");
262+
263+
HashMap types = new HashMap();
264+
265+
for(ColumnMetadata c: columns) {
266+
fields.add(c.getName());
267+
types.put(c.getName(), c.getType().getName().toString());
268+
}
269+
270+
PreparedStatement p = session.prepare(insert_query.toString());
271+
HashSet needs_quotes = new HashSet();
272+
needs_quotes.add("text");
273+
needs_quotes.add("datetime");
274+
259275
for(CSVRecord record: records) {
260276
// generate a CQL statement
261-
Insert insert = QueryBuilder.insertInto(tableMetadata);
262-
263-
int i = 0;
264-
for(ColumnMetadata cm: columns) {
265-
String t = cm.getType().getName().toString().toLowerCase();
266-
System.out.println(t);
267-
if(t.equals("int")) {
268-
insert.value(cm.getName(), new Integer(record.get(i)));
269-
}
270-
else if(t.equals("float")) {
271-
insert.value(cm.getName(), new Float(record.get(i)));
272-
}
273-
else if(t.equals("decimal")) {
274-
insert.value(cm.getName(), new BigDecimal(record.get(i)));
275-
}
276-
else if(t.equals("map")) {
277-
insert.value(cm.getName(), new BigDecimal(record.get(i)));
278-
}
279-
else {
280-
insert.value(cm.getName(), record.get(i));
281-
}
282-
i++;
283-
}
284-
String query = insert.toString();
285-
System.out.println(query);
286-
session.execute(query);
277+
String cql = generateCQL(record, types);
278+
session.execute(cql);
287279
}
288280
}
289281

290282

291283
System.out.println("Loading data");
292284
}
293285

286+
Iterable<CSVRecord> openCSV(String path) throws IOException {
287+
Reader in = new FileReader(path);
288+
Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(in);
289+
return records;
290+
}
291+
292+
String generateCQL(CSVRecord record, HashMap<String, String> types) {
293+
return "";
294+
}
294295

295296
void update() throws IOException {
296297
System.out.println("Updating datasets...");
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.datastax.cdm;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.apache.commons.csv.CSVRecord;
6+
import org.junit.Test;
7+
8+
/**
9+
* Created by jhaddad on 9/5/16.
10+
*/
11+
public class CassandraDatasetManagerTest {
12+
13+
@Test
14+
public void testCQLStatementGeneration() {
15+
16+
}
17+
18+
}

0 commit comments

Comments
 (0)