Skip to content

Commit 6720dca

Browse files
creating keyspace via the driver
1 parent e15bf5a commit 6720dca

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<artifactId>commons-csv</artifactId>
6666
<version>1.3</version>
6767
</dependency>
68-
68+
6969

7070
</dependencies>
7171

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

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.datastax.cdm;
22

3+
import com.datastax.driver.core.Cluster;
34
import com.datastax.driver.core.Session;
45
import com.fasterxml.jackson.core.type.TypeReference;
56
import com.fasterxml.jackson.databind.ObjectMapper;
67
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
78
import org.apache.commons.cli.*;
9+
import org.apache.commons.csv.CSVFormat;
10+
import org.apache.commons.csv.CSVRecord;
811
import org.apache.commons.io.FileUtils;
912
import org.eclipse.jgit.api.Git;
1013
import org.eclipse.jgit.api.errors.GitAPIException;
@@ -110,13 +113,13 @@ private void dump() throws IOException, InterruptedException {
110113
for(String table: config.tables) {
111114
StringBuilder command = new StringBuilder();
112115
command.append("cqlsh -k ")
113-
.append(config.keyspace)
114-
.append(" -e \"")
115-
.append("COPY ")
116-
.append(table)
117-
.append(" TO 'data/")
118-
.append(table)
119-
.append(".csv'\"");
116+
.append(config.keyspace)
117+
.append(" -e \"")
118+
.append("COPY ")
119+
.append(table)
120+
.append(" TO 'data/")
121+
.append(table)
122+
.append(".csv'\"");
120123
System.out.println(command);
121124
Runtime.getRuntime().exec(new String[]{"bash", "-c", command.toString()}).waitFor();
122125
}
@@ -210,12 +213,23 @@ void install(String name) throws IOException, InterruptedException, GitAPIExcept
210213

211214
Config config = mapper.readValue(configFile, Config.class);
212215

213-
String createKeyspace = "cqlsh -e \"DROP KEYSPACE IF EXISTS " + config.keyspace +
214-
"; CREATE KEYSPACE " + config.keyspace +
215-
" WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}\"";
216+
Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
217+
Session session = cluster.connect();
218+
219+
// String createKeyspace = "DROP KEYSPACE IF EXISTS " + config.keyspace +
220+
// "; CREATE KEYSPACE " + config.keyspace +
221+
// " WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}";
222+
223+
StringBuilder createKeyspace = new StringBuilder();
224+
createKeyspace.append(" CREATE KEYSPACE " )
225+
.append(config.keyspace)
226+
.append( " WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}");
216227

217228
System.out.println(createKeyspace);
218-
Runtime.getRuntime().exec(new String[]{"bash", "-c", createKeyspace}).waitFor();
229+
session.execute("DROP KEYSPACE IF EXISTS " + config.keyspace);
230+
session.execute(createKeyspace.toString());
231+
232+
// Runtime.getRuntime().exec(new String[]{"bash", "-c", createKeyspace}).waitFor();
219233

220234

221235
System.out.println("Schema: " + schema);
@@ -226,10 +240,17 @@ void install(String name) throws IOException, InterruptedException, GitAPIExcept
226240

227241
for(String table: config.tables) {
228242
String dataFile = dataPath + table + ".csv";
229-
String command = "COPY " + table + " FROM " + "'" + dataFile + "'";
230-
String loadData= "cqlsh -k " + config.keyspace + " -e \"" + command + "\"";
231-
System.out.println(loadData);
232-
Runtime.getRuntime().exec(new String[]{"bash", "-c", loadData}).waitFor();
243+
244+
Reader in = new FileReader(dataFile);
245+
Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(in);
246+
for(CSVRecord record: records) {
247+
248+
}
249+
250+
// String command = "COPY " + table + " FROM " + "'" + dataFile + "'";
251+
// String loadData= "cqlsh -k " + config.keyspace + " -e \"" + command + "\"";
252+
// System.out.println(loadData);
253+
// Runtime.getRuntime().exec(new String[]{"bash", "-c", loadData}).waitFor();
233254
}
234255

235256

0 commit comments

Comments
 (0)