Java Code Examples for com.couchbase.client.java.Bucket#close()

The following examples show how to use com.couchbase.client.java.Bucket#close() . 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 vote down vote up
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: CouchbaseClient.java    From nosql4idea with Apache License 2.0 6 votes vote down vote up
@Override
public void connect(ServerConfiguration serverConfiguration) {
    CouchbaseCluster cluster = CouchbaseCluster.create(serverConfiguration.getServerUrl());
    String userDatabase = serverConfiguration.getUserDatabase();
    Bucket bucket = null;
    try {
        if (StringUtils.isEmpty(userDatabase)) {
            bucket = cluster.openBucket();
        } else {
            bucket = cluster.openBucket(userDatabase);
        }

    } catch (Exception ex) {
        throw new ConfigurationException(ex);
    } finally {
        if (bucket != null) {
            bucket.close();
        }
        cluster.disconnect();
    }

}
 
Example 3
Source File: PersonServiceLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@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 4
Source File: StudentServiceLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@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 5
Source File: StudentServiceImplLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@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 6
Source File: PersonServiceImplLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@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 7
Source File: TestCouchbaseRemoteTableEndToEnd.java    From samza with Apache License 2.0 4 votes vote down vote up
@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();
}