使用ReverseIndexQuery时获取BadRequestException

时间:2012-12-21 15:03:34

标签: cassandra astyanax

我是Cassandra的新手。我正在使用Astyanax的ReverseIndexQuery来按范围获取数据。我可以将数据插入USER和USER_INDEX列族。但是在使用ReverseIndexQuery获取数据时,低于错误。

com.netflix.astyanax.connectionpool.exceptions.BadRequestException:BadRequestException:[host = localhost(127.0.0.1):9160,latency = 16(16),attempts = 1] InvalidRequestException(why:String未验证。 )

这是我的代码和列系列定义:

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 

提前致谢!!!

1 个答案:

答案 0 :(得分:0)

我常常在混淆数据类型时看到这一点。当需要字符串时,您是否将原始字节发送到列?