Search code examples
cassandrahector

How can I get a value of a countercolumn out of cassandra with hector?


Hello I'm trying to obtain data from cassandra using hector. There are two ways of doing this.

one is with a cqlQuery like this:

CqlQuery<String, String, Long> cqlQuery = new CqlQuery<String, String, Long>(connect.tutorialKeyspace, stringSerializer, stringSerializer, longSerializer);
    cqlQuery.setQuery("select home from page_view_counts where KEY ="localhost");

    QueryResult<CqlRows<String, String, Long>> result;
    try
    {
        result = cqlQuery.execute();
    }
    catch(HectorException e){
        result = null;
        message = e.getMessage();
    }

And without queries it should be something like this, but I can't get it to work:

SliceQuery<String, String, Long> query = HFactory.createSliceQuery(connect.tutorialKeyspace, stringSerializer,stringSerializer,longSerializer); 
    QueryResult<ColumnSlice<String, Long>> result2 = query.setColumnFamily("page_view_counts").setKey("localhost").setColumnNames("home").execute();

Where do I make the mistake?


Solution

  • Your examples are incomplete, you give no indication of what is failing. But from looking at your code, there are a few things wrong: incorrect types, arguments, you need to use specific Counter types, and if just getting a single column then don't need Slice. This example should work:

    CounterQuery<String, String> query = HFactory.createCounterColumnQuery(keyspace, stringSerializer,stringSerializer);
    query.setColumnFamily("page_view_counts").setKey("localhost").setName("home");
    HCounterColumn<String> counter = query.execute().get();
    System.out.println("Count:" + counter.getValue());
    

    If you do need a Slice, then the result returned from HFactory.createSliceCounterSliceQuery() will be

    QueryResult<CounterSlice<String>>
    

    which will contain the List of

    HCounterColumn<String>