update N1QL Example code

This commit is contained in:
Seun Matt 2017-09-17 15:30:10 +01:00
parent 695d335e61
commit c70752ee44
3 changed files with 33 additions and 20 deletions

View File

@ -5,16 +5,26 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.vavr.control.Try;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class CodeSnippets {
private static ObjectMapper objectMapper = new ObjectMapper();
public static List<JsonNode> extractJsonResult(N1qlQueryResult result) {
ObjectMapper objectMapper = new ObjectMapper();
return result.allRows().stream()
.map(row -> Try.of(() -> objectMapper.readTree(row.value().toString()))
.getOrNull())
.map(row -> {
try {
return objectMapper.readTree(row.value().toString());
}catch (IOException e) {
e.printStackTrace();
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
}

View File

@ -12,6 +12,7 @@ import com.couchbase.client.java.query.N1qlQueryResult;
import com.couchbase.client.java.query.N1qlQueryRow;
import com.couchbase.client.java.query.Statement;
import com.fasterxml.jackson.databind.JsonNode;
import io.vavr.collection.Stream;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@ -29,6 +30,7 @@ import rx.Observable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import static com.baeldung.couchbase.n1ql.CodeSnippets.extractJsonResult;
import static com.couchbase.client.java.query.Select.select;
@ -190,15 +192,16 @@ public class N1QLIntegrationTest {
@Test
public void givenDocuments_whenBatchInsert_thenResult() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
int docsToCreate = 10;
List<JsonDocument> documents = new ArrayList<>();
for (int i = 5; i < docsToCreate; i++) {
List<JsonDocument> documents = Stream.rangeClosed(0,10)
.map( i -> {
JsonObject content = JsonObject.create()
.put("id", i)
.put("type", "airline")
.put("name", "Sample Airline " + i);
documents.add(JsonDocument.create("cust_" + i, content));
}
return JsonDocument.create("cust_" + i, content);
})
.collect(Collectors.toList());
List<JsonDocument> r5 = Observable
.from(documents)