Use Views with the JAVA client


#1

Hi!
I’m using the following method to get the documents of a view:

public static ArrayList<AsyncViewRow> getView(String designDoc, String view) {
    final 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("ERROR" + 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();
    }
    return result;
}

However, if I use the folllowing line:

ArrayList<AsyncViewRow> returnItem = ConnectionManager.getView("_design/design_events/_view", "events");

I have an error:
INFO: Received a View HTTP response code (400) I did not expect, not retrying.
ERROR{“error”:“bad_request”,“reason”:“attachments not supported in Couchbase”}

And If I use the following line:

ArrayList<AsyncViewRow> returnItem = ConnectionManager.getView("design_events", "events");

I had a different error:
Whoops: View design_events/events does not exist.

How I have to define the document?

In the CouchBase server, I see the view with the following path:

  • _design/design_events/_view/events*

Thanks,
Carlos


#2

@cfontana0 so the first snippet works, but the rest doesn’t?

The “ConnectionManager” is not part of the SDK, can you share what it does? Also, in general in the SDK you never need to give it the full name, but rather only the name of the design doc and the name of the view.


#3

Hi @daschl, sorry about that.
This is the code of my project:

public class Main {

	private static CouchbaseCluster cluster;
	private static Bucket bucket;	
	
	public static void main(String[] args) {
		cluster=CouchbaseCluster.create("qa....com");
		ClusterManager cManager = cluster.clusterManager("Administrator", "..");
		BucketSettings b = cManager.getBucket("vdb");
		bucket = cluster.openBucket("vdb", "..");
		
		ArrayList<AsyncViewRow> returnItem = getView("design_events", "events");

	}
	
	public static ArrayList<AsyncViewRow> getView(String designDoc, String view) {
		final 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("ERROR" + 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();
		}
		return result;
	}
}
....

If I use “ArrayList returnItem = getView(“design_events”, “events”);”, I get the following error: View design_events/events does not exist.

If I use “ArrayList returnItem = getView("_design/design_events/_view", “events”);”… I get the following error: INFO: Received a View HTTP response code (400) I did not expect, not retrying. ERROR{“error”:“bad_request”,“reason”:“attachments not supported in Couchbase”}

This is the printscreen of the indicated view in the CouchBase server: http://snag.gy/MtqnY.jpg

Regards,
Carlos


#4

Oh I see. you need to use getView("events, “events”)… the design_ is not part of the design name.

Also, you can simplify your code if you want to block in .toList().toBlocking().single(), which will also give you a list of AsyncViewRows. Note that of course you can use the blocking API for that right away :wink:


#5

Hi @daschl, I modified the code based on your comments, but it did not work.
I only see this error: View events/events does not exist.

There is something that I have to configure on the CouchBase server?


#6

Did you publish your design document?


#7

Hi @daschl… I published the document… If I go to the following path, I can see the event list:

http://qa…com:8092/vdb/_design/%2Fdesign_events/_view/events?inclusive_end=false&stale=update_after&connection_timeout=60000&limit=10&skip=0


#8

oh wait a second. Two things:

  • You are right its misleading… you named your design document actually design_events. So “getView(“design_events”, “events”);” is correct.
  • But check the URL you posted here. It ha a “%2F” in there, could it be that you named it “/design_events” ??

#9

Hi @daschl… you found the origin of the problem! :smile:

I did not put the “%2F” element on the document name… but If I used it on the getView("%2Fdesign_events", “events”) method, I could get the list of events!

Thanks for your help!


#10

@cfontana0 glad that we worked it out. In general, you probably want to avoid things like “/” or “design_” in design document names. Maybe just name your design document “events” and the view something like “all” or whatever it does. Then it’s much easier for you to handle later on.