Search in sources :

Example 1 with AnalyticsResult

use of com.couchbase.client.java.analytics.AnalyticsResult in project couchbase-jvm-clients by couchbase.

the class AnalyticsCollectionIntegrationTest method performsDataverseCollectionQuery.

@Test
void performsDataverseCollectionQuery() {
    cluster.analyticsQuery("ALTER COLLECTION `" + bucket.name() + "`.`" + scopeName + "`.`" + collectionName + "` ENABLE ANALYTICS");
    // REQUEST_PLUS makes query hang
    // AnalyticsOptions opts = AnalyticsOptions.analyticsOptions().scanConsistency(AnalyticsScanConsistency.REQUEST_PLUS);
    // Ensure doc ingested by analytics
    waitUntilCondition(() -> singletonMap(delimitedDataverseName, singletonMap(collectionName, 0L)).equals(analytics.getPendingMutations()));
    Scope scope = cluster.bucket(config().bucketname()).scope(scopeName);
    AnalyticsResult result = scope.analyticsQuery("SELECT * FROM `" + bucket.name() + "`.`" + scopeName + "`.`" + collectionName + "` WHERE `" + collectionName + "`.foo=\"bar\"");
    List<JsonObject> rows = result.rowsAs(JsonObject.class);
    assertFalse(rows.isEmpty());
}
Also used : JsonObject(com.couchbase.client.java.json.JsonObject) AnalyticsResult(com.couchbase.client.java.analytics.AnalyticsResult) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 2 with AnalyticsResult

use of com.couchbase.client.java.analytics.AnalyticsResult in project couchbase-jvm-clients by couchbase.

the class AnalyticsCollectionIntegrationTest method performsDataverseQuery.

@Test
void performsDataverseQuery() {
    // default
    analytics.createDataset(dataset, bucket.name());
    analytics.createDataverse(dataverse);
    analytics.createDataset(dataset, bucket.name(), createDatasetAnalyticsOptions().dataverseName(dataverse));
    // REQUEST_PLUS makes query hang
    // AnalyticsOptions opts = AnalyticsOptions.analyticsOptions().scanConsistency(AnalyticsScanConsistency.REQUEST_PLUS).timeout(Duration.ofSeconds(300));
    AnalyticsResult result = cluster.analyticsQuery("SELECT * FROM " + dataverse + "." + dataset + " where " + dataset + ".test= \"hello\"");
    List<JsonObject> rows = result.rowsAs(JsonObject.class);
    assertFalse(!rows.isEmpty());
    AnalyticsMetaData meta = result.metaData();
    assertFalse(meta.clientContextId().isEmpty());
    assertTrue(meta.signature().isPresent());
    assertFalse(meta.requestId().isEmpty());
    assertEquals(AnalyticsStatus.SUCCESS, meta.status());
    assertFalse(meta.metrics().elapsedTime().isZero());
    assertFalse(meta.metrics().executionTime().isZero());
    assertEquals(rows.size(), meta.metrics().resultCount());
    // assertEquals(rows.size(), meta.metrics().processedObjects()); // fails
    // assertTrue(meta.metrics().resultSize() > 0); // fails
    assertTrue(meta.warnings().isEmpty());
// assertEquals(1, meta.metrics().errorCount()); //fails
}
Also used : JsonObject(com.couchbase.client.java.json.JsonObject) AnalyticsResult(com.couchbase.client.java.analytics.AnalyticsResult) AnalyticsMetaData(com.couchbase.client.java.analytics.AnalyticsMetaData) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with AnalyticsResult

use of com.couchbase.client.java.analytics.AnalyticsResult in project couchbase-jvm-clients by couchbase.

the class AnalyticsIntegrationTest method canSetCustomContextId.

@Test
void canSetCustomContextId() {
    String contextId = "mycontextid";
    AnalyticsResult result = cluster.analyticsQuery("SELECT DataverseName FROM Metadata.`Dataverse`", analyticsOptions().clientContextId(contextId));
    assertEquals(result.metaData().clientContextId(), contextId);
}
Also used : AnalyticsResult(com.couchbase.client.java.analytics.AnalyticsResult) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 4 with AnalyticsResult

use of com.couchbase.client.java.analytics.AnalyticsResult in project couchbase-jvm-clients by couchbase.

the class AnalyticsIntegrationTest method performsDataverseQuery.

@Test
void performsDataverseQuery() {
    AnalyticsResult result = cluster.analyticsQuery("SELECT DataverseName FROM Metadata.`Dataverse`");
    List<JsonObject> rows = result.rowsAs(JsonObject.class);
    assertFalse(rows.isEmpty());
    for (JsonObject row : rows) {
        assertNotNull(row.get("DataverseName"));
    }
    AnalyticsMetaData meta = result.metaData();
    assertFalse(meta.clientContextId().isEmpty());
    assertTrue(meta.signature().isPresent());
    assertFalse(meta.requestId().isEmpty());
    assertEquals(AnalyticsStatus.SUCCESS, meta.status());
    assertFalse(meta.metrics().elapsedTime().isZero());
    assertFalse(meta.metrics().executionTime().isZero());
    assertEquals(rows.size(), meta.metrics().resultCount());
    assertEquals(rows.size(), meta.metrics().processedObjects());
    assertTrue(meta.metrics().resultSize() > 0);
    assertTrue(meta.warnings().isEmpty());
    assertEquals(0, meta.metrics().errorCount());
}
Also used : JsonObject(com.couchbase.client.java.json.JsonObject) AnalyticsResult(com.couchbase.client.java.analytics.AnalyticsResult) AnalyticsMetaData(com.couchbase.client.java.analytics.AnalyticsMetaData) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 5 with AnalyticsResult

use of com.couchbase.client.java.analytics.AnalyticsResult in project connectors-se by Talend.

the class CouchbaseInput method init.

@PostConstruct
public void init() {
    Cluster cluster = service.openConnection(configuration.getDataSet().getDatastore());
    Bucket bucket = cluster.bucket(configuration.getDataSet().getBucket());
    collection = service.openDefaultCollection(cluster, configuration.getDataSet().getBucket());
    if (configuration.isCreatePrimaryIndex()) {
        cluster.queryIndexes().createPrimaryIndex(bucket.name(), CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions().ignoreIfExists(true));
    }
    columnsSet = new HashSet<>();
    if (configuration.getSelectAction() == SelectAction.ANALYTICS) {
        AnalyticsResult analyticsResult = null;
        try {
            analyticsResult = cluster.analyticsQuery(configuration.getQuery());
        } catch (CouchbaseException e) {
            LOG.error(i18n.queryResultError(e.getMessage()));
            throw new ComponentException(i18n.queryResultError(e.getMessage()));
        }
        queryResultsIterator = analyticsResult.rowsAsObject().iterator();
    } else {
        // DSL API (Statement, AsPath classes etc. was deprecated, cannot use it anymore!)
        // In most cases, a simple string statement is the best replacement.
        QueryResult n1qlResult;
        StringBuilder statementBuilder;
        switch(configuration.getSelectAction()) {
            case ALL:
                statementBuilder = new StringBuilder();
                statementBuilder.append("SELECT meta().id as `_meta_id_`, * FROM `").append(bucket.name()).append("`");
                if (!configuration.getLimit().isEmpty()) {
                    statementBuilder.append(" LIMIT ").append(configuration.getLimit().trim());
                }
                n1qlResult = cluster.query(statementBuilder.toString());
                break;
            case N1QL:
                /*
                 * should contain "meta().id as `_meta_id_`" field for non-json (binary) documents
                 */
                n1qlResult = cluster.query(configuration.getQuery());
                break;
            case ONE:
                statementBuilder = new StringBuilder();
                statementBuilder.append("SELECT meta().id as `_meta_id_`, * FROM `").append(bucket.name()).append("`");
                statementBuilder.append(" USE KEYS \"").append(configuration.getDocumentId()).append("\"");
                n1qlResult = cluster.query(statementBuilder.toString());
                break;
            default:
                throw new ComponentException("Select action: '" + configuration.getSelectAction() + "' is unsupported");
        }
        queryResultsIterator = n1qlResult.rowsAsObject().iterator();
    }
}
Also used : QueryResult(com.couchbase.client.java.query.QueryResult) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) Bucket(com.couchbase.client.java.Bucket) ComponentException(org.talend.sdk.component.api.exception.ComponentException) Cluster(com.couchbase.client.java.Cluster) AnalyticsResult(com.couchbase.client.java.analytics.AnalyticsResult) PostConstruct(javax.annotation.PostConstruct)

Aggregations

AnalyticsResult (com.couchbase.client.java.analytics.AnalyticsResult)7 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)6 Test (org.junit.jupiter.api.Test)6 JsonObject (com.couchbase.client.java.json.JsonObject)4 AnalyticsMetaData (com.couchbase.client.java.analytics.AnalyticsMetaData)2 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)1 Bucket (com.couchbase.client.java.Bucket)1 Cluster (com.couchbase.client.java.Cluster)1 QueryResult (com.couchbase.client.java.query.QueryResult)1 PostConstruct (javax.annotation.PostConstruct)1 ComponentException (org.talend.sdk.component.api.exception.ComponentException)1