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