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));
}
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);
}
}
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;
}
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();
}
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;
}
Aggregations