Search in sources :

Example 91 with JsonObject

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

the class CouchbaseInputTest method insertTestDataToDB.

private void insertTestDataToDB(String idPrefix) {
    Collection collection = couchbaseCluster.bucket(BUCKET_NAME).defaultCollection();
    List<JsonObject> jsonObjects = createJsonObjects();
    for (int i = 0; i < 2; i++) {
        collection.insert(generateDocId(idPrefix, i), jsonObjects.get(i));
    }
}
Also used : Collection(com.couchbase.client.java.Collection) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 92 with JsonObject

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

the class CouchbaseInputTest method firstValueIsNullInInputDBTest.

@Test
@DisplayName("When input data is null, record will be skipped")
void firstValueIsNullInInputDBTest() {
    log.info("Test start: firstValueIsNullInInputDBTest");
    String idPrefix = "firstValueIsNullInInputDBTest";
    Collection collection = couchbaseCluster.bucket(BUCKET_NAME).defaultCollection();
    JsonObject json = JsonObject.create().put("t_string1", "RRRR1").put("t_string2", "RRRR2").putNull("t_string3");
    collection.insert(generateDocId(idPrefix, 0), json);
    CouchbaseInputConfiguration inputConfiguration = getInputConfiguration();
    inputConfiguration.setSelectAction(SelectAction.N1QL);
    inputConfiguration.setQuery("SELECT `" + BUCKET_NAME + "`.* FROM `" + BUCKET_NAME + "` where meta().id like \"" + idPrefix + "%\"");
    executeJob(inputConfiguration);
    final List<Record> res = componentsHandler.getCollectedData(Record.class);
    assertNotNull(res);
    Assertions.assertFalse(res.isEmpty());
    assertEquals(2, res.get(0).getSchema().getEntries().size());
}
Also used : Collection(com.couchbase.client.java.Collection) JsonObject(com.couchbase.client.java.json.JsonObject) Record(org.talend.sdk.component.api.record.Record) Test(org.junit.jupiter.api.Test) CouchbaseUtilTest(org.talend.components.couchbase.CouchbaseUtilTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 93 with JsonObject

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

the class CouchbaseOutput method buildJsonObject.

private JsonObject buildJsonObject(Record rec, Map<String, String> mappings) {
    JsonObject jsonObject = JsonObject.create();
    rec.getSchema().getEntries().stream().forEach(entry -> {
        String property = mappings.getOrDefault(entry.getName(), entry.getName());
        Object value = jsonValueFromRecordValue(entry, rec);
        // need to save encoded byte array as a String
        if (value != null && value.getClass() == byte[].class) {
            value = new String((byte[]) value);
        }
        jsonObject.put(property, value);
    });
    return jsonObject;
}
Also used : JsonObject(com.couchbase.client.java.json.JsonObject) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 94 with JsonObject

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

the class CouchbaseOutput method onNext.

@ElementListener
public void onNext(@Input final Record rec) {
    if (configuration.isUseN1QLQuery()) {
        Map<String, String> mappings = configuration.getQueryParams().stream().collect(Collectors.toMap(N1QLQueryParameter::getColumn, N1QLQueryParameter::getQueryParameterName));
        JsonObject namedParams = buildJsonObject(rec, mappings);
        try {
            cluster.query(configuration.getQuery(), QueryOptions.queryOptions().parameters(namedParams));
        } catch (CouchbaseException ex) {
            log.error("N1QL failed: {}.", ex.getMessage());
            throw new ComponentException(ex.getMessage());
        }
    } else {
        if (configuration.isPartialUpdate()) {
            updatePartiallyDocument(rec);
        } else {
            if (configuration.getDataSet().getDocumentType() == DocumentType.BINARY) {
                collection.upsert(rec.getString(idFieldName), rec.getBytes(CONTENT_FIELD_NAME), UpsertOptions.upsertOptions().transcoder(RawBinaryTranscoder.INSTANCE));
            } else if (configuration.getDataSet().getDocumentType() == DocumentType.STRING) {
                collection.upsert(rec.getString(idFieldName), rec.getString(CONTENT_FIELD_NAME), UpsertOptions.upsertOptions().transcoder(RawStringTranscoder.INSTANCE));
            } else {
                collection.upsert(rec.getString(idFieldName), buildJsonObjectWithoutId(rec));
            }
        }
    }
}
Also used : CouchbaseException(com.couchbase.client.core.error.CouchbaseException) ComponentException(org.talend.sdk.component.api.exception.ComponentException) JsonObject(com.couchbase.client.java.json.JsonObject) ElementListener(org.talend.sdk.component.api.processor.ElementListener)

Example 95 with JsonObject

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

the class CouchbaseService method getSchema.

public Schema getSchema(JsonObject jsonObject, Set<String> jsonKeys) {
    Schema.Builder schemaBuilder = builderFactory.newSchemaBuilder(RECORD);
    if (jsonKeys == null || jsonKeys.isEmpty()) {
        jsonKeys = jsonObject.getNames();
    }
    for (String key : jsonKeys) {
        // receive value from JSON
        Object value = jsonObject.get(key);
        if (value == null) {
            LOG.warn(i18n.cannotGuessWhenDataIsNull());
            continue;
        }
        // With this value we can define type
        Schema.Type type = defineValueType(value);
        // We can add to schema builder entry
        Schema.Entry.Builder entryBuilder = builderFactory.newEntryBuilder();
        entryBuilder.withNullable(true).withName(key).withType(type);
        if (type == RECORD) {
            entryBuilder.withElementSchema(getSchema((JsonObject) value, null));
        } else if (type == ARRAY) {
            entryBuilder.withElementSchema(defineSchemaForArray((JsonArray) value));
        }
        Schema.Entry currentEntry = entryBuilder.build();
        schemaBuilder.withEntry(currentEntry);
    }
    return schemaBuilder.build();
}
Also used : DiscoverSchema(org.talend.sdk.component.api.service.schema.DiscoverSchema) Schema(org.talend.sdk.component.api.record.Schema) JsonObject(com.couchbase.client.java.json.JsonObject) JsonObject(com.couchbase.client.java.json.JsonObject)

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