Search in sources :

Example 1 with QueryResult

use of com.couchbase.client.java.query.QueryResult in project couchbase-jvm-clients by couchbase.

the class ParameterizedQueryExample method main.

public static void main(String... args) {
    Cluster cluster = Cluster.connect("127.0.0.1", "Administrator", "password");
    Bucket bucket = cluster.bucket("travel-sample");
    QueryResult result = cluster.query("select * from `travel-sample` where type = ? limit 5", queryOptions().parameters(JsonArray.from("airport")));
    System.out.println(result.rowsAsObject());
    result = cluster.query("select * from `travel-sample` where type = $type limit 5", queryOptions().parameters(JsonObject.create().put("type", "airport")));
    System.out.println(result.rowsAsObject());
}
Also used : QueryResult(com.couchbase.client.java.query.QueryResult) Bucket(com.couchbase.client.java.Bucket) Cluster(com.couchbase.client.java.Cluster)

Example 2 with QueryResult

use of com.couchbase.client.java.query.QueryResult in project couchbase-jvm-clients by couchbase.

the class QueryCollectionIntegrationTest method performsNonAdhocQuery.

@Test
void performsNonAdhocQuery() {
    Scope scope = cluster.bucket(config().bucketname()).scope(SCOPE_NAME);
    Collection collection = scope.collection(COLLECTION_NAME);
    String id = insertDoc(collection);
    QueryResult result = scope.query("select meta().id as id from `" + COLLECTION_NAME + "`", queryOptions().scanConsistency(QueryScanConsistency.REQUEST_PLUS).adhoc(false));
    boolean hasDoc = false;
    for (JsonObject row : result.rowsAsObject()) {
        if (row.getString("id").equals(id)) {
            hasDoc = true;
        }
    }
    assertTrue(hasDoc);
}
Also used : QueryResult(com.couchbase.client.java.query.QueryResult) ReactiveQueryResult(com.couchbase.client.java.query.ReactiveQueryResult) JsonObject(com.couchbase.client.java.json.JsonObject) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with QueryResult

use of com.couchbase.client.java.query.QueryResult in project couchbase-jvm-clients by couchbase.

the class QueryCollectionIntegrationTest method consistentWith.

// Test for MB-46876
@Test
void consistentWith() {
    String id = UUID.randomUUID().toString();
    Scope scope = cluster.bucket(config().bucketname()).scope(SCOPE_NAME);
    Collection collection = scope.collection(COLLECTION_NAME);
    MutationResult mr = collection.insert(id, FOO_CONTENT);
    QueryOptions options = queryOptions().consistentWith(MutationState.from(mr.mutationToken().get())).parameters(JsonArray.from(id));
    QueryResult result = scope.query("select * from `" + COLLECTION_NAME + "` where meta().id=$1", options);
    List<JsonObject> rows = result.rowsAs(JsonObject.class);
    assertEquals(1, rows.size());
    assertEquals(FOO_CONTENT, rows.get(0).getObject(COLLECTION_NAME));
}
Also used : QueryResult(com.couchbase.client.java.query.QueryResult) ReactiveQueryResult(com.couchbase.client.java.query.ReactiveQueryResult) JsonObject(com.couchbase.client.java.json.JsonObject) QueryOptions(com.couchbase.client.java.query.QueryOptions) MutationResult(com.couchbase.client.java.kv.MutationResult) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 4 with QueryResult

use of com.couchbase.client.java.query.QueryResult in project couchbase-jvm-clients by couchbase.

the class QueryConcurrencyIntegrationTest method setup.

@BeforeAll
static void setup() {
    cluster = createCluster(env -> env.ioConfig(IoConfig.enableMutationTokens(true)));
    Bucket bucket = cluster.bucket(config().bucketname());
    Collection collection = bucket.defaultCollection();
    bucket.waitUntilReady(Duration.ofSeconds(5));
    waitForService(bucket, ServiceType.QUERY);
    waitForQueryIndexerToHaveKeyspace(cluster, config().bucketname());
    bucketName = "`" + config().bucketname() + "`";
    createPrimaryIndex(cluster, config().bucketname());
    for (int i = 0; i < numDocsInserted; i++) {
        collection.insert("doc-" + i, FOO_CONTENT);
    }
    Util.waitUntilCondition(() -> {
        QueryResult countResult = cluster.query("select count(*) as count from " + bucketName, queryOptions().scanConsistency(QueryScanConsistency.REQUEST_PLUS));
        return countResult.rowsAsObject().get(0).getInt("count") >= numDocsInserted;
    }, Duration.ofSeconds(10), Duration.ofSeconds(1));
    QueryResult countResult = cluster.query("select count(*) as count from " + bucketName, queryOptions().scanConsistency(QueryScanConsistency.REQUEST_PLUS));
    assertTrue(numDocsInserted <= countResult.rowsAsObject().get(0).getInt("count"));
}
Also used : JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) QueryOptions.queryOptions(com.couchbase.client.java.query.QueryOptions.queryOptions) QueryScanConsistency(com.couchbase.client.java.query.QueryScanConsistency) Capabilities(com.couchbase.client.test.Capabilities) ClusterType(com.couchbase.client.test.ClusterType) IgnoreWhen(com.couchbase.client.test.IgnoreWhen) DISABLE_QUERY_TESTS_FOR_CLUSTER(com.couchbase.client.java.manager.query.QueryIndexManagerIntegrationTest.DISABLE_QUERY_TESTS_FOR_CLUSTER) Executors(java.util.concurrent.Executors) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) QueryResult(com.couchbase.client.java.query.QueryResult) CountDownLatch(java.util.concurrent.CountDownLatch) Util(com.couchbase.client.test.Util) BeforeAll(org.junit.jupiter.api.BeforeAll) ServiceType(com.couchbase.client.core.service.ServiceType) Duration(java.time.Duration) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) JsonObject(com.couchbase.client.java.json.JsonObject) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ExecutorService(java.util.concurrent.ExecutorService) IoConfig(com.couchbase.client.core.env.IoConfig) QueryResult(com.couchbase.client.java.query.QueryResult) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 5 with QueryResult

use of com.couchbase.client.java.query.QueryResult in project couchbase-jvm-clients by couchbase.

the class QueryIntegrationTest method handlesPreparedStatementsWithNamedArgs.

@Test
void handlesPreparedStatementsWithNamedArgs() {
    String id = insertDoc();
    for (int i = 0; i < 10; i++) {
        QueryOptions options = queryOptions().scanConsistency(QueryScanConsistency.REQUEST_PLUS).parameters(JsonObject.create().put("id", id)).adhoc(false);
        QueryResult result = cluster.query("select " + bucketName + ".* from " + bucketName + " where meta().id=$id", options);
        List<JsonObject> rows = result.rowsAs(JsonObject.class);
        assertEquals(1, rows.size());
        assertEquals(FOO_CONTENT, rows.get(0));
    }
}
Also used : QueryResult(com.couchbase.client.java.query.QueryResult) ReactiveQueryResult(com.couchbase.client.java.query.ReactiveQueryResult) JsonObject(com.couchbase.client.java.json.JsonObject) QueryOptions(com.couchbase.client.java.query.QueryOptions) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

QueryResult (com.couchbase.client.java.query.QueryResult)30 Test (org.junit.jupiter.api.Test)21 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)20 JsonObject (com.couchbase.client.java.json.JsonObject)18 QueryOptions (com.couchbase.client.java.query.QueryOptions)17 ReactiveQueryResult (com.couchbase.client.java.query.ReactiveQueryResult)17 Bucket (com.couchbase.client.java.Bucket)4 Cluster (com.couchbase.client.java.Cluster)4 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)2 MutationResult (com.couchbase.client.java.kv.MutationResult)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Mono (reactor.core.publisher.Mono)2 Reactor (com.couchbase.client.core.Reactor)1 Stability (com.couchbase.client.core.annotation.Stability)1 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)1 TracingIdentifiers (com.couchbase.client.core.cnc.TracingIdentifiers)1 IoConfig (com.couchbase.client.core.env.IoConfig)1 IndexExistsException (com.couchbase.client.core.error.IndexExistsException)1 IndexFailureException (com.couchbase.client.core.error.IndexFailureException)1