Inserting Data into Cassandra using hector

1.3k views Asked by At

I'm trying to insert data into cassandra data base using hector api. The code that I have used is displayed below.

<i>
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;

import java.util.UUID;

public class HectorAPI {
    private static final String USER="data";
    public static void main(String[] args) throws Exception{
        StringSerializer ss = StringSerializer.get();
        Cluster cluster = HFactory.getOrCreateCluster("Test Cluster", "127.0.0.1:9160");
        Keyspace keyspace = HFactory.createKeyspace("myKeySpace", cluster);
        String id = UUID.randomUUID().toString();

        Mutator<String> mutator = HFactory.createMutator(keyspace, ss);
        mutator.addInsertion(id, USER, HFactory.createStringColumn("full_name", "John"))
                .addInsertion(id, USER, HFactory.createStringColumn("email", "test"))
                .addInsertion(id, USER, HFactory.createStringColumn("state","yfcy"))
                .addInsertion(id, USER, HFactory.createStringColumn("gender", "male"))
                .addInsertion(id, USER, HFactory.createStringColumn("birth_year", "1990"));
        mutator.execute();

    }

}

But there cannot be found any inserted data in /var/lib/cassandra/data folder under the given KeySpace. It seems like data insertion does not work properly. What can be the issue with the code. The command that I have used to create 'data' column family is displayed below.

CREATE COLUMN FAMILY data
WITH comparator = UTF8Type
AND key_validation_class=UTF8Type
AND column_metadata = [
{column_name: full_name, validation_class: UTF8Type}
{column_name: email, validation_class: UTF8Type}
{column_name: state, validation_class:  UTF8Type, index_type: KEYS}
{column_name: gender, validation_class: UTF8Type}
{column_name: birth_year, validation_class: UTF8Type, index_type: KEYS}
];

1

There are 1 answers

0
Mahdi Dolati On

I executed your code on my machine and it worked fine; Use cassandra-cli tool and execute the command

get data where birth_year = 1990;

or use cqlsh and execute the command

select * from data where birth_year = '1990';

to see your data has been insrted into the column family...

And you can see your data in cassandra's commitlog, in "var\lib\cassandra\commitlog" directory...

Search for your data (for example keyword "full_name") and you'll find it.