我正在用java学习cassandra,我试着运行这本书中的酒店示例http://shop.oreilly.com/product/0636920010852.do (Cassandra:权威指南)。但是因为这个示例使用了一些过时版本的库(cassandra-0.7.0,libstrift-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\u放置\u策略:org.apache.cassandra.locator.rackunawarestegy replication\u因子:1列\u族:
名称:酒店比较类型:utf8type
名称:hotelbycity compare\u with:utf8type
名称:guest compare\u与:bytestype
名称:保留比较对象:timeuuidtype
名称:兴趣点列类型:超级比较类型:utf8type比较子列类型:utf8type
名称:文件室列类型:超级比较对象:ByTestType比较子列类型:ByTestType
名称:roomavailability列类型:超级比较类型:ByTestType比较子列类型:ByTestType
这是我的密码:
连接器等级:
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();
}
}
主应用程序类
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());
}
}
1条答案
按热度按时间unftdfkk1#
而不是使用
cambriaNameCol.timestamp = timestamp;
使用cambriaNameCol.setTimestamp(timestamp);
这是一种实现自0.7版本以来升级的时间戳的省钱方法。还有一件事,你为什么要节俭?有那么多简单的api。