Search in sources :

Example 1 with CouchbaseClient

use of com.couchbase.client.CouchbaseClient in project javaee7-samples by javaee-samples.

the class PersonSessionBean method initDB.

@PostConstruct
private void initDB() {
    try {
        // Get an instance of Couchbase
        List<URI> hosts = Arrays.asList(new URI("http://localhost:8091/pools"));
        // Get an instance of Couchbase
        // Name of the Bucket to connect to
        String bucket = "default";
        // Password of the bucket (empty) string if none
        String password = "";
        // Connect to the Cluster
        client = new CouchbaseClient(hosts, bucket, password);
    } catch (URISyntaxException | IOException ex) {
        Logger.getLogger(PersonSessionBean.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) CouchbaseClient(com.couchbase.client.CouchbaseClient) PostConstruct(javax.annotation.PostConstruct)

Example 2 with CouchbaseClient

use of com.couchbase.client.CouchbaseClient in project camel by apache.

the class CouchbaseEndpoint method createClient.

private CouchbaseClient createClient() throws IOException, URISyntaxException {
    List<URI> hosts = Arrays.asList(makeBootstrapURI());
    CouchbaseConnectionFactoryBuilder cfb = new CouchbaseConnectionFactoryBuilder();
    if (opTimeOut != DEFAULT_OP_TIMEOUT) {
        cfb.setOpTimeout(opTimeOut);
    }
    if (timeoutExceptionThreshold != DEFAULT_TIMEOUT_EXCEPTION_THRESHOLD) {
        cfb.setTimeoutExceptionThreshold(timeoutExceptionThreshold);
    }
    if (readBufferSize != DEFAULT_READ_BUFFER_SIZE) {
        cfb.setReadBufferSize(readBufferSize);
    }
    if (shouldOptimize) {
        cfb.setShouldOptimize(true);
    }
    if (maxReconnectDelay != DEFAULT_MAX_RECONNECT_DELAY) {
        cfb.setMaxReconnectDelay(maxReconnectDelay);
    }
    if (opQueueMaxBlockTime != DEFAULT_OP_QUEUE_MAX_BLOCK_TIME) {
        cfb.setOpQueueMaxBlockTime(opQueueMaxBlockTime);
    }
    if (obsPollInterval != DEFAULT_OBS_POLL_INTERVAL) {
        cfb.setObsPollInterval(obsPollInterval);
    }
    if (obsTimeout != DEFAULT_OBS_TIMEOUT) {
        cfb.setObsTimeout(obsTimeout);
    }
    return new CouchbaseClient(cfb.buildCouchbaseConnection(hosts, bucket, username, password));
}
Also used : CouchbaseConnectionFactoryBuilder(com.couchbase.client.CouchbaseConnectionFactoryBuilder) URI(java.net.URI) CouchbaseClient(com.couchbase.client.CouchbaseClient)

Example 3 with CouchbaseClient

use of com.couchbase.client.CouchbaseClient in project apex-malhar by apache.

the class CouchBaseStore method connectServer.

public CouchbaseClient connectServer(String urlString) throws IOException {
    ArrayList<URI> nodes = new ArrayList<URI>();
    CouchbaseClient clientPartition = null;
    try {
        nodes.add(new URI("http", urlString, "/pools", null, null));
    } catch (URISyntaxException ex) {
        DTThrowable.rethrow(ex);
    }
    try {
        clientPartition = new CouchbaseClient(nodes, bucket, password);
    } catch (IOException e) {
        logger.error("Error connecting to Couchbase:", e);
        DTThrowable.rethrow(e);
    }
    return clientPartition;
}
Also used : ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) CouchbaseClient(com.couchbase.client.CouchbaseClient)

Example 4 with CouchbaseClient

use of com.couchbase.client.CouchbaseClient in project apex-malhar by apache.

the class CouchBaseStore method connect.

@Override
public void connect() throws IOException {
    String[] tokens = uriString.split(",");
    URI uri = null;
    for (String url : tokens) {
        try {
            uri = new URI("http", url, "/pools", null, null);
        } catch (URISyntaxException ex) {
            DTThrowable.rethrow(ex);
        }
        baseURIs.add(uri);
    }
    try {
        CouchbaseConnectionFactoryBuilder cfb = new CouchbaseConnectionFactoryBuilder();
        // wait up to 10 seconds for an operation to succeed
        cfb.setOpTimeout(timeout);
        // wait up to 10 second when trying to enqueue an operation
        cfb.setOpQueueMaxBlockTime(blockTime);
        client = new CouchbaseClient(cfb.buildCouchbaseConnection(baseURIs, bucket, password));
    // client = new CouchbaseClient(baseURIs, "default", "");
    } catch (IOException e) {
        logger.error("Error connecting to Couchbase:", e);
        DTThrowable.rethrow(e);
    }
}
Also used : CouchbaseConnectionFactoryBuilder(com.couchbase.client.CouchbaseConnectionFactoryBuilder) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) CouchbaseClient(com.couchbase.client.CouchbaseClient)

Example 5 with CouchbaseClient

use of com.couchbase.client.CouchbaseClient in project apex-malhar by apache.

the class CouchBaseInputOperatorTest method TestCouchBaseInputOperator.

@Test
public void TestCouchBaseInputOperator() throws Exception {
    BucketConfiguration bucketConfiguration = new BucketConfiguration();
    CouchbaseConnectionFactoryBuilder cfb = new CouchbaseConnectionFactoryBuilder();
    CouchbaseMock mockCouchbase1 = createMock("default", "", bucketConfiguration);
    CouchbaseMock mockCouchbase2 = createMock("default", "", bucketConfiguration);
    mockCouchbase1.start();
    mockCouchbase1.waitForStartup();
    List<URI> uriList = new ArrayList<URI>();
    int port1 = mockCouchbase1.getHttpPort();
    logger.debug("port is {}", port1);
    mockCouchbase2.start();
    mockCouchbase2.waitForStartup();
    int port2 = mockCouchbase2.getHttpPort();
    logger.debug("port is {}", port2);
    uriList.add(new URI("http", null, "localhost", port1, "/pools", "", ""));
    connectionFactory = cfb.buildCouchbaseConnection(uriList, bucketConfiguration.name, bucketConfiguration.password);
    client = new CouchbaseClient(connectionFactory);
    CouchBaseStore store = new CouchBaseStore();
    keyList = new ArrayList<String>();
    store.setBucket(bucketConfiguration.name);
    store.setPasswordConfig(password);
    store.setPassword(bucketConfiguration.password);
    store.setUriString("localhost:" + port1 + "," + "localhost:" + port1);
    // couchbaseBucket.getCouchServers();
    AttributeMap.DefaultAttributeMap attributeMap = new AttributeMap.DefaultAttributeMap();
    attributeMap.put(DAG.APPLICATION_ID, APP_ID);
    TestInputOperator inputOperator = new TestInputOperator();
    inputOperator.setStore(store);
    inputOperator.insertEventsInTable(10);
    CollectorTestSink<Object> sink = new CollectorTestSink<Object>();
    inputOperator.outputPort.setSink(sink);
    List<Partition<AbstractCouchBaseInputOperator<String>>> partitions = Lists.newArrayList();
    Collection<Partition<AbstractCouchBaseInputOperator<String>>> newPartitions = inputOperator.definePartitions(partitions, new PartitioningContextImpl(null, 0));
    Assert.assertEquals(2, newPartitions.size());
    for (Partition<AbstractCouchBaseInputOperator<String>> p : newPartitions) {
        Assert.assertNotSame(inputOperator, p.getPartitionedInstance());
    }
    // Collect all operators in a list
    List<AbstractCouchBaseInputOperator<String>> opers = Lists.newArrayList();
    for (Partition<AbstractCouchBaseInputOperator<String>> p : newPartitions) {
        TestInputOperator oi = (TestInputOperator) p.getPartitionedInstance();
        oi.setServerURIString("localhost:" + port1);
        oi.setStore(store);
        oi.setup(null);
        oi.outputPort.setSink(sink);
        opers.add(oi);
        port1 = port2;
    }
    sink.clear();
    int wid = 0;
    for (int i = 0; i < 10; i++) {
        for (AbstractCouchBaseInputOperator<String> o : opers) {
            o.beginWindow(wid);
            o.emitTuples();
            o.endWindow();
        }
        wid++;
    }
    Assert.assertEquals("Tuples read should be same ", 10, sink.collectedTuples.size());
    for (AbstractCouchBaseInputOperator<String> o : opers) {
        o.teardown();
    }
    mockCouchbase1.stop();
    mockCouchbase2.stop();
}
Also used : CouchbaseConnectionFactoryBuilder(com.couchbase.client.CouchbaseConnectionFactoryBuilder) ArrayList(java.util.ArrayList) URI(java.net.URI) CouchbaseClient(com.couchbase.client.CouchbaseClient) Partition(com.datatorrent.api.Partitioner.Partition) CouchbaseMock(org.couchbase.mock.CouchbaseMock) BucketConfiguration(org.couchbase.mock.BucketConfiguration) AttributeMap(com.datatorrent.api.Attribute.AttributeMap) PartitioningContextImpl(org.apache.apex.malhar.lib.partitioner.StatelessPartitionerTest.PartitioningContextImpl) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Aggregations

CouchbaseClient (com.couchbase.client.CouchbaseClient)8 URI (java.net.URI)7 CouchbaseConnectionFactoryBuilder (com.couchbase.client.CouchbaseConnectionFactoryBuilder)5 IOException (java.io.IOException)5 URISyntaxException (java.net.URISyntaxException)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 AttributeMap (com.datatorrent.api.Attribute.AttributeMap)1 Partition (com.datatorrent.api.Partitioner.Partition)1 HashMap (java.util.HashMap)1 PostConstruct (javax.annotation.PostConstruct)1 PartitioningContextImpl (org.apache.apex.malhar.lib.partitioner.StatelessPartitionerTest.PartitioningContextImpl)1 CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)1 BucketConfiguration (org.couchbase.mock.BucketConfiguration)1 CouchbaseMock (org.couchbase.mock.CouchbaseMock)1