11package com .datastax .cdm ;
22
33import com .datastax .driver .core .*;
4- import com .datastax .driver .core .querybuilder .Insert ;
5- import com .datastax .driver .core .querybuilder .QueryBuilder ;
64import com .fasterxml .jackson .core .type .TypeReference ;
75import com .fasterxml .jackson .databind .ObjectMapper ;
86import com .fasterxml .jackson .dataformat .yaml .YAMLFactory ;
1513import org .eclipse .jgit .api .errors .InvalidRemoteException ;
1614import org .eclipse .jgit .api .errors .TransportException ;
1715
16+
1817import javax .management .Query ;
1918import java .lang .StringBuilder ;
2019
2423import java .math .BigDecimal ;
2524import java .net .MalformedURLException ;
2625import 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..." );
0 commit comments