Search in sources :

Example 6 with StringByteIterator

use of site.ycsb.StringByteIterator in project YCSB by brianfrankcooper.

the class BackoffSelectStrategy method readN1ql.

/**
 * Performs the {@link #read(String, String, Set, Map)} operation via N1QL ("SELECT").
 *
 * If this option should be used, the "-p couchbase.kv=false" property must be set.
 *
 * @param docId the document ID
 * @param fields the fields to be loaded
 * @param result the result map where the doc needs to be converted into
 * @return The result of the operation.
 */
private Status readN1ql(final String docId, Set<String> fields, final Map<String, ByteIterator> result) throws Exception {
    String readQuery = "SELECT " + joinFields(fields) + " FROM `" + bucketName + "` USE KEYS [$1]";
    N1qlQueryResult queryResult = bucket.query(N1qlQuery.parameterized(readQuery, JsonArray.from(docId), N1qlParams.build().adhoc(adhoc).maxParallelism(maxParallelism)));
    if (!queryResult.parseSuccess() || !queryResult.finalSuccess()) {
        throw new DBException("Error while parsing N1QL Result. Query: " + readQuery + ", Errors: " + queryResult.errors());
    }
    N1qlQueryRow row;
    try {
        row = queryResult.rows().next();
    } catch (NoSuchElementException ex) {
        return Status.NOT_FOUND;
    }
    JsonObject content = row.value();
    if (fields == null) {
        // n1ql result set scoped under *.bucketName
        content = content.getObject(bucketName);
        fields = content.getNames();
    }
    for (String field : fields) {
        Object value = content.get(field);
        result.put(field, new StringByteIterator(value != null ? value.toString() : ""));
    }
    return Status.OK;
}
Also used : DBException(site.ycsb.DBException) StringByteIterator(site.ycsb.StringByteIterator) JsonObject(com.couchbase.client.java.document.json.JsonObject) JsonObject(com.couchbase.client.java.document.json.JsonObject)

Example 7 with StringByteIterator

use of site.ycsb.StringByteIterator in project YCSB by brianfrankcooper.

the class BackoffSelectStrategy method decode.

/**
 * Decode the String from server and pass it into the decoded destination.
 *
 * @param source the loaded object.
 * @param fields the fields to check.
 * @param dest the result passed back to YCSB.
 */
private void decode(final String source, final Set<String> fields, final Map<String, ByteIterator> dest) {
    try {
        JsonNode json = JacksonTransformers.MAPPER.readTree(source);
        boolean checkFields = fields != null && !fields.isEmpty();
        for (Iterator<Map.Entry<String, JsonNode>> jsonFields = json.fields(); jsonFields.hasNext(); ) {
            Map.Entry<String, JsonNode> jsonField = jsonFields.next();
            String name = jsonField.getKey();
            if (checkFields && !fields.contains(name)) {
                continue;
            }
            JsonNode jsonValue = jsonField.getValue();
            if (jsonValue != null && !jsonValue.isNull()) {
                dest.put(name, new StringByteIterator(jsonValue.asText()));
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Could not decode JSON");
    }
}
Also used : StringByteIterator(site.ycsb.StringByteIterator) JsonNode(com.couchbase.client.deps.com.fasterxml.jackson.databind.JsonNode) TemporaryFailureException(com.couchbase.client.java.error.TemporaryFailureException) DBException(site.ycsb.DBException)

Example 8 with StringByteIterator

use of site.ycsb.StringByteIterator in project YCSB by brianfrankcooper.

the class TestTimeSeriesWorkload method verifyRow.

@Test
public void verifyRow() throws Exception {
    final Properties p = getUTProperties();
    final TimeSeriesWorkload wl = getWorkload(p, true);
    final TreeMap<String, String> validationTags = new TreeMap<String, String>();
    final HashMap<String, ByteIterator> cells = new HashMap<String, ByteIterator>();
    validationTags.put("AA", "AAAA");
    cells.put("AA", new StringByteIterator("AAAA"));
    validationTags.put("AB", "AAAB");
    cells.put("AB", new StringByteIterator("AAAB"));
    long hash = wl.validationFunction("AAAA", 1451606400L, validationTags);
    cells.put(TimeSeriesWorkload.TIMESTAMP_KEY_PROPERTY_DEFAULT, new NumericByteIterator(1451606400L));
    cells.put(TimeSeriesWorkload.VALUE_KEY_PROPERTY_DEFAULT, new NumericByteIterator(hash));
    assertEquals(wl.verifyRow("AAAA", cells), Status.OK);
    // tweak the last value a bit
    for (final ByteIterator it : cells.values()) {
        it.reset();
    }
    cells.put(TimeSeriesWorkload.VALUE_KEY_PROPERTY_DEFAULT, new NumericByteIterator(hash + 1));
    assertEquals(wl.verifyRow("AAAA", cells), Status.UNEXPECTED_STATE);
    // no value cell, returns an unexpected state
    for (final ByteIterator it : cells.values()) {
        it.reset();
    }
    cells.remove(TimeSeriesWorkload.VALUE_KEY_PROPERTY_DEFAULT);
    assertEquals(wl.verifyRow("AAAA", cells), Status.UNEXPECTED_STATE);
}
Also used : NumericByteIterator(site.ycsb.NumericByteIterator) ByteIterator(site.ycsb.ByteIterator) NumericByteIterator(site.ycsb.NumericByteIterator) StringByteIterator(site.ycsb.StringByteIterator) HashMap(java.util.HashMap) StringByteIterator(site.ycsb.StringByteIterator) Properties(java.util.Properties) TreeMap(java.util.TreeMap) Test(org.testng.annotations.Test)

Example 9 with StringByteIterator

use of site.ycsb.StringByteIterator in project YCSB by brianfrankcooper.

the class ElasticsearchRestClient method scan.

@Override
public Status scan(final String table, final String startkey, final int recordcount, final Set<String> fields, final Vector<HashMap<String, ByteIterator>> result) {
    try {
        final Response response;
        try (XContentBuilder builder = jsonBuilder()) {
            builder.startObject();
            builder.startObject("query");
            builder.startObject("range");
            builder.startObject(KEY);
            builder.field("gte", startkey);
            builder.endObject();
            builder.endObject();
            builder.endObject();
            builder.field("size", recordcount);
            builder.endObject();
            response = search(table, builder);
            @SuppressWarnings("unchecked") final Map<String, Object> map = map(response);
            @SuppressWarnings("unchecked") final Map<String, Object> hits = (Map<String, Object>) map.get("hits");
            @SuppressWarnings("unchecked") final List<Map<String, Object>> list = (List<Map<String, Object>>) hits.get("hits");
            for (final Map<String, Object> hit : list) {
                @SuppressWarnings("unchecked") final Map<String, Object> source = (Map<String, Object>) hit.get("_source");
                final HashMap<String, ByteIterator> entry;
                if (fields != null) {
                    entry = new HashMap<>(fields.size());
                    for (final String field : fields) {
                        entry.put(field, new StringByteIterator((String) source.get(field)));
                    }
                } else {
                    entry = new HashMap<>(hit.size());
                    for (final Map.Entry<String, Object> field : source.entrySet()) {
                        if (KEY.equals(field.getKey())) {
                            continue;
                        }
                        entry.put(field.getKey(), new StringByteIterator((String) field.getValue()));
                    }
                }
                result.add(entry);
            }
        }
        return Status.OK;
    } catch (final Exception e) {
        e.printStackTrace();
        return Status.ERROR;
    }
}
Also used : IOException(java.io.IOException) DBException(site.ycsb.DBException) Response(org.elasticsearch.client.Response) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) StringByteIterator(site.ycsb.StringByteIterator) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 10 with StringByteIterator

use of site.ycsb.StringByteIterator in project YCSB by brianfrankcooper.

the class ElasticsearchRestClient method read.

@Override
public Status read(final String table, final String key, final Set<String> fields, final Map<String, ByteIterator> result) {
    try {
        final Response searchResponse = search(table, key);
        final int statusCode = searchResponse.getStatusLine().getStatusCode();
        if (statusCode == 404) {
            return Status.NOT_FOUND;
        } else if (statusCode != HttpStatus.SC_OK) {
            return Status.ERROR;
        }
        final Map<String, Object> map = map(searchResponse);
        @SuppressWarnings("unchecked") final Map<String, Object> hits = (Map<String, Object>) map.get("hits");
        final int total = (int) hits.get("total");
        if (total == 0) {
            return Status.NOT_FOUND;
        }
        @SuppressWarnings("unchecked") final Map<String, Object> hit = (Map<String, Object>) ((List<Object>) hits.get("hits")).get(0);
        @SuppressWarnings("unchecked") final Map<String, Object> source = (Map<String, Object>) hit.get("_source");
        if (fields != null) {
            for (final String field : fields) {
                result.put(field, new StringByteIterator((String) source.get(field)));
            }
        } else {
            for (final Map.Entry<String, Object> e : source.entrySet()) {
                if (KEY.equals(e.getKey())) {
                    continue;
                }
                result.put(e.getKey(), new StringByteIterator((String) e.getValue()));
            }
        }
        return Status.OK;
    } catch (final Exception e) {
        e.printStackTrace();
        return Status.ERROR;
    }
}
Also used : Response(org.elasticsearch.client.Response) StringByteIterator(site.ycsb.StringByteIterator) HashMap(java.util.HashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) IOException(java.io.IOException) DBException(site.ycsb.DBException)

Aggregations

StringByteIterator (site.ycsb.StringByteIterator)49 ByteIterator (site.ycsb.ByteIterator)32 HashMap (java.util.HashMap)31 Status (site.ycsb.Status)19 DBException (site.ycsb.DBException)15 Test (org.junit.Test)12 Map (java.util.Map)8 IOException (java.io.IOException)5 Assume.assumeNoException (org.junit.Assume.assumeNoException)4 UnknownHostException (java.net.UnknownHostException)3 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 SearchHit (org.elasticsearch.search.SearchHit)3 JsonObject (com.couchbase.client.java.document.json.JsonObject)2 Collections.emptyMap (java.util.Collections.emptyMap)2 User (org.apache.gora.benchmark.generated.User)2 GoraException (org.apache.gora.util.GoraException)2 SolrQuery (org.apache.solr.client.solrj.SolrQuery)2 SolrServerException (org.apache.solr.client.solrj.SolrServerException)2 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)2 SolrDocumentList (org.apache.solr.common.SolrDocumentList)2