gpt4 book ai didi

java - Cassandra 错误: Column timestamp is required

转载 作者:行者123 更新时间:2023-12-02 07:01:47 26 4
gpt4 key购买 nike

我正在用 Java 学习 Cassandra,并且我尝试运行本书中的 Hotel 示例 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)

你能帮我解决这个问题吗?非常感谢。
以下是我的项目的详细信息:

  1. 要使用的库:cassandra-all-1.2.4 和 cassandra-thrift-1.2.4
  2. 数据库:我使用了 apache-cassandra-1.2.3 并在我的计算机上安装了 DataStax OpsCenter。我按照以下说明在 OpsCenter 上手动创建了键空间“hotelier”:

    键空间:

    • 姓名:酒店老板replica_placement_strategy:org.apache.cassandra.locator.RackUnawareStrategy复制因子:1列族:
    • 名称:酒店Compare_with:UTF8类型
    • 名称:城市酒店Compare_with:UTF8类型
    • 姓名:客人Compare_with:字节类型
    • 名称:预订Compare_with:TimeUUIDType
    • 名称:兴趣点列类型: super 与:UTF8类型比较Compare_subcolumns_with:UTF8类型
    • 名称:房间列类型: super 比较_与:字节类型Compare_subcolumns_with:字节类型
    • 名称:空房情况列类型: super 比较_与:字节类型Compare_subcolumns_with:字节类型
  3. 这是我的代码:

连接器类别:

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());
}
}

最佳答案

而不是使用cambriaNameCol.timestamp = timestamp;

使用cambriaNameCol.setTimestamp(timestamp);

这是从 0.7 版本开始升级的一种实现时间戳的简洁方法。

还有一件事,为什么要遵循节俭?那里有很多简单的 API。

关于java - Cassandra 错误: Column timestamp is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16570156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com