deewreck
deewreck

Reputation: 133

Couchbase view pagination with java client

I am trying to pull records from a view which emits in following way

DefaultViewRow{id=0329a6ac-84cb-403e-9d1d, key=[“X”,“Y”,“0329a6ac-84cb-403e-9d1d”,“31816552700”], value=1}

As we have millions of record, we are trying to implement pagination to pull 500 records per page and do some process then get next 500 records i have implemented following code with java client

def cluster = CouchbaseCluster.create(host)
def placesBucket = cluster.openBucket("pass", "pass")
def startKey = JsonArray.from(X, "Y")
def endKey = JsonArray.from(X, "Y", new JsonObject())
hasRow = true
rowPerPage = 500
page = 0
currentStartkey=""
startDocId=""
def viewResult
def counter = 0
while (hasRow) {
    hasRow = false
   def skip = page == 0 ?0: 1
    page = page + 1
     viewResult = placesBucket.query(ViewQuery.from("design", "view")
            .startKey(startKey)
             .endKey(endKey)
            .reduce(false)
            .inclusiveEnd()
            .limit(rowPerPage)
            .stale(Stale.FALSE)
            .skip(skip).startKeyDocId(startDocId)
    )
    def runResult = viewResult.allRows()
    for(ViewRow row: runResult){
        hasRow = true
        println(row)
        counter++
        startDocId = row.document().id()
    }
    println("Page NUMBER "+ page)
}
println("total "+ counter)

Post execution, i am getting few repetitive rows and even though the total records is around 1000 for particular small scenario i get around 3000+ rows in response and it keeps going. can someone please tell me if i am doing something wrong ?? PS: My start key value will be same for each run as i am trying to get each unique doc _id. please help.

Upvotes: 1

Views: 123

Answers (0)

Related Questions