How to use ViewRow to get the size of the total documents in Couchbase SDK3

Hello,

With Couchbase SDK2, the ViewRow could be used as below to fetch the total docoment list size.

        ViewQuery countQuery = ViewQuery
                        .from("status", "updated_datetime")
                        .stale(Stale.FALSE)
                        .startKey(createDatetime.getTime());
        /**
         * Execute count view
         */
        List<ViewRow> countView = bucket.query(countQuery).allRows();

        
        int totalItemCnt = (int) countView.get(0).value();

In SDK 3, i have tried to change the code as below

        List<ViewRow> countView = bucket.viewQuery(
                        "status",
                        "updated_datetime",
                        ViewOptions
                                .viewOptions()
                                .scanConsistency(ViewScanConsistency.REQUEST_PLUS).startKey(createDatetime.getTime()))
                .rows();

int totalItemCnt = (int) countView.get(0).value();

However, I am getting compilation error as below:

Cannot resolve method ‘value’ in ‘ViewRow’. How can i fix this in Couchbase SDK3 ?

It is now called valueAs and allows you to specify the value type instead of just returning an object → ViewRow (Couchbase Java SDK 3.3.1 API)

So , we if we need to change it to integer , then the expected format should be like this ?

    Optional<Integer> totalItemCnt = countView.get(0).valueAs(Integer.class);

I don’t know how the result looks like in your case specifically but it looks right I think.

1 Like