0

I have started working with Cassandra database. I am planning to use Datastax API to upsert/read into/from Cassandra database. I am totally new to this Datastax API (which uses new Binary protocol) and I am not able to find lot of documentations as well which have some proper examples.

create column family profile
    with key_validation_class = 'UTF8Type'
    and comparator = 'UTF8Type'
    and default_validation_class = 'UTF8Type'
    and column_metadata = [
      {column_name : crd, validation_class : 'DateType'}
      {column_name : lmd, validation_class : 'DateType'}
      {column_name : account, validation_class : 'UTF8Type'}
      {column_name : advertising, validation_class : 'UTF8Type'}
      {column_name : behavior, validation_class : 'UTF8Type'}
      {column_name : info, validation_class : 'UTF8Type'}
      ];

Now below is the Singleton class that I have created for connecting to Cassandra database using Datastax API which uses new Binary protocol-

public class CassandraDatastaxConnection {

    private static CassandraDatastaxConnection _instance;
    protected static Cluster cluster;
    protected static Session session;


    public static synchronized CassandraDatastaxConnection getInstance() {
        if (_instance == null) {
            _instance = new CassandraDatastaxConnection();
        }
        return _instance;
    }

    /**
     * Creating Cassandra connection using Datastax API
     *
     */
    private CassandraDatastaxConnection() {

        try{
            cluster = Cluster.builder().addContactPoint("localhost").build();
            session = cluster.connect("my_keyspace");           
        } catch (NoHostAvailableException e) {
            throw new RuntimeException(e);
        }
    }

    public static Cluster getCluster() {
        return cluster;
    }

    public static Session getSession() {
        return session;
    }
}

First question- let me know if I am missing anything in the above singleton class while making connection to Cassandra database using Datastax API which uses new Binary protocol.

Second question- Now I am trying to upsert and read data into/from Cassandra database-

These are the methods I have in my DAO's which will use the above Singleton class-

public Map<String, String> getColumnNames(final String userId, final Collection<String> columnNames) {

    //I am not sure what I am supposed to do here?
    //Given a userId, I need to retrieve those columnNames from the Cassandra database
    //And then put it in the map with column name and its value and then finally return the map

    Map<String, String> attributes = new ConcurrentHashMap<String, String>();

    for(String col : columnNames ) {
        attributes.put(col, colValue);
    }

    return attributes;
}


/**
 * Performs an upsert of the specified attributes for the specified id.
 */
public void upsertAttributes(final String userId, final Map<String, String> columnNameAndValue) {

    //I am not sure what I am supposed to do here to upsert the data in Cassandra database.
    //Given a userId, I need to upsert the columns values into Cassandra database.
    //columnNameAndValue is the map which will have column name as the key and corresponding column value as the value.

}

Can anyone help me with this? I am totally new to this Datastax API which is using new Binary protocol so having lot of problem on this.

Thanks for the help.

su-
  • 3,116
  • 3
  • 32
  • 42
arsenal
  • 23,366
  • 85
  • 225
  • 331
  • are you able to connect or its showing any error? – abhi Apr 19 '13 at 13:42
  • Yeah, I am also not able to connect to Cassandra database using Datastax Java driver. I am also getting the same exception that you got `NoHostAvailableException`. Were you able to fix this problem? – arsenal Apr 20 '13 at 01:43
  • ohh....i was also having the same prob, then posted a question a cassandra-forum, and yes able to finally solve it. Which version of cassandra you are using?? 1.2.? – abhi Apr 20 '13 at 04:12
  • I am running 1.2.3. I also send you an email by which I am trying to make a connection and connect to cluster. – arsenal Apr 20 '13 at 04:18
  • By the way, what changes you have made in cassandra.yaml file? I will also make that change and run it and see whether that will make any difference or not. – arsenal Apr 20 '13 at 04:28
  • @abhi,And how are you trying to make connection to cluster using java-driver? Can you provide that example as well? May be I am doing something wrong in that? – arsenal Apr 20 '13 at 04:39

1 Answers1

2

In your cassandra.yaml file look for the tag start_native_transport, by default its disabled, enable it.

Playing with Datastax Java Driver is quite similar like jdbc driver.

Insertion code

 String query = "insert into test(key,col1,col2) values('1','value1','value2')";
 session.execute(query);

Reading from Cassandra

 String query="select * from test;";
 ResultSet result = session.execute(query);
 for (Row rows: result){
     System.out.println(rows.getString("key"));
 } 
abhi
  • 4,762
  • 4
  • 29
  • 49
  • I have already enabled that from false to true. Anyways, I will try to make that thing work. Do you know how can I insert into Cassandra using Datastax Java driver and then retrieve it.? Meaning my original question. – arsenal Apr 20 '13 at 04:35
  • after changing the yaml file have you restart the server? try to clean the commitlog files and start the server. Answer to original question, surely will try to provide – abhi Apr 20 '13 at 04:43
  • Can you also tell me how to clean the commit log files as well? Meaning what way we should do this in general? – arsenal Apr 20 '13 at 04:46
  • by default it is stored in '\var\lib\cassandra\commitlog\'. clean all the files in commitlog folder (use rm -fr) – abhi Apr 20 '13 at 04:53
  • Ok, that means delete it out. I am working in windows. I deleted it as well and then restarted the server and still the same thing. I believe the way I am trying to connect is not right. Can you provide an example by which you are trying to connect to Cassandra database as well? Meaning connection code. – arsenal Apr 20 '13 at 05:00
  • ,Ok. I am able to connect now. I believe, I was making that property change in wrong .yaml file. Now I just need to upsert and retrieve the data into my above column family in my question. – arsenal Apr 20 '13 at 05:06
  • It's not working for my above column family example when I try to insert. I am getting this exception- `com.datastax.driver.core.exceptions.InvalidQueryException: unconfigured columnfamily profile`. Any idea? – arsenal Apr 20 '13 at 18:54
  • have configured the columnfamily in cql3 mode?? – abhi Apr 20 '13 at 19:08
  • hmm. I don't think so whether I have done that. Some background what I have done- I have installed Cassandra 1.2.3 and created keyspace and column families there. And in `Cassandra.yaml` file I modified native.transport to true. That's all. And I started inserting data in Cassandra database using Datastax Java driver. Is there anything else I need to do to start populating it? – arsenal Apr 20 '13 at 19:10
  • how you have created ur columnfamily? your schema may be – abhi Apr 20 '13 at 19:18
  • also check ur session initiation code? it is done w.r.t which keyspace??? is it the current one? – abhi Apr 20 '13 at 19:24