1
votes

We are trying to insert data from CSV file into Cassandra using the DataStax driver for Java. What are the available methods to do so?

We are currently using running cqlsh to load from a CSV file.

1
please describe particular problem that you have with Java Driver or cqlsh. You just need to open your CSV file, parse the line, and insert data into CassandraAlex Ott
there is something called copyCommand to insert from csv to cassandra have a look docs.datastax.com/en/cql/3.3/cql/cql_reference/cqlshCopy.htmlRaman Mishra

1 Answers

2
votes

The question is quite vague. Usually, you should be able to provide code, and give an example of something that isn't working quite right for you.

That being said, I just taught a class (this week) on this subject for our developers at work. So I can give you some quick examples.

First of all, you should have a separate class built to handle your Cassandra connection objects. I usually build it with a couple of constructors so that it can be called in a couple different ways. But each essentially calls a connect method, which looks something like this:

public void connect(String[] nodes, String user, String pwd, String dc) {
    QueryOptions qo = new QueryOptions();
    qo.setConsistencyLevel(ConsistencyLevel.LOCAL_ONE);

    cluster = Cluster.builder()
        .addContactPoints(nodes)
        .withCredentials(user,pwd)
        .withQueryOptions(qo)
        .withLoadBalancingPolicy(
            new TokenAwarePolicy(
                DCAwareRoundRobinPolicy.builder()
                .withLocalDc(dc)
                .build()
                )
        )
        .build();
        session = cluster.connect();

With that in place, I also write a few simple methods to expose some functionality of the of the session object:

public ResultSet query(String strCQL) {
    return session.execute(strCQL);
}

public PreparedStatement prepare(String strCQL) {
  return session.prepare(strCQL);
}

public ResultSet query(BoundStatement bStatement) {
  return session.execute(bStatement);
}

With those in-place, I can then call these methods from within a service layer. A simple INSERT (with preparing a statement and binding values to it) looks like this:

String[] nodes = {"10.6.8.2","10.6.6.4"};
CassandraConnection conn = new CassandraConnection(nodes, "aploetz", "flynnLives", "West-DC");

String userID = "Aaron";
String value = "whatever";
String strINSERT = "INSERT INTO stackoverflow.timestamptest "
    + "(userid, activetime, value) "
    + "VALUES (?,dateof(now()),?)";     

PreparedStatement pIStatement = conn.prepare(strINSERT);
BoundStatement bIStatement = new BoundStatement(pIStatement);
bIStatement.bind(userID, value);
conn.query(bIStatement);

In addition, the DataStax Java Driver has a folder called "examples" in their Git repo. Here's a link to the "basic" examples, which I recommend reading further.