Java driver simple named columns

opuneet edited this page Jan 11, 2014 · 1 revision
Clone this wiki locally

See Java-driver-direct-cql3 as reference for the schema that is being used for this example.

Assume that we have this row in the table

Basic Setup

ColumnFamily<Integer, String> CF_EMPLOYEES = 
			new ColumnFamily<Integer, String>("EmployeeInfo", 
							  IntegerSerializer.get(), 
                                                          StringSerializer.get());

Insert Query

MutationBatch m = keyspace.prepareMutationBatch();
		
m.withRow(CF_EMPLOYEES, 1)
	 .putColumn("fname", "Joe")
	 .putColumn("fname", "Smith")
	 .putColumn("age", 35)
	 .putColumn("salary", 10000);
		 
m.execute();

Read Single Row

ColumnList<String> columns = keyspace.prepareQuery(CF_EMPLOYEES)
		        .getRow(1)
		        .execute()
		        .getResult();
		
System.out.println("FirstName: " + columns.getColumnByName("fname").getStringValue());
System.out.println("LastName:  " + columns.getColumnByName("lname").getStringValue());
System.out.println("Age:       " + columns.getColumnByName("age").getIntegerValue());
System.out.println("Salary:    " + columns.getColumnByName("salary").getLongValue());

Read Single row with select columns

columns = keyspace.prepareQuery(CF_EMPLOYEES)
		        .getRow(1)
		        .withColumnSlice("fname", "age", "salary")
		        .execute()
		        .getResult();

Row slice query - multiple row keys

Rows<Integer, String> rows = keyspace.prepareQuery(CF_EMPLOYEES)
				.getRowSlice(1,3,5)
				.execute()
				.getResult();

Row Slice query with specific columns

rows = keyspace.prepareQuery(CF_EMPLOYEES)
			.getRowSlice(1,3,5)
		        .withColumnSlice("fname", "age", "salary")
			.execute()
			.getResult();

Note that column range queries are not applicable to this use case.