Search in sources :

Example 86 with JsonObject

use of com.couchbase.client.java.json.JsonObject in project connectors-se by Talend.

the class CouchbaseInput method addColumn.

private void addColumn(Record.Builder recordBuilder, final Schema.Entry entry, Object value) {
    final Schema.Entry.Builder entryBuilder = builderFactory.newEntryBuilder();
    Schema.Type type = entry.getType();
    entryBuilder.withName(entry.getName()).withNullable(true).withType(type);
    if (value == null)
        return;
    switch(type) {
        case ARRAY:
            Schema elementSchema = entry.getElementSchema();
            entryBuilder.withElementSchema(elementSchema);
            if (elementSchema.getType() == Schema.Type.RECORD) {
                List<Record> recordList = new ArrayList<>();
                // schema of the first element
                Schema currentSchema = elementSchema.getEntries().get(0).getElementSchema();
                for (int i = 0; i < ((JsonArray) value).size(); i++) {
                    JsonObject currentJsonObject = (JsonObject) ((JsonArray) value).get(i);
                    recordList.add(createRecord(currentSchema, currentJsonObject));
                }
                recordBuilder.withArray(entryBuilder.build(), recordList);
            } else {
                recordBuilder.withArray(entryBuilder.build(), ((JsonArray) value).toList());
            }
            break;
        case FLOAT:
            recordBuilder.withFloat(entryBuilder.build(), (Float) value);
            break;
        case DOUBLE:
            recordBuilder.withDouble(entryBuilder.build(), (Double) value);
            break;
        case BYTES:
            throw new ComponentException("BYTES is unsupported");
        case STRING:
            recordBuilder.withString(entryBuilder.build(), value.toString());
            break;
        case LONG:
            recordBuilder.withLong(entryBuilder.build(), (Long) value);
            break;
        case INT:
            recordBuilder.withInt(entryBuilder.build(), (Integer) value);
            break;
        case DATETIME:
            recordBuilder.withDateTime(entryBuilder.build(), (ZonedDateTime) value);
            break;
        case BOOLEAN:
            recordBuilder.withBoolean(entryBuilder.build(), (Boolean) value);
            break;
        case RECORD:
            entryBuilder.withElementSchema(entry.getElementSchema());
            recordBuilder.withRecord(entryBuilder.build(), createRecord(entry.getElementSchema(), (JsonObject) value));
            break;
    }
}
Also used : Schema(org.talend.sdk.component.api.record.Schema) ArrayList(java.util.ArrayList) ComponentException(org.talend.sdk.component.api.exception.ComponentException) JsonObject(com.couchbase.client.java.json.JsonObject) Record(org.talend.sdk.component.api.record.Record)

Example 87 with JsonObject

use of com.couchbase.client.java.json.JsonObject in project connectors-se by Talend.

the class CouchbaseInputTest method insertTestDataToDBAndPrepareAnalytics.

private void insertTestDataToDBAndPrepareAnalytics(String idPrefix) {
    int insertCount = 2;
    Collection collection = couchbaseCluster.bucket(BUCKET_NAME).defaultCollection();
    couchbaseCluster.analyticsQuery("ALTER COLLECTION `" + BUCKET_NAME + "`._default._default ENABLE ANALYTICS", AnalyticsOptions.analyticsOptions().scanConsistency(AnalyticsScanConsistency.REQUEST_PLUS).priority(true));
    List<JsonObject> jsonObjects = createJsonObjects();
    for (int i = 0; i < insertCount; i++) {
        collection.insert(generateDocId(idPrefix, i), jsonObjects.get(i));
    }
    // Bucket needs some time to index newly created entries; analytics dataset will
    // be based on those entries.
    // We need to wait until the data is correct in the statistics by sending an
    // actual query to the dataset using ScanConsistency = Request.PLUS
    couchbaseCluster.analyticsQuery("select * from " + BUCKET_NAME, AnalyticsOptions.analyticsOptions().scanConsistency(AnalyticsScanConsistency.REQUEST_PLUS));
}
Also used : Collection(com.couchbase.client.java.Collection) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 88 with JsonObject

use of com.couchbase.client.java.json.JsonObject in project connectors-se by Talend.

the class CouchbaseInputTest method createJsonObjects.

private List<JsonObject> createJsonObjects() {
    TestData testData = new TestData();
    List<JsonObject> jsonObjects = new ArrayList<>();
    for (int i = 1; i <= 2; i++) {
        jsonObjects.add(createJsonObject(testData.getColId() + i));
    }
    return jsonObjects;
}
Also used : TestData(org.talend.components.couchbase.TestData) ArrayList(java.util.ArrayList) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 89 with JsonObject

use of com.couchbase.client.java.json.JsonObject in project testcontainers-java by testcontainers.

the class CouchbaseContainerTest method testBasicContainerUsageForCommunityContainer.

@Test
public void testBasicContainerUsageForCommunityContainer() {
    BucketDefinition bucketDefinition = new BucketDefinition("mybucket");
    try (CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE_COMMUNITY).withBucket(bucketDefinition)) {
        setUpClient(container, cluster -> {
            Bucket bucket = cluster.bucket(bucketDefinition.getName());
            bucket.waitUntilReady(Duration.ofSeconds(10L));
            Collection collection = bucket.defaultCollection();
            collection.upsert("foo", JsonObject.create().put("key", "value"));
            JsonObject fooObject = collection.get("foo").contentAsObject();
            assertEquals("value", fooObject.getString("key"));
        });
    }
}
Also used : Bucket(com.couchbase.client.java.Bucket) Collection(com.couchbase.client.java.Collection) JsonObject(com.couchbase.client.java.json.JsonObject) Test(org.junit.Test)

Example 90 with JsonObject

use of com.couchbase.client.java.json.JsonObject in project testcontainers-java by testcontainers.

the class CouchbaseContainerTest method testBasicContainerUsageForEnterpriseContainer.

@Test
public void testBasicContainerUsageForEnterpriseContainer() {
    // bucket_definition {
    BucketDefinition bucketDefinition = new BucketDefinition("mybucket");
    try (// container_definition {
    CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE_ENTERPRISE).withBucket(bucketDefinition)) // }
    {
        setUpClient(container, cluster -> {
            Bucket bucket = cluster.bucket(bucketDefinition.getName());
            bucket.waitUntilReady(Duration.ofSeconds(10L));
            Collection collection = bucket.defaultCollection();
            collection.upsert("foo", JsonObject.create().put("key", "value"));
            JsonObject fooObject = collection.get("foo").contentAsObject();
            assertEquals("value", fooObject.getString("key"));
        });
    }
}
Also used : Bucket(com.couchbase.client.java.Bucket) Collection(com.couchbase.client.java.Collection) JsonObject(com.couchbase.client.java.json.JsonObject) Test(org.junit.Test)

Aggregations

JsonObject (com.couchbase.client.java.json.JsonObject)189 Test (org.junit.jupiter.api.Test)145 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)119 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)39 JsonArray (com.couchbase.client.java.json.JsonArray)18 QueryResult (com.couchbase.client.java.query.QueryResult)16 GetResult (com.couchbase.client.java.kv.GetResult)15 MutationResult (com.couchbase.client.java.kv.MutationResult)15 QueryOptions (com.couchbase.client.java.query.QueryOptions)15 ReactiveQueryResult (com.couchbase.client.java.query.ReactiveQueryResult)15 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)6 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)6 Collection (com.couchbase.client.java.Collection)6 ReplaceBodyWithXattr (com.couchbase.client.java.kv.ReplaceBodyWithXattr)6 Duration (java.time.Duration)6 DisplayName (org.junit.jupiter.api.DisplayName)6 CouchbaseUtilTest (org.talend.components.couchbase.CouchbaseUtilTest)6 ArrayList (java.util.ArrayList)5 TestData (org.talend.components.couchbase.TestData)5 MutateInResult (com.couchbase.client.java.kv.MutateInResult)4