update N1QL Example code
This commit is contained in:
parent
695d335e61
commit
c70752ee44
@ -29,9 +29,9 @@
|
|||||||
<version>${jackson-version}</version>
|
<version>${jackson-version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.vavr</groupId>
|
<groupId>io.vavr</groupId>
|
||||||
<artifactId>vavr</artifactId>
|
<artifactId>vavr</artifactId>
|
||||||
<version>${vavr-version}</version>
|
<version>${vavr-version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,16 +5,26 @@ import com.fasterxml.jackson.databind.JsonNode;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import io.vavr.control.Try;
|
import io.vavr.control.Try;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class CodeSnippets {
|
public class CodeSnippets {
|
||||||
|
|
||||||
|
private static ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
public static List<JsonNode> extractJsonResult(N1qlQueryResult result) {
|
public static List<JsonNode> extractJsonResult(N1qlQueryResult result) {
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
return result.allRows().stream()
|
return result.allRows().stream()
|
||||||
.map(row -> Try.of(() -> objectMapper.readTree(row.value().toString()))
|
.map(row -> {
|
||||||
.getOrNull())
|
try {
|
||||||
|
return objectMapper.readTree(row.value().toString());
|
||||||
|
}catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.filter(Objects::nonNull)
|
||||||
.collect(Collectors.toList());
|
.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.N1qlQueryRow;
|
||||||
import com.couchbase.client.java.query.Statement;
|
import com.couchbase.client.java.query.Statement;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import io.vavr.collection.Stream;
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -29,6 +30,7 @@ import rx.Observable;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static com.baeldung.couchbase.n1ql.CodeSnippets.extractJsonResult;
|
import static com.baeldung.couchbase.n1ql.CodeSnippets.extractJsonResult;
|
||||||
import static com.couchbase.client.java.query.Select.select;
|
import static com.couchbase.client.java.query.Select.select;
|
||||||
@ -190,23 +192,24 @@ public class N1QLIntegrationTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenDocuments_whenBatchInsert_thenResult() {
|
public void givenDocuments_whenBatchInsert_thenResult() {
|
||||||
Bucket bucket = bucketFactory.getTravelSampleBucket();
|
Bucket bucket = bucketFactory.getTravelSampleBucket();
|
||||||
int docsToCreate = 10;
|
|
||||||
List<JsonDocument> documents = new ArrayList<>();
|
List<JsonDocument> documents = Stream.rangeClosed(0,10)
|
||||||
for (int i = 5; i < docsToCreate; i++) {
|
.map( i -> {
|
||||||
JsonObject content = JsonObject.create()
|
JsonObject content = JsonObject.create()
|
||||||
.put("id", i)
|
.put("id", i)
|
||||||
.put("type", "airline")
|
.put("type", "airline")
|
||||||
.put("name", "Sample Airline " + i);
|
.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
|
List<JsonDocument> r5 = Observable
|
||||||
.from(documents)
|
.from(documents)
|
||||||
.flatMap(doc -> bucket.async().insert(doc))
|
.flatMap(doc -> bucket.async().insert(doc))
|
||||||
.toList()
|
.toList()
|
||||||
.last()
|
.last()
|
||||||
.toBlocking()
|
.toBlocking()
|
||||||
.single();
|
.single();
|
||||||
|
|
||||||
r5.forEach(System.out::println);
|
r5.forEach(System.out::println);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user