Async get insert retrievenew - srujanabala/springboot-couchbase GitHub Wiki
@GetMapping("/sdk/get/async") public String getDocsUsingSDK1ql() { Bucket bucket = CouchBaseHelper.getBucket(); Observable .just("88", "8", "80") // give coc id here .flatMap(bucket.async()::get) .subscribe(document -> System.out.println("Got: " + document)); return "Asynchoronous"; }
@GetMapping("/sdk/get/async") public List getDocsUsingSDK1ql() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); return Observable .just("88", "8", "80") .flatMap(CouchBaseHelper.getBucket().async()::get) .toList() .toBlocking() .single() .stream() .map(JsonDocument::content) .map(content -> { try { return objectMapper.readValue(content.toString(), Student.class); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }).collect(Collectors.toList()); }
@GetMapping("/sdk/get/async/insert")
public String insertDocsUsingSDK1ql() {
// Generate a number of dummy JSON documents
int docsToCreate = 5;
List<JsonDocument> documents = new ArrayList<JsonDocument>();
for (int i = 0; i < docsToCreate; i++) {
JsonObject content = JsonObject.create()
.put("counter", i)
.put("name", "Foo Bar");
documents.add(JsonDocument.
create("doc-"+i, content)); }
// Insert them in one batch, waiting until the last one is done.
return Observable
.from(documents)
.flatMap(new Func1<JsonDocument, Observable<JsonDocument>>() {
@Override
public Observable<JsonDocument> call(final JsonDocument docToInsert) {
return CouchBaseHelper.getBucket().async().insert(docToInsert);
}
})
.toList() // to get all
//.last() // to get last element
.toBlocking() .single().toString(); }