Upsert/Read into/from Cassandra database using Datastax API (using new Binary protocol)
Asked Answered
J

1

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.

Jalisajalisco answered 19/4, 2013 at 2:23 Comment(6)
are you able to connect or its showing any error?Matronly
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?Jalisajalisco
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.?Matronly
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.Jalisajalisco
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.Jalisajalisco
@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?Jalisajalisco
M
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"));
 } 
Matronly answered 20/4, 2013 at 4:34 Comment(11)
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.Jalisajalisco
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 provideMatronly
Can you also tell me how to clean the commit log files as well? Meaning what way we should do this in general?Jalisajalisco
by default it is stored in '\var\lib\cassandra\commitlog\'. clean all the files in commitlog folder (use rm -fr)Matronly
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.Jalisajalisco
,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.Jalisajalisco
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?Jalisajalisco
have configured the columnfamily in cql3 mode??Matronly
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?Jalisajalisco
how you have created ur columnfamily? your schema may beMatronly
also check ur session initiation code? it is done w.r.t which keyspace??? is it the current one?Matronly

© 2022 - 2024 — McMap. All rights reserved.