私は Java で Cassandra を学んでおり、この本のhttp://shop.oreilly.com/product/0636920010852.do (Cassandra: The Definitive Guide) でホテルのサンプルを実行しようとしました。ただし、このサンプルは古いバージョンのライブラリ (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」を手動で作成しました。次の説明に従ってください。
キースペース:
- 名前: Hotelier replica_placement_strategy: org.apache.cassandra.locator.RackUnawareStrategy replication_factor: 1 column_families:
- 名前: ホテル 比較対象: UTF8Type
- 名前: HotelByCity 比較対象: UTF8Type
- name: ゲスト compare_with: BytesType
- name: 予約 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
- これが私のコードです:
コネクタ クラス:
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());
}
}