我正在使用 Java 学习 Cassandra,并且我尝试运行本书http://shop.oreilly.com/product/0636920010852.do(Cassandra:权威指南)中的酒店示例。但是因为这个示例使用了一些版本过时的库(cassandra-0.7.0,libthrift-0.9.0),所以无法编译它;所以我通过切换到最新版本(cassandra-all-1.2.4 和 cassandra-thrift-1.2.4)对其进行了修改,并且编译成功。但我不能运行它来享受结果。发生此错误:
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)
你能帮我解决这个问题吗?非常感谢。
这是我的项目的详细信息:
- 要使用的库:cassandra-all-1.2.4 和 cassandra-thrift-1.2.4
数据库:我使用了 apache-cassandra-1.2.3 并且还在我的机器上安装了 DataStax OpsCenter。我在 OpsCenter 上手动创建了键空间“hotelier”,遵循以下描述:
键空间:
- 名称:Hotelierreplica_placement_strategy:org.apache.cassandra.locator.RackUnawareStrategy replication_factor:1 column_families:
- 名称:酒店 compare_with:UTF8Type
- 名称:HotelByCity compare_with:UTF8Type
- 名称:访客 compare_with:BytesType
- 名称:预订 compare_with:TimeUUIDType
- 名称:PointOfInterest column_type:超级 compare_with:UTF8Type compare_subcolumns_with:UTF8Type
- 名称:房间 column_type:超级 compare_with:BytesType compare_subcolumns_with:BytesType
- 名称:RoomAvailability column_type:超级 compare_with:BytesType compare_subcolumns_with:BytesType
- 这是我的代码:
连接器类:
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 类
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());
}
}