我是卡桑德拉的新手。我正在使用 Astyanax 的 ReverseIndexQuery 按范围获取数据。我能够将数据插入 USER 和 USER_INDEX 列族。但是在使用 ReverseIndexQuery 获取数据时,出现以下错误。
com.netflix.astyanax.connectionpool.exceptions.BadRequestException:BadRequestException:[主机=本地主机(127.0.0.1):9160,延迟=16(16),尝试= 1]InvalidRequestException(为什么:字符串没有验证。)
这是我的代码和列族定义:
import com.netflix.astyanax.AstyanaxContext;
import com.netflix.astyanax.Keyspace;
import com.netflix.astyanax.MutationBatch;
import com.netflix.astyanax.annotations.Component;
import com.netflix.astyanax.connectionpool.NodeDiscoveryType;
import com.netflix.astyanax.connectionpool.OperationResult;
import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
import com.netflix.astyanax.connectionpool.impl.ConnectionPoolConfigurationImpl;
import com.netflix.astyanax.connectionpool.impl.CountingConnectionPoolMonitor;
import com.netflix.astyanax.impl.AstyanaxConfigurationImpl;
import com.netflix.astyanax.model.ColumnFamily;
import com.netflix.astyanax.model.Row;
import com.netflix.astyanax.serializers.LongSerializer;
import com.netflix.astyanax.recipes.Shards;
import com.netflix.astyanax.recipes.ReverseIndexQuery;
import com.netflix.astyanax.serializers.StringSerializer;
import com.netflix.astyanax.thrift.ThriftFamilyFactory;
import com.google.common.base.Function;
import java.util.Arrays;
import java.util.Iterator;
public class AstyanaxTest {
/**
* @param args
*/
private static class IndexEntry {
@Override
public boolean equals(Object o) {
IndexEntry ie = (IndexEntry) o;
return (this.key == ie.key) && (this.value == ie.value);
}
@Override
public int hashCode() {
System.out.println("inside hashcode");
return key.hashCode() * value.hashCode();
}
@Override
public String toString() {
return value + ":" + key;
}
@Component(ordinal = 0)
Long value;
@Component(ordinal = 1)
Long key;
public IndexEntry(Long value, Long key) {
this.value = value;
this.key = key;
}
}
public static void main(String[] args) {
AstyanaxContext<Keyspace> ctx = new AstyanaxContext.Builder()
.forKeyspace("LOG_TRACER").withAstyanaxConfiguration(
new AstyanaxConfigurationImpl()
.setDiscoveryType(NodeDiscoveryType.NONE))
.withConnectionPoolConfiguration(
new ConnectionPoolConfigurationImpl("ConnPool")
.setPort(9160).setMaxConnsPerHost(1)
.setMaxBlockedThreadsPerHost(1).setSeeds(
"localhost" + ":" + 9160)
.setConnectTimeout(10000))
.withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
.buildKeyspace(ThriftFamilyFactory.getInstance());
ctx.start();
Keyspace keyspace = ctx.getEntity();
ColumnFamily<Long, String> usercf = ColumnFamily
.<Long, String> newColumnFamily("USER", LongSerializer.get(),
StringSerializer.get());
ColumnFamily<String, String> userindexcf = ColumnFamily
.newColumnFamily("USER_INDEX", StringSerializer.get(),
StringSerializer.get());
MutationBatch batch = keyspace.prepareMutationBatch();
long age = 10;
for (long row = 0; row < 1000; row++) {
long value = age + row;
batch.withRow(usercf, row).putColumn("FIRST_NAME",
"Prafulla" + row, null).putColumn("LAST_NAME",
"Pradhan" + row).putColumn("AGE", value + "");
batch.withRow(userindexcf, "AGE_" + (row % 11)).putColumn(
new IndexEntry(value, row).toString(), row + "", null);
System.out
.println("value " + new IndexEntry(value, row).toString());
}
try {
OperationResult<Void> result = batch.execute();
System.out.println("Inserted..." + result.getAttemptsCount());
} catch (ConnectionException e) {
System.out.println("error " + e.getMessage());
}
// Fetch using range of AGE
ReverseIndexQuery.newQuery(keyspace, usercf, userindexcf.getName(),
LongSerializer.get()).fromIndexValue(10L).toIndexValue(100L)
.withIndexShards(
new Shards.StringShardBuilder().setPrefix("AGE_")
.setShardCount(11).build()).withColumnSlice(
Arrays
.asList(new String[] { "FIRST_NAME",
"LAST_NAME" })).forEach(
new Function<Row<Long, String>, Void>() {
public Void apply(Row<Long, String> eachRow) {
System.out.println("Key "
+ eachRow.getRawKey());
for (Iterator iterator = eachRow.getColumns()
.iterator(); iterator.hasNext();) {
}
return null;
}
}).execute();
}
}
CREATE COLUMN FAMILY USER WITH comparator=UTF8Type and key_validation_class=LongType
CREATE COLUMN FAMILY USER_INDEX WITH comparator=UTF8Type and key_validation_class=UTF8Type
提前致谢!!!