hector-examples icon indicating copy to clipboard operation
hector-examples copied to clipboard

read a single row

Open monisa opened this issue 13 years ago • 2 comments

Hi am new to cassandra 0.7. could u able to clear my doubt???

This is my code for read a single row

public class Read1Column { private static StringSerializer stringSerializer = StringSerializer.get(); private static DoubleSerializer doubtSerializer = DoubleSerializer.get();
public static void main(String[] args) throws Exception {

          String keyspaceName = "User";
          String columnFamilyName ="User_Details";
          String serverAddress = "localhost:9160";
          //String username="stu1";
          String Uid="0001";
          // String FirstName="ss";
         // String LastName="kk";
         // String EMail="sksk";
         String password="ssss";      
    // Create a cluster
          Cluster cluster = HFactory.getOrCreateCluster("TestCluster", serverAddress);            
          Keyspace keyspaceOperator = HFactory.createKeyspace(keyspaceName, cluster);


            try{

                Iterator<SuperRow<String, String, String, Double>> rowIter =
                    HFactory.createRangeSuperSlicesQuery(keyspaceOperator,
                            stringSerializer, stringSerializer, stringSerializer,
                            doubtSerializer)
                                        .setColumnFamily(columnFamilyName)
                                        .setKeys(Uid,password)
                                        .setRange(Uid, password, false, 40)
                                        .execute()
                                        .get()
                                        .iterator();

                    while (rowIter.hasNext()) {

                                    SuperRow<String, String, String, Double> row =
                    rowIter.next();

                                    List<HSuperColumn<String, String, Double>> scList =
                    row.getSuperSlice().getSuperColumns();

                                    for (int i=0; i<scList.size(); i++) {
                                             HSuperColumn<String, String, Double> sc =
                    scList.get(i);
                                            // Double val = sc.get(2).getValue(); 
                            // gets the value of column 2 within the supercolumn
                                             System.out.println(sc.get(2).getValue());

                                    }
                    }


            } catch (HectorException e) {
                e.printStackTrace();
            }
          //  client.getConnectionManager().shutdown();
        }

}

its show the error:

log4j:WARN No appenders could be found for logger (me.prettyprint.cassandra.connection.CassandraHostRetryService). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" java.lang.NullPointerException: Null not allowed, number 1 at me.prettyprint.cassandra.utils.Assert.noneNull(Assert.java:25) at me.prettyprint.cassandra.model.HSuperColumnImpl.(HSuperColumnImpl.java:58) at me.prettyprint.cassandra.model.SuperSliceImpl.(SuperSliceImpl.java:35) at me.prettyprint.cassandra.model.SuperRowImpl.(SuperRowImpl.java:34) at me.prettyprint.cassandra.model.SuperRowsImpl.(SuperRowsImpl.java:35) at me.prettyprint.cassandra.model.OrderedSuperRowsImpl.(OrderedSuperRowsImpl.java:28) at me.prettyprint.cassandra.model.thrift.ThriftRangeSuperSlicesQuery$1.doInKeyspace(ThriftRangeSuperSlicesQuery.java:73) at me.prettyprint.cassandra.model.thrift.ThriftRangeSuperSlicesQuery$1.doInKeyspace(ThriftRangeSuperSlicesQuery.java:67) at me.prettyprint.cassandra.model.KeyspaceOperationCallback.doInKeyspaceAndMeasure(KeyspaceOperationCallback.java:20) at me.prettyprint.cassandra.model.ExecutingKeyspace.doExecute(ExecutingKeyspace.java:65) at me.prettyprint.cassandra.model.thrift.ThriftRangeSuperSlicesQuery.execute(ThriftRangeSuperSlicesQuery.java:66) at Read1Column.main(Read1Column.java:40)

monisa avatar May 05 '11 13:05 monisa

Strange - looks like you got a null SC back in the result list. Can you show me output from cassandra-cli of what you expect to be there?

zznate avatar May 05 '11 15:05 zznate

If i give the key(Uid) means its display the entire column from the cassandra-cli. This is my expecting output.

monisa avatar May 06 '11 06:05 monisa