Java Code Examples for com.couchbase.client.java.Bucket#upsert()
The following examples show how to use
com.couchbase.client.java.Bucket#upsert() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CouchbaseInputTestIT.java From components with Apache License 2.0 | 6 votes |
private void populateBucket() { CouchbaseEnvironment env = DefaultCouchbaseEnvironment .builder() .socketConnectTimeout(60000) .connectTimeout(60000) .keepAliveInterval(60000) .keyValueServiceConfig(KeyValueServiceConfig.create(60)) // If skip this config, we may get TimeoutException https://forums.couchbase.com/t/kv-upsert-throwing-timeoutexception-couchbase-4-5/9399 .build(); CouchbaseCluster cluster = CouchbaseCluster.create(env, bootstrapNodes); Bucket bucket = cluster.openBucket(bucketName, password); LOGGER.info("Connected to bucket - " + bucketName); assertTrue(bucket.bucketManager().flush()); JsonDocument document = JsonDocument.create("foo", JsonObject.create().put("bar", 42)); bucket.upsert(document, PersistTo.MASTER); bucket.close(); LOGGER.info("Bucket is closed after upserting data"); if (cluster != null) { cluster.disconnect(); } }
Example 2
Source File: CouchbaseClientTest.java From java-specialagent with Apache License 2.0 | 5 votes |
@Test public void test(final MockTracer tracer) { final Cluster cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment.builder().connectTimeout(TimeUnit.SECONDS.toMillis(60)).build()); final Bucket bucket = cluster.openBucket(bucketName); final JsonObject arthur = JsonObject.create() .put("name", "Arthur") .put("email", "[email protected]") .put("interests", JsonArray.from("Holy Grail", "African Swallows")); bucket.upsert(JsonDocument.create("u:king_arthur", arthur)); System.out.println(bucket.get("u:king_arthur")); cluster.disconnect(60, TimeUnit.SECONDS); final List<MockSpan> spans = tracer.finishedSpans(); assertEquals(6, spans.size()); boolean foundCouchbaseSpan = false; for (final MockSpan span : spans) { final String component = (String)span.tags().get(Tags.COMPONENT.getKey()); if (component != null && component.startsWith("couchbase-java-client")) { foundCouchbaseSpan = true; break; } } assertTrue("couchbase-java-client span not found", foundCouchbaseSpan); }
Example 3
Source File: CouchbaseClientITest.java From java-specialagent with Apache License 2.0 | 5 votes |
public static void main(final String[] args) throws BucketAlreadyExistsException, InterruptedException, IOException { final CouchbaseMock couchbaseMock = new CouchbaseMock("localhost", 8091, 2, 1); final BucketConfiguration bucketConfiguration = new BucketConfiguration(); bucketConfiguration.name = bucketName; bucketConfiguration.numNodes = 1; bucketConfiguration.numReplicas = 1; bucketConfiguration.password = ""; couchbaseMock.start(); couchbaseMock.waitForStartup(); couchbaseMock.createBucket(bucketConfiguration); final Cluster cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment.builder().connectTimeout(TimeUnit.SECONDS.toMillis(60)).build()); final Bucket bucket = cluster.openBucket(bucketName); final JsonObject arthur = JsonObject .create().put("name", "Arthur") .put("email", "[email protected]") .put("interests", JsonArray.from("Holy Grail", "African Swallows")); bucket.upsert(JsonDocument.create("u:king_arthur", arthur)); System.out.println(bucket.get("u:king_arthur")); cluster.disconnect(60, TimeUnit.SECONDS); couchbaseMock.stop(); TestUtil.checkSpan(new ComponentSpanCount("couchbase-java-client.*", 2)); }
Example 4
Source File: WorkloadGenerator.java From java-dcp-client with Apache License 2.0 | 5 votes |
public static void main(String... args) throws Exception { CouchbaseCluster cluster = CouchbaseCluster.create("127.0.0.1").authenticate("Administrator", "password"); Bucket bucket = cluster.openBucket("default"); while (true) { for (int i = 0; i < 1024; i++) { bucket.upsert(JsonDocument.create("doc:" + i, JsonObject.create().put("uuid", UUID.randomUUID().toString()))); Thread.sleep(1000); } } }
Example 5
Source File: CodeSnippets.java From tutorials with MIT License | 5 votes |
static JsonDocument retrieveAndUpsertExample(Bucket bucket, String id) { JsonDocument document = bucket.get(id); JsonObject content = document.content(); content.put("homeTown", "Kansas City"); JsonDocument upserted = bucket.upsert(document); return upserted; }
Example 6
Source File: N1QLLiveTest.java From tutorials with MIT License | 5 votes |
@Test public void givenDocument_whenUpsert_thenUpdate() { Bucket bucket = bucketFactory.getTravelSampleBucket(); JsonObject o2 = JsonObject.create() .put("name", "Sample Airline Updated"); bucket.upsert(JsonDocument.create("cust_1", o2)); }
Example 7
Source File: PersonServiceLiveTest.java From tutorials with MIT License | 5 votes |
@BeforeClass public static void setupBeforeClass() { final Cluster cluster = CouchbaseCluster.create(MyCouchbaseConfig.NODE_LIST); final Bucket bucket = cluster.openBucket(MyCouchbaseConfig.BUCKET_NAME, MyCouchbaseConfig.BUCKET_PASSWORD); bucket.upsert(JsonDocument.create(johnSmithId, jsonJohnSmith)); bucket.upsert(JsonDocument.create(foobarId, jsonFooBar)); bucket.close(); cluster.disconnect(); }
Example 8
Source File: StudentServiceLiveTest.java From tutorials with MIT License | 5 votes |
@BeforeClass public static void setupBeforeClass() { Cluster cluster = CouchbaseCluster.create(MyCouchbaseConfig.NODE_LIST); Bucket bucket = cluster.openBucket(MyCouchbaseConfig.BUCKET_NAME, MyCouchbaseConfig.BUCKET_PASSWORD); bucket.upsert(JsonDocument.create(joeCollegeId, jsonJoeCollege)); bucket.upsert(JsonDocument.create(judyJetsonId, jsonJudyJetson)); bucket.close(); cluster.disconnect(); }
Example 9
Source File: StudentServiceImplLiveTest.java From tutorials with MIT License | 5 votes |
@BeforeClass public static void setupBeforeClass() { Cluster cluster = CouchbaseCluster.create(MultiBucketCouchbaseConfig.NODE_LIST); Bucket bucket = cluster.openBucket(MultiBucketCouchbaseConfig.DEFAULT_BUCKET_NAME, MultiBucketCouchbaseConfig.DEFAULT_BUCKET_PASSWORD); bucket.upsert(JsonDocument.create(joeCollegeId, jsonJoeCollege)); bucket.upsert(JsonDocument.create(judyJetsonId, jsonJudyJetson)); bucket.close(); cluster.disconnect(); }
Example 10
Source File: PersonServiceImplLiveTest.java From tutorials with MIT License | 5 votes |
@BeforeClass public static void setupBeforeClass() { final Cluster cluster = CouchbaseCluster.create(MultiBucketCouchbaseConfig.NODE_LIST); final Bucket bucket = cluster.openBucket(MultiBucketCouchbaseConfig.DEFAULT_BUCKET_NAME, MultiBucketCouchbaseConfig.DEFAULT_BUCKET_PASSWORD); bucket.upsert(JsonDocument.create(johnSmithId, jsonJohnSmith)); bucket.upsert(JsonDocument.create(foobarId, jsonFooBar)); bucket.close(); cluster.disconnect(); }
Example 11
Source File: TestCouchbaseRemoteTableEndToEnd.java From samza with Apache License 2.0 | 4 votes |
@Test public void testEndToEnd() throws Exception { Bucket inputBucket = cluster.openBucket(inputBucketName); inputBucket.upsert(ByteArrayDocument.create("Alice", "20".getBytes())); inputBucket.upsert(ByteArrayDocument.create("Bob", "30".getBytes())); inputBucket.upsert(ByteArrayDocument.create("Chris", "40".getBytes())); inputBucket.upsert(ByteArrayDocument.create("David", "50".getBytes())); inputBucket.close(); String[] users = new String[]{"Alice", "Bob", "Chris", "David"}; int partitionCount = 1; Map<String, String> configs = TestLocalTableEndToEnd.getBaseJobConfig(bootstrapUrl(), zkConnect()); configs.put("streams.User.samza.system", "test"); configs.put("streams.User.source", Base64Serializer.serialize(users)); configs.put("streams.User.partitionCount", String.valueOf(partitionCount)); Config config = new MapConfig(configs); final StreamApplication app = appDesc -> { DelegatingSystemDescriptor inputSystemDescriptor = new DelegatingSystemDescriptor("test"); GenericInputDescriptor<String> inputDescriptor = inputSystemDescriptor.getInputDescriptor("User", new NoOpSerde<>()); CouchbaseTableReadFunction<String> readFunction = new CouchbaseTableReadFunction<>(inputBucketName, String.class, "couchbase://127.0.0.1") .withBootstrapCarrierDirectPort(couchbaseMock.getCarrierPort(inputBucketName)) .withBootstrapHttpDirectPort(couchbaseMock.getHttpPort()) .withSerde(new StringSerde()); CouchbaseTableWriteFunction<JsonObject> writeFunction = new CouchbaseTableWriteFunction<>(outputBucketName, JsonObject.class, "couchbase://127.0.0.1") .withBootstrapCarrierDirectPort(couchbaseMock.getCarrierPort(outputBucketName)) .withBootstrapHttpDirectPort(couchbaseMock.getHttpPort()); RemoteTableDescriptor inputTableDesc = new RemoteTableDescriptor<String, String>("input-table") .withReadFunction(readFunction) .withRateLimiterDisabled(); Table<KV<String, String>> inputTable = appDesc.getTable(inputTableDesc); RemoteTableDescriptor outputTableDesc = new RemoteTableDescriptor<String, JsonObject>("output-table") .withReadFunction(new NoOpTableReadFunction<>()) .withWriteFunction(writeFunction) .withRateLimiterDisabled(); Table<KV<String, JsonObject>> outputTable = appDesc.getTable(outputTableDesc); appDesc.getInputStream(inputDescriptor) .map(k -> KV.of(k, k)) .join(inputTable, new JoinFunction()) .sendTo(outputTable); }; final LocalApplicationRunner runner = new LocalApplicationRunner(app, config); executeRun(runner, config); runner.waitForFinish(); Bucket outputBucket = cluster.openBucket(outputBucketName); Assert.assertEquals("{\"name\":\"Alice\",\"age\":\"20\"}", outputBucket.get("Alice").content().toString()); Assert.assertEquals("{\"name\":\"Bob\",\"age\":\"30\"}", outputBucket.get("Bob").content().toString()); Assert.assertEquals("{\"name\":\"Chris\",\"age\":\"40\"}", outputBucket.get("Chris").content().toString()); Assert.assertEquals("{\"name\":\"David\",\"age\":\"50\"}", outputBucket.get("David").content().toString()); outputBucket.close(); }
Example 12
Source File: CouchbaseContainerTest.java From testcontainers-java with MIT License | 4 votes |
@Test public void testBasicContainerUsage() { // bucket_definition { BucketDefinition bucketDefinition = new BucketDefinition("mybucket"); // } try ( // container_definition { CouchbaseContainer container = new CouchbaseContainer() .withBucket(bucketDefinition) // } ) { container.start(); // cluster_creation { CouchbaseEnvironment environment = DefaultCouchbaseEnvironment .builder() .bootstrapCarrierDirectPort(container.getBootstrapCarrierDirectPort()) .bootstrapHttpDirectPort(container.getBootstrapHttpDirectPort()) .build(); Cluster cluster = CouchbaseCluster.create( environment, container.getHost() ); // } try { // auth { cluster.authenticate(container.getUsername(), container.getPassword()); // } Bucket bucket = cluster.openBucket(bucketDefinition.getName()); bucket.upsert(JsonDocument.create("foo", JsonObject.empty())); assertTrue(bucket.exists("foo")); assertNotNull(cluster.clusterManager().getBucket(bucketDefinition.getName())); } finally { cluster.disconnect(); environment.shutdown(); } } }
Example 13
Source File: PutCouchbaseKey.java From nifi with Apache License 2.0 | 4 votes |
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { final ComponentLog logger = getLogger(); FlowFile flowFile = session.get(); if (flowFile == null) { return; } final byte[] content = new byte[(int) flowFile.getSize()]; session.read(flowFile, new InputStreamCallback() { @Override public void process(final InputStream in) throws IOException { StreamUtils.fillBuffer(in, content, true); } }); String docId = flowFile.getAttribute(CoreAttributes.UUID.key()); if (context.getProperty(DOC_ID).isSet()) { docId = context.getProperty(DOC_ID).evaluateAttributeExpressions(flowFile).getValue(); } try { Document<?> doc = null; final DocumentType documentType = DocumentType.valueOf(context.getProperty(DOCUMENT_TYPE).getValue()); switch (documentType) { case Json: { doc = RawJsonDocument.create(docId, new String(content, StandardCharsets.UTF_8)); break; } case Binary: { doc = ByteArrayDocument.create(docId, content); break; } } final PersistTo persistTo = PersistTo.valueOf(context.getProperty(PERSIST_TO).getValue()); final ReplicateTo replicateTo = ReplicateTo.valueOf(context.getProperty(REPLICATE_TO).getValue()); final Bucket bucket = openBucket(context); doc = bucket.upsert(doc, persistTo, replicateTo); final Map<String, String> updatedAttrs = new HashMap<>(); updatedAttrs.put(CouchbaseAttributes.Cluster.key(), context.getProperty(COUCHBASE_CLUSTER_SERVICE).getValue()); updatedAttrs.put(CouchbaseAttributes.Bucket.key(), bucket.name()); updatedAttrs.put(CouchbaseAttributes.DocId.key(), docId); updatedAttrs.put(CouchbaseAttributes.Cas.key(), String.valueOf(doc.cas())); updatedAttrs.put(CouchbaseAttributes.Expiry.key(), String.valueOf(doc.expiry())); flowFile = session.putAllAttributes(flowFile, updatedAttrs); session.getProvenanceReporter().send(flowFile, getTransitUrl(bucket, docId)); session.transfer(flowFile, REL_SUCCESS); } catch (final CouchbaseException e) { String errMsg = String.format("Writing document %s to Couchbase Server using %s failed due to %s", docId, flowFile, e); handleCouchbaseException(context, session, logger, flowFile, e, errMsg); } }
Example 14
Source File: TestCouchbaseUtils.java From nifi with Apache License 2.0 | 4 votes |
@Ignore("This test method requires a live Couchbase Server instance") @Test public void testDocumentTypesAndStringConversion() { final CouchbaseCluster cluster = CouchbaseCluster.fromConnectionString("couchbase://192.168.99.100:8091"); final Bucket bucket = cluster.openBucket("b1", "b1password"); bucket.upsert(JsonDocument.create("JsonDocument", JsonObject.create().put("one", 1))); bucket.upsert(JsonArrayDocument.create("JsonArray", JsonArray.create().add(1).add(2).add(3))); bucket.upsert(JsonDoubleDocument.create("JsonDouble", 0.123)); bucket.upsert(JsonStringDocument.create("JsonString", "value")); bucket.upsert(JsonBooleanDocument.create("JsonBoolean", true)); bucket.upsert(JsonLongDocument.create("JsonLong", 123L)); bucket.upsert(RawJsonDocument.create("RawJsonDocument", "value")); bucket.upsert(StringDocument.create("StringDocument", "value")); bucket.upsert(BinaryDocument.create("BinaryDocument", Unpooled.copiedBuffer("value".getBytes(StandardCharsets.UTF_8)))); bucket.upsert(ByteArrayDocument.create("ByteArrayDocument", "value".getBytes(StandardCharsets.UTF_8))); final String[][] expectations = { {"JsonDocument", "String", "{\"one\":1}"}, {"JsonArray", "String", "[1,2,3]"}, {"JsonDouble", "String", "0.123"}, {"JsonString", "String", "\"value\""}, {"JsonBoolean", "String", "true"}, {"JsonLong", "String", "123"}, {"RawJsonDocument", "String", "value"}, {"StringDocument", "String", "value"}, {"BinaryDocument", "byte[]", "value"}, {"ByteArrayDocument", "byte[]", "value"}, }; for (String[] expectation : expectations) { final LegacyDocument document = bucket.get(LegacyDocument.create(expectation[0])); assertEquals(expectation[1], document.content().getClass().getSimpleName()); assertEquals(expectation[2], CouchbaseUtils.getStringContent(document.content())); } final BinaryDocument binaryDocument = bucket.get(BinaryDocument.create("BinaryDocument")); final String stringFromByteBuff = CouchbaseUtils.getStringContent(binaryDocument.content()); assertEquals("value", stringFromByteBuff); try { bucket.get(BinaryDocument.create("JsonDocument")); fail("Getting a JSON document as a BinaryDocument fails"); } catch (TranscodingException e) { assertTrue(e.getMessage().contains("Flags (0x2000000) indicate non-binary document for id JsonDocument")); } }