I am new to Cassandra. I'm using Astyanax's ReverseIndexQuery to fetch data by range. I'm able to insert data into USER and USER_INDEX column family. But while fetch data using ReverseIndexQuery, getting below error.
com.netflix.astyanax.connectionpool.exceptions.BadRequestException: BadRequestException: [host=localhost(127.0.0.1):9160, latency=16(16), attempts=1]InvalidRequestException(why:String didn't validate.)
Here is my code and column family definations:
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
Thanks in advance!!!