Updated N1QL Example Code (#2636)

* added updated example codes

* updated example code StringToCharStream

* deleted StringToCharStream.java locally

* removed redundant file

* added code for apache commons collection SetUtils

* refactored example code

* added example code for bytebuddy

* added example code for PCollections

* update pom

* refactored tests for PCollections

* spring security xml config

* spring security xml config

* remove redundant comment

* example code for apache-shiro

* updated example code for Vavr Collections

* updated Vavr's Collection example

* updated Vavr Collection file

* updated example code for Apache Shiro

* updated Vavr Collections example

* added example code for N1QL

* update example code for N1QL

* added integration test for N1QL

* update N1QL Example code
This commit is contained in:
Seun Matt 2017-09-18 01:05:11 +01:00 committed by maibin
parent 3a9654370a
commit f7ab9fa910
3 changed files with 34 additions and 40 deletions

View File

@ -28,12 +28,6 @@
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version> <version>${jackson-version}</version>
</dependency> </dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>${vavr-version}</version>
</dependency>
<!-- Spring Context for Dependency Injection --> <!-- Spring Context for Dependency Injection -->
<dependency> <dependency>
@ -82,7 +76,6 @@
<couchbase.client.version>2.5.0</couchbase.client.version> <couchbase.client.version>2.5.0</couchbase.client.version>
<spring-framework.version>4.3.5.RELEASE</spring-framework.version> <spring-framework.version>4.3.5.RELEASE</spring-framework.version>
<commons-lang3.version>3.5</commons-lang3.version> <commons-lang3.version>3.5</commons-lang3.version>
<vavr-version>0.9.0</vavr-version>
<jackson-version>2.9.1</jackson-version> <jackson-version>2.9.1</jackson-version>
</properties> </properties>

View File

@ -3,18 +3,26 @@ package com.baeldung.couchbase.n1ql;
import com.couchbase.client.java.query.N1qlQueryResult; import com.couchbase.client.java.query.N1qlQueryResult;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
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());
} }

View File

@ -1,9 +1,7 @@
package com.baeldung.couchbase.n1ql; package com.baeldung.couchbase.n1ql;
import com.baeldung.couchbase.n1ql.IntegrationTestConfig;
import com.couchbase.client.java.Bucket; import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster; import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.document.JsonDocument; import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.document.json.JsonArray; import com.couchbase.client.java.document.json.JsonArray;
import com.couchbase.client.java.document.json.JsonObject; import com.couchbase.client.java.document.json.JsonObject;
@ -12,30 +10,24 @@ 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 org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import rx.Observable; import rx.Observable;
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 java.util.stream.IntStream;
import java.util.stream.Stream;
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;
import static com.couchbase.client.java.query.dsl.Expression.i; import static com.couchbase.client.java.query.dsl.Expression.*;
import static com.couchbase.client.java.query.dsl.Expression.s; import static org.junit.Assert.assertNotNull;
import static com.couchbase.client.java.query.dsl.Expression.x;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { IntegrationTestConfig.class }) @ContextConfiguration(classes = { IntegrationTestConfig.class })
@ -190,23 +182,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 = IntStream.rangeClosed(0,10)
for (int i = 5; i < docsToCreate; i++) { .mapToObj( 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);
} }