Java Code Examples for com.couchbase.client.java.CouchbaseCluster#openBucket()

The following examples show how to use com.couchbase.client.java.CouchbaseCluster#openBucket() . 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: CouchbasePipeline.java    From NetDiscovery with Apache License 2.0 5 votes vote down vote up
public CouchbasePipeline(CouchbaseCluster cluster, String bucketName, String documentId, long pipelineDelay){

        super(pipelineDelay);
        this.cluster = cluster;
        this.bucket = cluster.openBucket(bucketName);
        this.documentId = documentId;
    }
 
Example 4
Source File: WorkloadGenerator.java    From java-dcp-client with Apache License 2.0 5 votes vote down vote up
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: CouchbaseTestServer.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@Test
public static void testServer()
    throws InterruptedException, IOException {
  CouchbaseTestServer couchbaseTestServer = new CouchbaseTestServer(TestUtils.findFreePort());
  couchbaseTestServer.start();

  int port = couchbaseTestServer.getPort();
  int serverPort = couchbaseTestServer.getServerPort();



  try {
    CouchbaseEnvironment cbEnv = DefaultCouchbaseEnvironment.builder().bootstrapHttpEnabled(true)
        .bootstrapHttpDirectPort(port)
        .bootstrapCarrierDirectPort(serverPort)
        .connectTimeout(TimeUnit.SECONDS.toMillis(15))
        .bootstrapCarrierEnabled(true).build();
    CouchbaseCluster cbCluster = CouchbaseCluster.create(cbEnv, "localhost");
    Bucket bucket = cbCluster.openBucket("default","");
    try {
      JsonObject content = JsonObject.empty().put("name", "Michael");
      JsonDocument doc = JsonDocument.create("docId", content);
      JsonDocument inserted = bucket.insert(doc);
    }
    catch (Exception e)
    {
      Assert.fail("Should not throw exception on insert", e);
    }
  }
  finally
  {
    couchbaseTestServer.stop();
  }
}
 
Example 6
Source File: TestCouchbaseUtils.java    From nifi with Apache License 2.0 4 votes vote down vote up
@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"));
    }

}