Example Code for N1QL (#2629)

* 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
This commit is contained in:
Seun Matt 2017-09-17 01:42:33 +01:00 committed by maibin
parent a3f70aafe4
commit f384e46bb1
6 changed files with 349 additions and 1 deletions

6
couchbase-sdk/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
# Created by .ignore support plugin (hsz.mobi)
# IntelliJ project files
.idea
*.iml
/target/

View File

@ -23,6 +23,18 @@
<version>${couchbase.client.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>${vavr-version}</version>
</dependency>
<!-- Spring Context for Dependency Injection -->
<dependency>
<groupId>org.springframework</groupId>
@ -67,9 +79,11 @@
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<couchbase.client.version>2.4.0</couchbase.client.version>
<couchbase.client.version>2.5.0</couchbase.client.version>
<spring-framework.version>4.3.5.RELEASE</spring-framework.version>
<commons-lang3.version>3.5</commons-lang3.version>
<vavr-version>0.9.0</vavr-version>
<jackson-version>2.9.1</jackson-version>
</properties>
</project>

View File

@ -0,0 +1,26 @@
package com.baeldung.couchbase.n1ql;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BucketFactory {
@Autowired
private Cluster cluster;
private Bucket travelSampleBucket;
private Bucket testBucket;
public Bucket getTravelSampleBucket() {
return (travelSampleBucket != null) ?
travelSampleBucket : cluster.openBucket("travel-sample");
}
public Bucket getTestBucket() {
return (testBucket != null) ?
testBucket : cluster.openBucket("test");
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.couchbase.n1ql;
import com.couchbase.client.java.query.N1qlQueryResult;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.vavr.control.Try;
import java.util.List;
import java.util.stream.Collectors;
public class CodeSnippets {
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())
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.couchbase.n1ql;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.env.CouchbaseEnvironment;
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
@Configuration
@ComponentScan(basePackages = { "com.baeldung.couchbase.n1ql" })
public class IntegrationTestConfig {
@Bean
public Cluster cluster() {
CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder()
.connectTimeout(60000)
.build();
return CouchbaseCluster.create(env, "127.0.0.1");
}
}

View File

@ -0,0 +1,255 @@
package com.baeldung.couchbase.n1ql;
import com.baeldung.couchbase.n1ql.IntegrationTestConfig;
import com.couchbase.client.java.Bucket;
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.json.JsonArray;
import com.couchbase.client.java.document.json.JsonObject;
import com.couchbase.client.java.query.N1qlQuery;
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 org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
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.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import rx.Observable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static com.baeldung.couchbase.n1ql.CodeSnippets.extractJsonResult;
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.s;
import static com.couchbase.client.java.query.dsl.Expression.x;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { IntegrationTestConfig.class })
public class N1QLIntegrationTest {
@Autowired
private Cluster cluster;
@Autowired
private BucketFactory bucketFactory;
@Test
public void givenAutowiredCluster_whenNotNull_thenNotNull() {
assertNotNull(cluster);
}
@Test
public void givenBucketFactory_whenGetTestBucket_thenNotNull() {
assertNotNull(bucketFactory.getTestBucket());
}
@Test
public void givenBucketFactory_whenGetTravelSampleBucket_thenNotNull() {
assertNotNull(bucketFactory.getTravelSampleBucket());
}
@Test
public void givenDocument_whenInsert_thenResult() {
Bucket bucket = bucketFactory.getTestBucket();
JsonObject personObj = JsonObject.create()
.put("name", "John")
.put("email", "john@doe.com")
.put("interests", JsonArray.from("Java", "Nigerian Jollof"));
String id = UUID.randomUUID().toString();
JsonDocument doc = JsonDocument.create(id, personObj);
bucket.insert(doc);
assertNotNull(bucket.get(id));
}
@Test
public void whenBasicSelectQuery_thenGetQueryResult() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
N1qlQueryResult result
= bucket.query(N1qlQuery.simple("SELECT * FROM test"));
result.forEach(System.out::println);
System.out.println("result count: " + result.info().resultCount());
System.out.println("error count: " + result.info().errorCount());
}
@Test
public void givenSelectStatement_whenQuery_thenResult() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
String query = "SELECT name FROM `travel-sample` " +
"WHERE type = 'airport' LIMIT 100";
N1qlQueryResult result1 = bucket.query(N1qlQuery.simple(query));
System.out.println("Result Count " + result1.info().resultCount());
N1qlQueryRow row = result1.allRows().get(0);
JsonObject rowJson = row.value();
System.out.println("Name in First Row " + rowJson.get("name"));
}
@Test
public void givenSelectStatement2_whenQuery_thenResult() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
JsonObject pVal = JsonObject.create().put("type", "airport");
String query = "SELECT * FROM `travel-sample` " +
"WHERE type = $type LIMIT 100";
N1qlQueryResult r2 = bucket.query(N1qlQuery.parameterized(query, pVal));
System.out.println(r2.allRows());
List<JsonNode> list = extractJsonResult(r2);
System.out.println(
list.get(0).get("travel-sample").get("airportname").asText());
}
@Test
public void givenSelectDSL_whenQuery_thenResult() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
Statement statement = select("*")
.from(i("travel-sample"))
.where(x("type").eq(s("airport")))
.limit(100);
N1qlQueryResult r3 = bucket.query(N1qlQuery.simple(statement));
List<JsonNode> list2 = extractJsonResult(r3);
System.out.println("First Airport Name: " + list2.get(0).get("travel-sample").get("airportname").asText());
}
@Test
public void givenSelectStatementWithOperators_whenQuery_thenResult() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
String query2 = "SELECT t.city, " +
"t.airportname || \" (\" || t.faa || \")\" AS portname_faa " +
"FROM `travel-sample` t " +
"WHERE t.type=\"airport\"" +
"AND t.country LIKE '%States'" +
"AND t.geo.lat >= 70 " +
"LIMIT 2";
N1qlQueryResult r4 = bucket.query(N1qlQuery.simple(query2));
List<JsonNode> list3 = extractJsonResult(r4);
System.out.println("First Doc : " + list3.get(0));
}
@Test
public void givenSelectStatementWithDSL2_whenQuery_thenResult() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
Statement st2 = select(
x("t.city, t.airportname")
.concat(s(" (")).concat(x("t.faa")).concat(s(")")).as("portname_faa"))
.from(i("travel-sample").as("t"))
.where( x("t.type").eq(s("airport"))
.and(x("t.country").like(s("%States")))
.and(x("t.geo.lat").gte(70)))
.limit(2);
N1qlQueryResult r5 = bucket.query(N1qlQuery.simple(st2));
List<JsonNode> list5 = extractJsonResult(r5);
System.out.println("First Doc : " + list5.get(0));
System.out.println("Query from Statement2: " + st2.toString());
}
@Test
public void givenInsertStatement_whenQuery_thenUpdate() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
String query = "INSERT INTO `travel-sample` (KEY, VALUE) " +
" VALUES(" +
"\"cust1293\", " +
"{\"id\":\"1293\",\"name\":\"Sample Airline\", \"type\":\"airline\"})" +
" RETURNING META().id as docid, *";
N1qlQueryResult r1 = bucket.query(N1qlQuery.simple(query));
r1.forEach(System.out::println);
}
@Test
public void givenDocument_whenInsert_thenResults() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
JsonObject ob = JsonObject.create()
.put("id", "1293")
.put("name", "Sample Airline")
.put("type", "airline");
bucket.insert(JsonDocument.create("cust1295", ob));
}
@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++) {
JsonObject content = JsonObject.create()
.put("id", i)
.put("type", "airline")
.put("name", "Sample Airline " + i);
documents.add(JsonDocument.create("cust_" + i, content));
}
List<JsonDocument> r5 = Observable
.from(documents)
.flatMap(doc -> bucket.async().insert(doc))
.toList()
.last()
.toBlocking()
.single();
r5.forEach(System.out::println);
}
@Test
public void givenUpdateStatement_whenQuery_thenUpdate() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
String query2 = "UPDATE `travel-sample` USE KEYS \"cust_1\" " +
"SET name=\"Sample Airline Updated\" RETURNING name";
N1qlQueryResult result = bucket.query(N1qlQuery.simple(query2));
result.forEach(System.out::println);
}
@Test
public void givenDocument_whenUpsert_thenUpdate() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
JsonObject o2 = JsonObject.create()
.put("name", "Sample Airline Updated");
bucket.upsert(JsonDocument.create("cust_1", o2));
}
@Test
public void givenUnestUpdateStatement_whenQuery_thenResult() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
String query3 = "UPDATE `travel-sample` USE KEYS \"cust_2\" " +
"UNSET name RETURNING *";
N1qlQueryResult result1 = bucket.query(N1qlQuery.simple(query3));
result1.forEach(System.out::println);
}
@Test
public void givenDeleteStatement_whenQuery_thenDelete() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
String query4 = "DELETE FROM `travel-sample` USE KEYS \"cust_50\"";
N1qlQueryResult result4 = bucket.query(N1qlQuery.simple(query4));
}
@Test
public void givenDeleteStatement2_whenQuery_thenDelete() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
String query5 = "DELETE FROM `travel-sample` WHERE id = 0 RETURNING *";
N1qlQueryResult result5 = bucket.query(N1qlQuery.simple(query5));
}
}