I am learning Cassandra with Java, and I've tried to run the Hotel sample in this book http://shop.oreilly.com/product/0636920010852.do (Cassandra: The Definitive Guide). But because this sample used some libraries with obsolete version (cassandra-0.7.0, libthrift-0.9.0) and it's impossible to compile it; so I've modified it by switching to latest version (cassandra-all-1.2.4 and cassandra-thrift-1.2.4) and it is compiled successfully. But I cannot run it to enjoy the result. This error occurs:
Exception in thread "main" InvalidRequestException(why:Column timestamp is required)
at org.apache.cassandra.thrift.Cassandra$batch_mutate_result.read(Cassandra.java:20833)
at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:78)
at org.apache.cassandra.thrift.Cassandra$Client.recv_batch_mutate(Cassandra.java:964)
at org.apache.cassandra.thrift.Cassandra$Client.batch_mutate(Cassandra.java:950)
at com.cassandraguide.hotel.MainApplication.insertAllHotels(MainApplication.java:41)
at com.cassandraguide.hotel.MainApplication.main(MainApplication.java:28)
Could you please help me solve this? Thank you so much.
Here are my project's details:
- Libraries to use: cassandra-all-1.2.4 and cassandra-thrift-1.2.4
Database: I used apache-cassandra-1.2.3 and also installed DataStax OpsCenter on my machine. I created the keyspace "hotelier" manually on OpsCenter follow this description:
keyspaces:
- name: Hotelier replica_placement_strategy: org.apache.cassandra.locator.RackUnawareStrategy replication_factor: 1 column_families:
- name: Hotel compare_with: UTF8Type
- name: HotelByCity compare_with: UTF8Type
- name: Guest compare_with: BytesType
- name: Reservation compare_with: TimeUUIDType
- name: PointOfInterest column_type: Super compare_with: UTF8Type compare_subcolumns_with: UTF8Type
- name: Room column_type: Super compare_with: BytesType compare_subcolumns_with: BytesType
- name: RoomAvailability column_type: Super compare_with: BytesType compare_subcolumns_with: BytesType
- Here is my codes:
Connector class:
package com.cassandraguide.hotel;
import static com.cassandraguide.hotel.Constants.KEYSPACE;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
//simple convenience class to wrap connections, just to reduce repeat code
public class Connector {
TTransport tr = new TSocket("localhost", 9160);
// returns a new connection to our keyspace
public Cassandra.Client connect() throws TTransportException, TException, InvalidRequestException {
TFramedTransport tf = new TFramedTransport(tr);
TProtocol proto = new TBinaryProtocol(tf);
Cassandra.Client client = new Cassandra.Client(proto);
tr.open();
client.set_keyspace(KEYSPACE);
return client;
}
public void close() {
tr.close();
}
}
MainApplication class
package com.cassandraguide.hotel;
import static com.cassandraguide.hotel.Constants.CL;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.Mutation;
public class MainApplication {
private static Cassandra.Client client;
private static Connector connector;
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
connector = new Connector();
client = connector.connect();
insertAllHotels();
}
public static void insertAllHotels() throws Exception {
// Try to put some dummy data
String columnFamily = "Hotel";
// row keys
String cambriaKey = "AZC_043";
// conveniences
Map<ByteBuffer, Map<String, List<Mutation>>> cambriaMutationMap = createCambriaMutation(columnFamily, cambriaKey);
client.batch_mutate(cambriaMutationMap, CL);
}
private static Map<ByteBuffer, Map<String, List<Mutation>>> createCambriaMutation(String columnFamily, String cambriaKey)
throws UnsupportedEncodingException {
// set up columns for Cambria
long timestamp = System.currentTimeMillis();
Column cambriaNameCol = new Column(getByteBuf("name"));
cambriaNameCol.value = getByteBuf("Cambria Suites Hayden");
cambriaNameCol.timestamp = timestamp;
Column cambriaPhoneCol = new Column(getByteBuf("phone"));
cambriaPhoneCol.value = getByteBuf("480-444-4444");
cambriaPhoneCol.timestamp = timestamp;
Column cambriaAddressCol = new Column(getByteBuf("address"));
cambriaAddressCol.value = getByteBuf("400 N. Hayden");
cambriaAddressCol.timestamp = timestamp;
Column cambriaCityCol = new Column(getByteBuf("city"));
cambriaCityCol.value = getByteBuf("Scottsdale");
cambriaCityCol.timestamp = timestamp;
Column cambriaStateCol = new Column(getByteBuf("state"));
cambriaStateCol.value = getByteBuf("AZ");
cambriaStateCol.timestamp = timestamp;
Column cambriaZipCol = new Column(getByteBuf("zip"));
cambriaZipCol.value = getByteBuf("85255");
cambriaZipCol.timestamp = timestamp;
ColumnOrSuperColumn nameCosc = new ColumnOrSuperColumn();
nameCosc.column = cambriaNameCol;
ColumnOrSuperColumn phoneCosc = new ColumnOrSuperColumn();
phoneCosc.column = cambriaPhoneCol;
ColumnOrSuperColumn addressCosc = new ColumnOrSuperColumn();
addressCosc.column = cambriaAddressCol;
ColumnOrSuperColumn cityCosc = new ColumnOrSuperColumn();
cityCosc.column = cambriaCityCol;
ColumnOrSuperColumn stateCosc = new ColumnOrSuperColumn();
stateCosc.column = cambriaStateCol;
ColumnOrSuperColumn zipCosc = new ColumnOrSuperColumn();
zipCosc.column = cambriaZipCol;
Mutation nameMut = new Mutation();
nameMut.column_or_supercolumn = nameCosc;
Mutation phoneMut = new Mutation();
phoneMut.column_or_supercolumn = phoneCosc;
Mutation addressMut = new Mutation();
addressMut.column_or_supercolumn = addressCosc;
Mutation cityMut = new Mutation();
cityMut.column_or_supercolumn = cityCosc;
Mutation stateMut = new Mutation();
stateMut.column_or_supercolumn = stateCosc;
Mutation zipMut = new Mutation();
zipMut.column_or_supercolumn = zipCosc;
// set up the batch
Map<ByteBuffer, Map<String, List<Mutation>>> cambriaMutationMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>();
Map<String, List<Mutation>> cambriaMuts = new HashMap<String, List<Mutation>>();
List<Mutation> cambriaCols = new ArrayList<Mutation>();
cambriaCols.add(nameMut);
cambriaCols.add(phoneMut);
cambriaCols.add(addressMut);
cambriaCols.add(cityMut);
cambriaCols.add(stateMut);
cambriaCols.add(zipMut);
cambriaMuts.put(columnFamily, cambriaCols);
// outer map key is a row key
// inner map key is the column family name
cambriaMutationMap.put(getByteBuf(cambriaKey), cambriaMuts);
return cambriaMutationMap;
}
private static ByteBuffer getByteBuf(String str) {
return ByteBuffer.wrap(str.getBytes());
}
}