Inserting data into Apache Cassandra using Java

We already discussed the fundamentals of Apache Cassandra. In the Just previous chapter , we have discussed the configuration of Apache Cassandra in local machine and configuration of work space. In this chapter we are   discussing how data is inserting into Apache Cassandra using Java with example.We  are using the same key space ‘USERKEYSPACE‘ created while configuring Apache Cassandra in local machine.Also Cassandra should be started as explained in the previous chapter before running  our Java application.

Inserting  data into Apache  Cassandra using Java

Assume we need to create a column family to insert our data. We are naming our row key as ‘names‘. We have to insert three names in this column family.The details of the particular column family  with required data is shown below as a schematic.We will be creating this column family with name ‘userColumnFamily‘ in our Java application.

Now let us see the Java code.

Steps Involved to insert data to Cassandra  are:

1)Create or get the required Cassandra cluster.

2)Create or get the keyspace ‘USERKEYSPACE‘ .

3)Create column family object and add the column family to cluster.

4)Create ‘StringSerializer‘ and ‘Mutator‘ objects.

5)Insert the data using ‘Mutator’ object with required row key and column names .

InsertSample.java

import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import me.prettyprint.hector.api.ddl.ComparatorType;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;

public class InsertSample {
Cluster cluster = null;
Keyspace keySpace =null;

public InsertSample() {

}

public void insert() {
cluster = HFactory.getOrCreateCluster("Test Cluster", "localHost:9160");
keySpace = HFactory.createKeyspace("USERKEYSPACE", cluster);
ColumnFamilyDefinition cf = HFactory.createColumnFamilyDefinition("USERKEYSPACE","userColumnFamily",ComparatorType.UTF8TYPE);
cluster.addColumnFamily(cf);
StringSerializer stringSerializer = StringSerializer.get();
Mutator mutator = HFactory.createMutator(keySpace, stringSerializer);
mutator.insert("names", cf.getName(), HFactory.createStringColumn("1", "Bijoy"));
mutator.insert("names", cf.getName(), HFactory.createStringColumn("2", "Karthik"));
mutator.insert("names", cf.getName(), HFactory.createStringColumn("3", "JayaKrishnan"));
System.out.println("Done..");

}

public static void main(String[] args) {
InsertSample sample = new InsertSample();
sample.insert();
}

}

Output

Compile and run the  application . When data is inserted the ‘Done’ message will be displayed without any error messages .We can retrieve the same piece of data to confirm the result. It is explained in next chapter:Reading data from Apache Cassandra using Java.

See Related

Apache Cassandra Tutorial

Configuring Apache Cassandra in local machine & Configuring Java work space

Reading data from Apache Cassandra using Java

Listing columns in Apache Cassandra using Hector API

Deleting column from Apache Cassandra using Java

Inserting objects into Apache Cassandra using Hector API

Reading object data from Apache Cassandra using Hector API