update N1QL Example code
This commit is contained in:
parent
695d335e61
commit
c70752ee44
@ -29,9 +29,9 @@
|
||||
<version>${jackson-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vavr</groupId>
|
||||
<artifactId>vavr</artifactId>
|
||||
<version>${vavr-version}</version>
|
||||
<groupId>io.vavr</groupId>
|
||||
<artifactId>vavr</artifactId>
|
||||
<version>${vavr-version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
|
@ -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,23 +192,24 @@ 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));
|
||||
}
|
||||
.put("id", i)
|
||||
.put("type", "airline")
|
||||
.put("name", "Sample Airline " + i);
|
||||
return JsonDocument.create("cust_" + i, content);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<JsonDocument> r5 = Observable
|
||||
.from(documents)
|
||||
.flatMap(doc -> bucket.async().insert(doc))
|
||||
.toList()
|
||||
.last()
|
||||
.toBlocking()
|
||||
.single();
|
||||
.from(documents)
|
||||
.flatMap(doc -> bucket.async().insert(doc))
|
||||
.toList()
|
||||
.last()
|
||||
.toBlocking()
|
||||
.single();
|
||||
|
||||
r5.forEach(System.out::println);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user