Search in sources :

Example 81 with JsonObject

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

the class CouchbaseOutputTest method simpleOutputTest.

@Test
@DisplayName("Check fields from retrieved data")
void simpleOutputTest() {
    log.info("Test start: simpleOutputTest");
    final String SIMPLE_OUTPUT_TEST_ID = "simpleOutputTest";
    List<Record> records = createRecords(new TestData(), SIMPLE_OUTPUT_TEST_ID);
    componentsHandler.setInputData(records);
    executeJob(getOutputConfiguration());
    List<JsonObject> resultList = retrieveDataFromDatabase(SIMPLE_OUTPUT_TEST_ID, 2);
    assertEquals(2, resultList.size());
    assertJsonEquals(new TestData(), resultList.get(0));
}
Also used : TestData(org.talend.components.couchbase.TestData) 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 82 with JsonObject

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

the class CouchbaseOutputTest method executeSimpleN1QLQueryWithParameters.

@Test
@DisplayName("N1QL query with parameters")
void executeSimpleN1QLQueryWithParameters() {
    log.info("Test start: executeSimpleN1QLQueryWithParameters");
    final String N1QL_WITH_PARAMETERS_ID_PREFIX = "n1qlWithParametersIdPrefix";
    CouchbaseOutputConfiguration configuration = getOutputConfiguration();
    configuration.setUseN1QLQuery(true);
    String js = new TestData().createParameterizedJsonString();
    String qry = String.format("INSERT INTO `%s` (KEY, VALUE) VALUES ($t_string, " + js + ")", BUCKET_NAME);
    configuration.setQuery(qry);
    List<N1QLQueryParameter> params = new ArrayList<>();
    params.add(new N1QLQueryParameter("$t_string", "t_string"));
    params.add(new N1QLQueryParameter("$t_int_min", "t_int_min"));
    params.add(new N1QLQueryParameter("$t_int_max", "t_int_max"));
    params.add(new N1QLQueryParameter("$t_long_min", "t_long_min"));
    params.add(new N1QLQueryParameter("$t_long_max", "t_long_max"));
    params.add(new N1QLQueryParameter("$t_float_min", "t_float_min"));
    params.add(new N1QLQueryParameter("$t_float_max", "t_float_max"));
    params.add(new N1QLQueryParameter("$t_double_min", "t_double_min"));
    params.add(new N1QLQueryParameter("$t_double_max", "t_double_max"));
    params.add(new N1QLQueryParameter("$t_boolean", "t_boolean"));
    params.add(new N1QLQueryParameter("$t_datetime", "t_datetime"));
    params.add(new N1QLQueryParameter("$t_array", "t_array"));
    configuration.setQueryParams(params);
    TestData td = new TestData();
    td.setColDoubleMax(Integer.MAX_VALUE);
    td.setColFloatMax(Integer.MAX_VALUE);
    td.setColLongMax(Integer.MAX_VALUE);
    componentsHandler.setInputData(createRecords(td, N1QL_WITH_PARAMETERS_ID_PREFIX));
    executeJob(configuration);
    List<JsonObject> resultList = retrieveDataFromDatabase(N1QL_WITH_PARAMETERS_ID_PREFIX, 2);
    assertEquals(2, resultList.size());
    for (JsonObject json : resultList) {
        assertJsonEquals(td, json);
    }
}
Also used : TestData(org.talend.components.couchbase.TestData) ArrayList(java.util.ArrayList) JsonObject(com.couchbase.client.java.json.JsonObject) Test(org.junit.jupiter.api.Test) CouchbaseUtilTest(org.talend.components.couchbase.CouchbaseUtilTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 83 with JsonObject

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

the class CouchbaseOutputTest method retrieveDataFromDatabase.

private List<JsonObject> retrieveDataFromDatabase(String prefix, int count) {
    List<JsonObject> resultList = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        JsonObject jsonObject = couchbaseCluster.bucket(BUCKET_NAME).defaultCollection().get(generateDocId(prefix, i)).contentAsObject();
        jsonObject.put(META_ID_FIELD, generateDocId(prefix, i));
        resultList.add(jsonObject);
    }
    return resultList;
}
Also used : ArrayList(java.util.ArrayList) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 84 with JsonObject

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

the class CouchbaseService method defineSchemaForArray.

private Schema defineSchemaForArray(JsonArray jsonArray) {
    Object firstValueInArray = jsonArray.get(0);
    Schema.Builder schemaBuilder = builderFactory.newSchemaBuilder(RECORD);
    if (firstValueInArray == null) {
        throw new ComponentException("First value of Array is null. Can't define type of values in array");
    }
    Schema.Type type = defineValueType(firstValueInArray);
    schemaBuilder.withType(type);
    if (type == RECORD) {
        schemaBuilder.withEntry(builderFactory.newEntryBuilder().withElementSchema(getSchema((JsonObject) firstValueInArray, null)).build());
    } else if (type == ARRAY) {
        schemaBuilder.withEntry(builderFactory.newEntryBuilder().withElementSchema(defineSchemaForArray((JsonArray) firstValueInArray)).build());
    }
    return schemaBuilder.withType(type).build();
}
Also used : DiscoverSchema(org.talend.sdk.component.api.service.schema.DiscoverSchema) Schema(org.talend.sdk.component.api.record.Schema) ComponentException(org.talend.sdk.component.api.exception.ComponentException) JsonObject(com.couchbase.client.java.json.JsonObject) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 85 with JsonObject

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

the class CouchbaseInput method next.

@Producer
public Record next() {
    // loop to find first document with appropriate type (for non-json documents)
    while (queryResultsIterator.hasNext()) {
        JsonObject jsonObject = queryResultsIterator.next();
        if (configuration.getDataSet().getDocumentType() == DocumentType.JSON) {
            try {
                return createJsonRecord(jsonObject);
            } catch (ClassCastException e) {
            // document is a non-json, try to get next document
            }
        } else {
            String id = jsonObject.getString(META_ID_FIELD);
            if (id == null) {
                LOG.error("Cannot find '_meta_id_' field. The query should contain 'meta().id as _meta_id_' field");
                return null;
            }
            DocumentParser documentParser = ParserFactory.createDocumentParser(configuration.getDataSet().getDocumentType(), builderFactory);
            return documentParser.parse(collection, id);
        }
    }
    return null;
}
Also used : DocumentParser(org.talend.components.couchbase.source.parsers.DocumentParser) JsonObject(com.couchbase.client.java.json.JsonObject) Producer(org.talend.sdk.component.api.input.Producer)

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