Search in sources :

Example 41 with Bucket

use of com.couchbase.client.java.Bucket in project sling by apache.

the class CouchbaseNoSqlAdapter method getChildren.

@Override
public Iterator<NoSqlData> getChildren(String parentPath) {
    Bucket bucket = couchbaseClient.getBucket();
    // fetch all direct children of this path
    N1qlQuery query = N1qlQuery.simple(select("*").from(couchbaseClient.getBucketName()).where(x(PN_PARENT_PATH).eq(s(parentPath))), N1QL_PARAMS);
    N1qlQueryResult queryResult = bucket.query(query);
    handleQueryError(queryResult);
    final Iterator<N1qlQueryRow> results = queryResult.iterator();
    return new Iterator<NoSqlData>() {

        @Override
        public boolean hasNext() {
            return results.hasNext();
        }

        @Override
        public NoSqlData next() {
            JsonObject item = results.next().value();
            JsonObject envelope = item.getObject(couchbaseClient.getBucketName());
            String path = envelope.getString(PN_PATH);
            JsonObject data = envelope.getObject(PN_DATA);
            return new NoSqlData(path, data.toMap(), MultiValueMode.LISTS);
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : N1qlQueryRow(com.couchbase.client.java.query.N1qlQueryRow) NoSqlData(org.apache.sling.nosql.generic.adapter.NoSqlData) N1qlQuery(com.couchbase.client.java.query.N1qlQuery) Bucket(com.couchbase.client.java.Bucket) Iterator(java.util.Iterator) JsonObject(com.couchbase.client.java.document.json.JsonObject) N1qlQueryResult(com.couchbase.client.java.query.N1qlQueryResult)

Example 42 with Bucket

use of com.couchbase.client.java.Bucket in project sling by apache.

the class CouchbaseNoSqlAdapter method get.

@Override
public NoSqlData get(String path) {
    Bucket bucket = couchbaseClient.getBucket();
    String cacheKey = CouchbaseKey.build(path, cacheKeyPrefix);
    JsonDocument doc = bucket.get(cacheKey);
    if (doc == null) {
        return null;
    } else {
        JsonObject data = doc.content().getObject(PN_DATA);
        if (data == null) {
            return null;
        } else {
            return new NoSqlData(path, data.toMap(), MultiValueMode.LISTS);
        }
    }
}
Also used : NoSqlData(org.apache.sling.nosql.generic.adapter.NoSqlData) Bucket(com.couchbase.client.java.Bucket) JsonObject(com.couchbase.client.java.document.json.JsonObject) JsonDocument(com.couchbase.client.java.document.JsonDocument)

Example 43 with Bucket

use of com.couchbase.client.java.Bucket in project sling by apache.

the class CouchbaseNoSqlAdapter method checkConnection.

@Override
public void checkConnection() throws LoginException {
    // try to access root element to check connection
    try {
        Bucket bucket = couchbaseClient.getBucket();
        String cacheKey = CouchbaseKey.build("/", cacheKeyPrefix);
        bucket.exists(cacheKey);
    } catch (Throwable ex) {
        throw new LoginException(ex);
    }
}
Also used : Bucket(com.couchbase.client.java.Bucket) LoginException(org.apache.sling.api.resource.LoginException)

Example 44 with Bucket

use of com.couchbase.client.java.Bucket in project sling by apache.

the class CouchbaseNoSqlAdapter method deleteRecursive.

@Override
public boolean deleteRecursive(String path) {
    Bucket bucket = couchbaseClient.getBucket();
    // fetch all descendants and self for deletion
    Pattern descendantsAndSelf = Pattern.compile("^" + path + "(/.+)?$");
    N1qlQuery query = N1qlQuery.simple(select("*").from(couchbaseClient.getBucketName()).where("REGEXP_LIKE(`" + PN_PATH + "`, '" + descendantsAndSelf.pattern() + "')"), N1QL_PARAMS);
    N1qlQueryResult queryResult = bucket.query(query);
    handleQueryError(queryResult);
    final Iterator<N1qlQueryRow> results = queryResult.iterator();
    boolean deletedAny = false;
    while (results.hasNext()) {
        JsonObject item = results.next().value();
        JsonObject envelope = item.getObject(couchbaseClient.getBucketName());
        String itemPath = envelope.getString(PN_PATH);
        String itemCacheKey = CouchbaseKey.build(itemPath, cacheKeyPrefix);
        bucket.remove(itemCacheKey);
        deletedAny = true;
    }
    return deletedAny;
}
Also used : N1qlQueryRow(com.couchbase.client.java.query.N1qlQueryRow) Pattern(java.util.regex.Pattern) N1qlQuery(com.couchbase.client.java.query.N1qlQuery) Bucket(com.couchbase.client.java.Bucket) JsonObject(com.couchbase.client.java.document.json.JsonObject) N1qlQueryResult(com.couchbase.client.java.query.N1qlQueryResult)

Example 45 with Bucket

use of com.couchbase.client.java.Bucket in project nifi by apache.

the class GetCouchbaseKey method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile inFile = session.get();
    if (inFile == null) {
        return;
    }
    final long startNanos = System.nanoTime();
    final ComponentLog logger = getLogger();
    String docId = null;
    if (!StringUtils.isEmpty(context.getProperty(DOC_ID).getValue())) {
        docId = context.getProperty(DOC_ID).evaluateAttributeExpressions(inFile).getValue();
    } else {
        final byte[] content = new byte[(int) inFile.getSize()];
        session.read(inFile, new InputStreamCallback() {

            @Override
            public void process(final InputStream in) throws IOException {
                StreamUtils.fillBuffer(in, content, true);
            }
        });
        docId = new String(content, StandardCharsets.UTF_8);
    }
    if (StringUtils.isEmpty(docId)) {
        throw new ProcessException("Please check 'Document Id' setting. Couldn't get document id from " + inFile);
    }
    try {
        final Document<?> doc;
        final byte[] content;
        final Bucket bucket = openBucket(context);
        final DocumentType documentType = DocumentType.valueOf(context.getProperty(DOCUMENT_TYPE).getValue());
        switch(documentType) {
            case Json:
                {
                    RawJsonDocument document = bucket.get(docId, RawJsonDocument.class);
                    if (document == null) {
                        doc = null;
                        content = null;
                    } else {
                        content = document.content().getBytes(StandardCharsets.UTF_8);
                        doc = document;
                    }
                    break;
                }
            case Binary:
                {
                    BinaryDocument document = bucket.get(docId, BinaryDocument.class);
                    if (document == null) {
                        doc = null;
                        content = null;
                    } else {
                        content = document.content().array();
                        doc = document;
                    }
                    break;
                }
            default:
                {
                    doc = null;
                    content = null;
                }
        }
        if (doc == null) {
            logger.error("Document {} was not found in {}; routing {} to failure", new Object[] { docId, getTransitUrl(context, docId), inFile });
            inFile = session.putAttribute(inFile, CouchbaseAttributes.Exception.key(), DocumentDoesNotExistException.class.getName());
            session.transfer(inFile, REL_FAILURE);
            return;
        }
        FlowFile outFile = session.create(inFile);
        outFile = session.write(outFile, new OutputStreamCallback() {

            @Override
            public void process(final OutputStream out) throws IOException {
                out.write(content);
            }
        });
        final Map<String, String> updatedAttrs = new HashMap<>();
        updatedAttrs.put(CouchbaseAttributes.Cluster.key(), context.getProperty(COUCHBASE_CLUSTER_SERVICE).getValue());
        updatedAttrs.put(CouchbaseAttributes.Bucket.key(), context.getProperty(BUCKET_NAME).getValue());
        updatedAttrs.put(CouchbaseAttributes.DocId.key(), docId);
        updatedAttrs.put(CouchbaseAttributes.Cas.key(), String.valueOf(doc.cas()));
        updatedAttrs.put(CouchbaseAttributes.Expiry.key(), String.valueOf(doc.expiry()));
        outFile = session.putAllAttributes(outFile, updatedAttrs);
        final long fetchMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
        session.getProvenanceReporter().fetch(outFile, getTransitUrl(context, docId), fetchMillis);
        session.transfer(outFile, REL_SUCCESS);
        session.transfer(inFile, REL_ORIGINAL);
    } catch (final CouchbaseException e) {
        String errMsg = String.format("Getting document %s from Couchbase Server using %s failed due to %s", docId, inFile, e);
        handleCouchbaseException(context, session, logger, inFile, e, errMsg);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) HashMap(java.util.HashMap) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) ComponentLog(org.apache.nifi.logging.ComponentLog) RawJsonDocument(com.couchbase.client.java.document.RawJsonDocument) BinaryDocument(com.couchbase.client.java.document.BinaryDocument) ProcessException(org.apache.nifi.processor.exception.ProcessException) CouchbaseException(com.couchbase.client.core.CouchbaseException) Bucket(com.couchbase.client.java.Bucket) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback)

Aggregations

Bucket (com.couchbase.client.java.Bucket)72 Test (org.junit.Test)53 Matchers.anyString (org.mockito.Matchers.anyString)21 JsonObject (com.couchbase.client.java.document.json.JsonObject)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)16 N1qlQueryResult (com.couchbase.client.java.query.N1qlQueryResult)15 MockFlowFile (org.apache.nifi.util.MockFlowFile)15 AsyncBucket (com.couchbase.client.java.AsyncBucket)13 TimeUnit (java.util.concurrent.TimeUnit)12 CouchbaseCluster (com.couchbase.client.java.CouchbaseCluster)11 CouchbaseException (com.couchbase.client.core.CouchbaseException)9 Cluster (com.couchbase.client.java.Cluster)8 RawJsonDocument (com.couchbase.client.java.document.RawJsonDocument)8 JsonDocument (com.couchbase.client.java.document.JsonDocument)7 CouchbaseEnvironment (com.couchbase.client.java.env.CouchbaseEnvironment)7 StringSerde (org.apache.samza.serializers.StringSerde)7 HashMap (java.util.HashMap)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 N1qlQuery (com.couchbase.client.java.query.N1qlQuery)4 N1qlQueryRow (com.couchbase.client.java.query.N1qlQueryRow)4