1

I have created sample view in my couchbase 4.0 B version for windows. I also published my view. I am accessing it through java program, but not getting any result, but instead getting some error with json.

Here is the full code of what I am doing.

Created view:

function (doc, meta) {
   if(doc.type && doc.type == "beer") {
     emit(doc.name, doc.brewery_id);
   }
}

My java code using it:

package com.couch.base.simple;

import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;

import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.view.AsyncViewResult;
import com.couchbase.client.java.view.AsyncViewRow;
import com.couchbase.client.java.view.Stale;
import com.couchbase.client.java.view.ViewQuery;

public class Client3 {

    public static void main(String[] args) {
        // getView("_design/dev_beer", "_view/by_name");
        // getView( "_design/beer", "_view/by_name" );
        getView( "_design/beer", "by_name" );
    }

 public static ArrayList<AsyncViewRow> getView(String designDoc, String   
 view) {
    Cluster cluster = CouchbaseCluster.create();
    final Bucket bucket = cluster.openBucket("bucket-1");

    ArrayList<AsyncViewRow> result = new ArrayList<AsyncViewRow>();
    final CountDownLatch latch = new CountDownLatch(1);
    System.out.println("METHOD START");

    bucket.async()
            .query(ViewQuery.from(designDoc, view).limit(20)
                    .stale(Stale.FALSE))
            .doOnNext(new Action1<AsyncViewResult>() {
                @Override
                public void call(AsyncViewResult viewResult) {
                    if (!viewResult.success()) {
                        System.out.println(viewResult.error());
                    } else {
                        System.out.println("Query is running!");
                    }
                }
            })
            .flatMap(
                    new Func1<AsyncViewResult, Observable<AsyncViewRow>>() {
                        @Override
                        public Observable<AsyncViewRow> call(
                                AsyncViewResult viewResult) {
                            return viewResult.rows();
                        }
                    }).subscribe(new Subscriber<AsyncViewRow>() {
                @Override
                public void onCompleted() {
                    latch.countDown();
                }

                @Override
                public void onError(Throwable throwable) {
                    System.err.println("Whoops: " + throwable.getMessage());
                }

                @Override
                public void onNext(AsyncViewRow viewRow) {
                    result.add(viewRow);
                }
            });
    try {
        latch.await();
        } catch (InterruptedException e) {
        e.printStackTrace();
     }`enter code here`
     return result;
  }
}

Error in console while running this program:

INFO: CoreEnvironment: {sslEnabled=false, sslKeystoreFile='null',     
sslKeystorePassword='null', queryEnabled=false, queryPort=8093,    
bootstrapHttpEnabled=true, bootstrapCarrierEnabled=true, boot
strapHttpDirectPort=8091, bootstrapHttpSslPort=18091, boot
strapCarrierDirectPort=11210, bootstrapCarrierSslPort=11207, ioPoolSize=4,    
computationPoolSize=4, responseBufferSize=16384, requestBufferSize=16384,  
kvServiceEndpoints=1, viewServiceEndpoints=1, queryServiceEndpoints=1, 
ioPool=NioEventLoopGroup, coreScheduler=CoreScheduler, 
eventBus=DefaultEventBus, packageNameAndVersion=couchbase-java-client/2.1.0 
(git: 2.1.0), dcpEnabled=false, retryStrategy=BestEffort, 
maxRequestLifetime=75000, 
retryDelay=com.couchbase.client.core.time.ExponentialDelay@27ddd392, 
reconnectDelay=com.couchbase.client.core.time.ExponentialDelay@19e1023e, 

Jul 31, 2015 10:17:11 AM com.couchbase.client.core.node.CouchbaseNode$5 call   
 INFO: Connected to Node 127.0.0.1
 Jul 31, 2015 10:17:11 AM     
 com.couchbase.client.core.config.DefaultConfigurationProvider$6 call
INFO: Opened bucket bucket-1
 METHOD START
 Jul 31, 2015 10:17:12 AM com.couchbase.client.java.view.ViewRetryHandler    
 shouldRetry
 INFO: Received a View HTTP response code (400) I did not expect, not   
  retrying.

 {"error":"bad_request","reason":"attachments not supported in Couchbase"}
mikewied
  • 5,273
  • 1
  • 20
  • 32

2 Answers2

1

Based on this ( https://forums.couchbase.com/t/use-views-with-the-java-client/2679/3 ) I'm going to guess your problem is the name of the design-doc:

it should probably not be "_design/beer" but just "beer".

FuzzyAmi
  • 7,543
  • 6
  • 45
  • 79
  • I tried it too, (tried with all possible combinations of design name and view name. but not got suceeded in running it. – Kartik Agrawal Aug 03 '15 at 09:30
  • Have you published your views? – FuzzyAmi Aug 03 '15 at 09:38
  • I confirm that the OP code had 2 errors, the one that is marked as answer and this one. So both corrections needs to be made to the code for it to work properly. If you came across this post, check both points. – Mehdi LAMRANI Jul 20 '17 at 18:31
0

I was doing very silly mistake, I was creating the views on beer-sample bucket, but in code I was doing query on bucket-1 bucket, so was getting error.

So please be sure that you are querying the same bucket on which the views are created. Thanks for all your support.