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