Search in sources :

Example 1 with CouchbaseCluster

use of com.couchbase.client.java.CouchbaseCluster in project incubator-gobblin by apache.

the class CouchbaseTestServer method testServer.

@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();
    }
}
Also used : Bucket(com.couchbase.client.java.Bucket) CouchbaseCluster(com.couchbase.client.java.CouchbaseCluster) JsonObject(com.couchbase.client.java.document.json.JsonObject) CouchbaseEnvironment(com.couchbase.client.java.env.CouchbaseEnvironment) DefaultCouchbaseEnvironment(com.couchbase.client.java.env.DefaultCouchbaseEnvironment) JsonDocument(com.couchbase.client.java.document.JsonDocument) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 2 with CouchbaseCluster

use of com.couchbase.client.java.CouchbaseCluster in project components by Talend.

the class CouchbaseInputTestIT method populateBucket.

private void populateBucket() {
    CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder().socketConnectTimeout(60000).connectTimeout(60000).keepAliveInterval(60000).build();
    CouchbaseCluster cluster = CouchbaseCluster.create(env, bootstrapNodes);
    Bucket bucket = cluster.openBucket(bucketName, password);
    assertTrue(bucket.bucketManager().flush());
    JsonDocument document = JsonDocument.create("foo", JsonObject.create().put("bar", 42));
    bucket.upsert(document, PersistTo.MASTER);
    bucket.close();
    cluster.disconnect();
}
Also used : Bucket(com.couchbase.client.java.Bucket) CouchbaseCluster(com.couchbase.client.java.CouchbaseCluster) CouchbaseEnvironment(com.couchbase.client.java.env.CouchbaseEnvironment) DefaultCouchbaseEnvironment(com.couchbase.client.java.env.DefaultCouchbaseEnvironment) JsonDocument(com.couchbase.client.java.document.JsonDocument)

Example 3 with CouchbaseCluster

use of com.couchbase.client.java.CouchbaseCluster in project jnosql-diana-driver by eclipse.

the class CouchbaseKeyValueConfiguration method getManagerFactory.

/**
 * Creates a {@link DefaultCouchbaseBucketManagerFactory} from {@link CouchbaseEnvironment}
 *
 * @param environment the {@link CouchbaseEnvironment}
 * @return the new {@link DefaultCouchbaseBucketManagerFactory} instance
 * @throws NullPointerException when environment is null
 */
public CouchbaseBucketManagerFactory getManagerFactory(CouchbaseEnvironment environment) throws NullPointerException {
    Objects.requireNonNull(environment, "environment is required");
    CouchbaseCluster couchbaseCluster = CouchbaseCluster.create(environment, nodes);
    return new DefaultCouchbaseBucketManagerFactory(couchbaseCluster, user, password);
}
Also used : CouchbaseCluster(com.couchbase.client.java.CouchbaseCluster)

Example 4 with CouchbaseCluster

use of com.couchbase.client.java.CouchbaseCluster in project samza by apache.

the class TestCouchbaseBucketRegistry method testCloseTwoBucketsInSameCluster.

/**
 * This unit test simulates closing two buckets within one cluster. The cluster should only be disconnected when all
 * buckets has been closed.
 */
@Test
public void testCloseTwoBucketsInSameCluster() {
    String bucketName1 = "bucket1";
    String bucketName2 = "bucket2";
    List<String> clusterNodes = Arrays.asList("cluster");
    CouchbaseEnvironmentConfigs configs = new CouchbaseEnvironmentConfigs();
    CouchbaseCluster cluster = mock(CouchbaseCluster.class);
    Bucket bucket1 = mock(Bucket.class);
    Bucket bucket2 = mock(Bucket.class);
    when(bucket1.close()).thenReturn(true).thenReturn(false);
    when(bucket2.close()).thenReturn(true).thenReturn(false);
    when(cluster.openBucket(bucketName1)).thenReturn(bucket1);
    when(cluster.openBucket(bucketName2)).thenReturn(bucket2);
    when(cluster.disconnect()).thenReturn(true).thenReturn(false);
    mockStatic(CouchbaseCluster.class);
    when(CouchbaseCluster.create(any(CouchbaseEnvironment.class), eq(clusterNodes))).thenReturn(cluster);
    CouchbaseBucketRegistry registry = new CouchbaseBucketRegistry();
    registry.getBucket(bucketName1, clusterNodes, configs);
    registry.getBucket(bucketName2, clusterNodes, configs);
    assertTrue(registry.closeBucket(bucketName1, clusterNodes));
    assertTrue(registry.closeBucket(bucketName2, clusterNodes));
    // Cluster should have been disconnected. Should return false.
    assertFalse(cluster.disconnect());
    // Buckets should have been closed. Should return false.
    assertFalse(cluster.disconnect());
}
Also used : Bucket(com.couchbase.client.java.Bucket) CouchbaseCluster(com.couchbase.client.java.CouchbaseCluster) CouchbaseEnvironment(com.couchbase.client.java.env.CouchbaseEnvironment) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with CouchbaseCluster

use of com.couchbase.client.java.CouchbaseCluster in project jnosql-diana-driver by eclipse.

the class CouchbaseClusterUtil method getCouchbaseCluster.

public static CouchbaseCluster getCouchbaseCluster(String database, CouchbaseCluster couchbaseCluster, String user, String password) {
    Objects.requireNonNull(database, "database is required");
    CouchbaseCluster authenticate = couchbaseCluster.authenticate(user, password);
    /*
        ClusterManager clusterManager = authenticate.clusterManager();
        List<BucketSettings> buckets = clusterManager.getBuckets();
        if(buckets.stream().noneMatch(b -> b.name().equals(database))) {
            BucketSettings bucketSettings =  DefaultBucketSettings.builder().name(database).quota(100);
            clusterManager.insertBucket(bucketSettings);
            Bucket bucket = authenticate.openBucket(database);
            bucket.query(N1qlQuery.simple("CREATE PRIMARY INDEX index_" + database + " on " + database));
            bucket.close();
        }*/
    return authenticate;
}
Also used : CouchbaseCluster(com.couchbase.client.java.CouchbaseCluster)

Aggregations

CouchbaseCluster (com.couchbase.client.java.CouchbaseCluster)9 Bucket (com.couchbase.client.java.Bucket)6 CouchbaseEnvironment (com.couchbase.client.java.env.CouchbaseEnvironment)6 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 JsonDocument (com.couchbase.client.java.document.JsonDocument)2 DefaultCouchbaseEnvironment (com.couchbase.client.java.env.DefaultCouchbaseEnvironment)2 JsonObject (com.couchbase.client.java.document.json.JsonObject)1 IOException (java.io.IOException)1 Test (org.testng.annotations.Test)1