javacassandrapelops

Pelops Java Client to insert into Cassandra Database


I recently started working with Cassandra database. I was able to setup single node cluster in my local machine.

And now I was thinking to start writing some sample data to Cassandra Database using Pelops client.

Below is the keyspace and column family I have created so far-

create keyspace my_keyspace with placement_strategy = 'org.apache.cassandra.locator.SimpleStrategy' and strategy_options = {replication_factor:1};
use my_keyspace;
create column family users with column_type = 'Standard' and comparator = 'UTF8Type';

Below is the code that I have so far. I made some progress as before I was getting some exception which I was able to fix it. Now I am getting another exception

public class MyPelops {
    private static final Logger log = Logger.getLogger(MyPelops.class);

    public static void main(String[] args) throws Exception {

        // A comma separated List of Nodes
        String NODES = "localhost";
        // Thrift Connection Pool
        String THRIFT_CONNECTION_POOL = "Test Cluster";
        // Keyspace
        String KEYSPACE = "my_keyspace";
        // Column Family
        String COLUMN_FAMILY = "users";
        Cluster cluster = new Cluster(NODES, 9160);
        Pelops.addPool(THRIFT_CONNECTION_POOL, cluster, KEYSPACE);
        Mutator mutator = Pelops.createMutator(THRIFT_CONNECTION_POOL);
        log.info("- Write Column -");
        mutator.writeColumn(
                COLUMN_FAMILY,
                "Row1",
                new Column().setName(" Name ".getBytes()).setValue(
                        " Test One ".getBytes()));
        mutator.writeColumn(
                COLUMN_FAMILY,
                "Row1",
                new Column().setName(" Work ".getBytes()).setValue(
                        " Engineer ".getBytes()));
        log.info("- Execute -");
        mutator.execute(ConsistencyLevel.ONE);
        Selector selector = Pelops.createSelector(THRIFT_CONNECTION_POOL);
        int columnCount = selector.getColumnCount(COLUMN_FAMILY, "Row1",
                ConsistencyLevel.ONE);
        log.info("- Column Count = " + columnCount);
        List<Column> columnList = selector
                .getColumnsFromRow(COLUMN_FAMILY, "Row1",
                        Selector.newColumnsPredicateAll(true, 10),
                        ConsistencyLevel.ONE);
        log.info("- Size of Column List = " + columnList.size());
        for (Column column : columnList) {
            log.info("- Column: (" + new String(column.getName()) + ","
                    + new String(column.getValue()) + ")");
        }
        log.info("- All Done. Exit -");
        System.exit(0);
    }
}

Whenever I am running this program, I am getting this exception-

Exception in thread "main" org.scale7.cassandra.pelops.exceptions.InvalidRequestException: Column timestamp is required

And this exception is coming as soon as it tries to execute this line-

mutator.execute line

As I mentioned above, I am new to Cassandra Database and Pelops client as well. This is my first time working with that. Can anyone help me with this problem with step by step process? I am running Cassandra 1.2.3 in my local box.

Any step by step guidance like how to insert data in Cassandra database will help me a lot in understanding how Cassandra works.

Thanks in advance.


Solution

  • Each cassandra column is a Key-Value-Timestamp triplet. You didn't set the timestamp in yours columns

    Column c = new Column();
    c.setTimestamp(System.currentTimeMillis());
    

    You can use the client way to create the column and make the job easier

       mutator.writeColumn(
                COLUMN_FAMILY,
                "Row1",
                mutator.newColumn(" Name ", " Test One "));
    

    In this way you can avoid both setting the timestamp (the client will do it for you) and using getBytes() on String.

    Regards, Carlo