Question

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.

Était-ce utile?

La solution

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"));
 } 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top