Search in sources :

Example 1 with StringByteIterator

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

the class ElasticsearchClient method scan.

/**
 * Perform a range scan for a set of records in the database. Each field/value
 * pair from the result will be stored in a HashMap.
 *
 * @param table
 *          The name of the table
 * @param startkey
 *          The record key of the first record to read.
 * @param recordcount
 *          The number of records to read
 * @param fields
 *          The list of fields to read, or null for all of them
 * @param result
 *          A Vector of HashMaps, where each HashMap is a set field/value
 *          pairs for one record
 * @return Zero on success, a non-zero error code on error. See this class's
 *         description for a discussion of error codes.
 */
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    try {
        final RangeQueryBuilder rangeQuery = rangeQuery("_id").gte(startkey);
        final SearchResponse response = client.prepareSearch(indexKey).setTypes(table).setQuery(rangeQuery).setSize(recordcount).execute().actionGet();
        HashMap<String, ByteIterator> entry;
        for (SearchHit hit : response.getHits()) {
            entry = new HashMap<>(fields.size());
            for (String field : fields) {
                entry.put(field, new StringByteIterator((String) hit.getSource().get(field)));
            }
            result.add(entry);
        }
        return Status.OK;
    } catch (Exception e) {
        e.printStackTrace();
        return Status.ERROR;
    }
}
Also used : ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) SearchHit(org.elasticsearch.search.SearchHit) StringByteIterator(site.ycsb.StringByteIterator) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) UnknownHostException(java.net.UnknownHostException) DBException(site.ycsb.DBException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with StringByteIterator

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

the class ElasticsearchClient method read.

@Override
public Status read(final String table, final String key, final Set<String> fields, final Map<String, ByteIterator> result) {
    try {
        final SearchResponse searchResponse = search(table, key);
        if (searchResponse.getHits().totalHits == 0) {
            return Status.NOT_FOUND;
        }
        final SearchHit hit = searchResponse.getHits().getAt(0);
        if (fields != null) {
            for (final String field : fields) {
                result.put(field, new StringByteIterator((String) hit.getSource().get(field)));
            }
        } else {
            for (final Map.Entry<String, Object> e : hit.getSource().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 : SearchHit(org.elasticsearch.search.SearchHit) StringByteIterator(site.ycsb.StringByteIterator) HashMap(java.util.HashMap) Map(java.util.Map) UnknownHostException(java.net.UnknownHostException) DBException(site.ycsb.DBException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with StringByteIterator

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

the class ElasticsearchClient 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 {
        refreshIfNeeded();
        final RangeQueryBuilder query = new RangeQueryBuilder(KEY).gte(startkey);
        final SearchResponse response = client.prepareSearch(indexKey).setQuery(query).setSize(recordcount).get();
        for (final SearchHit hit : response.getHits()) {
            final HashMap<String, ByteIterator> entry;
            if (fields != null) {
                entry = new HashMap<>(fields.size());
                for (final String field : fields) {
                    entry.put(field, new StringByteIterator((String) hit.getSource().get(field)));
                }
            } else {
                entry = new HashMap<>(hit.getSource().size());
                for (final Map.Entry<String, Object> field : hit.getSource().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 : SearchHit(org.elasticsearch.search.SearchHit) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) StringByteIterator(site.ycsb.StringByteIterator) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) HashMap(java.util.HashMap) Map(java.util.Map) UnknownHostException(java.net.UnknownHostException) DBException(site.ycsb.DBException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 4 with StringByteIterator

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

the class ElasticsearchIntegTestBase method testUpdate.

/**
 * Test of update method, of class ElasticsearchClient.
 */
@Test
public void testUpdate() {
    final HashMap<String, ByteIterator> newValues = new HashMap<>(10);
    for (int i = 1; i <= 10; i++) {
        newValues.put(FIELD_PREFIX + i, new StringByteIterator("newvalue" + i));
    }
    final Status updateResult = db.update(MOCK_TABLE, "1", newValues);
    assertEquals(Status.OK, updateResult);
    // validate that the values changed
    final HashMap<String, ByteIterator> resultParam = new HashMap<>(10);
    final Status readResult = db.read(MOCK_TABLE, "1", MOCK_DATA.keySet(), resultParam);
    assertEquals(Status.OK, readResult);
    for (int i = 1; i <= 10; i++) {
        assertEquals("newvalue" + i, resultParam.get(FIELD_PREFIX + i).toString());
    }
}
Also used : Status(site.ycsb.Status) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) HashMap(java.util.HashMap) StringByteIterator(site.ycsb.StringByteIterator) Test(org.junit.Test)

Example 5 with StringByteIterator

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

the class CouchbaseClient method decode.

/**
 * Decode the object from server into the storable result.
 *
 * @param source the loaded object.
 * @param fields the fields to check.
 * @param dest the result passed back to the ycsb core.
 */
private void decode(final Object source, final Set<String> fields, final Map<String, ByteIterator> dest) {
    if (useJson) {
        try {
            JsonNode json = JSON_MAPPER.readTree((String) 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");
        }
    } else {
        Map<String, String> converted = (HashMap<String, String>) source;
        for (Map.Entry<String, String> entry : converted.entrySet()) {
            dest.put(entry.getKey(), new StringByteIterator(entry.getValue()));
        }
    }
}
Also used : HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) DBException(site.ycsb.DBException) StringByteIterator(site.ycsb.StringByteIterator) HashMap(java.util.HashMap) Map(java.util.Map)

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