Search in sources :

Example 91 with KeyValue

use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.

the class TestTSMeta method parseFromColumn.

@Test
public void parseFromColumn() throws Exception {
    final KeyValue column = mock(KeyValue.class);
    when(column.key()).thenReturn(TSUID);
    when(column.value()).thenReturn(storage.getColumn(META_TABLE, TSUID, NAME_FAMILY, "ts_meta".getBytes(MockBase.ASCII())));
    final TSMeta meta = TSMeta.parseFromColumn(tsdb, column, false).joinUninterruptibly();
    assertNotNull(meta);
    assertEquals("000001000001000001", meta.getTSUID());
    assertNull(meta.getMetric());
}
Also used : KeyValue(org.hbase.async.KeyValue) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 92 with KeyValue

use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.

the class TestTSMeta method parseFromColumnWithUIDMeta.

@Test
public void parseFromColumnWithUIDMeta() throws Exception {
    final KeyValue column = mock(KeyValue.class);
    when(column.key()).thenReturn(TSUID);
    when(column.value()).thenReturn(storage.getColumn(META_TABLE, TSUID, NAME_FAMILY, "ts_meta".getBytes(MockBase.ASCII())));
    final TSMeta meta = TSMeta.parseFromColumn(tsdb, column, true).joinUninterruptibly();
    assertNotNull(meta);
    assertEquals("000001000001000001", meta.getTSUID());
    assertNotNull(meta.getMetric());
    assertEquals("sys.cpu.0", meta.getMetric().getName());
}
Also used : KeyValue(org.hbase.async.KeyValue) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 93 with KeyValue

use of org.hbase.async.KeyValue in project YCSB by brianfrankcooper.

the class AsyncHBaseClient method read.

@Override
public Status read(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
    setTable(table);
    final GetRequest get = new GetRequest(lastTableBytes, key.getBytes(), columnFamilyBytes);
    if (fields != null) {
        get.qualifiers(getQualifierList(fields));
    }
    try {
        if (debug) {
            System.out.println("Doing read from HBase columnfamily " + Bytes.pretty(columnFamilyBytes));
            System.out.println("Doing read for key: " + key);
        }
        final ArrayList<KeyValue> row = client.get(get).join(joinTimeout);
        if (row == null || row.isEmpty()) {
            return Status.NOT_FOUND;
        }
        // got something so populate the results
        for (final KeyValue column : row) {
            result.put(new String(column.qualifier()), // be GC'd.
            new ByteArrayByteIterator(column.value()));
            if (debug) {
                System.out.println("Result for field: " + Bytes.pretty(column.qualifier()) + " is: " + Bytes.pretty(column.value()));
            }
        }
        return Status.OK;
    } catch (InterruptedException e) {
        System.err.println("Thread interrupted");
        Thread.currentThread().interrupt();
    } catch (Exception e) {
        System.err.println("Failure reading from row with key " + key + ": " + e.getMessage());
        return Status.ERROR;
    }
    return Status.ERROR;
}
Also used : ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) KeyValue(org.hbase.async.KeyValue) GetRequest(org.hbase.async.GetRequest) DBException(com.yahoo.ycsb.DBException) IOException(java.io.IOException)

Example 94 with KeyValue

use of org.hbase.async.KeyValue in project YCSB by brianfrankcooper.

the class AsyncHBaseClient method scan.

@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    setTable(table);
    final Scanner scanner = client.newScanner(lastTableBytes);
    scanner.setFamily(columnFamilyBytes);
    scanner.setStartKey(startkey.getBytes(UTF8_CHARSET));
    // No end key... *sniff*
    if (fields != null) {
        scanner.setQualifiers(getQualifierList(fields));
    }
    // no filters? *sniff*
    ArrayList<ArrayList<KeyValue>> rows = null;
    try {
        int numResults = 0;
        while ((rows = scanner.nextRows().join(joinTimeout)) != null) {
            for (final ArrayList<KeyValue> row : rows) {
                final HashMap<String, ByteIterator> rowResult = new HashMap<String, ByteIterator>(row.size());
                for (final KeyValue column : row) {
                    rowResult.put(new String(column.qualifier()), // be GC'd.
                    new ByteArrayByteIterator(column.value()));
                    if (debug) {
                        System.out.println("Got scan result for key: " + Bytes.pretty(column.key()));
                    }
                }
                result.add(rowResult);
                numResults++;
                if (numResults >= recordcount) {
                    // if hit recordcount, bail out
                    break;
                }
            }
        }
        scanner.close().join(joinTimeout);
        return Status.OK;
    } catch (InterruptedException e) {
        System.err.println("Thread interrupted");
        Thread.currentThread().interrupt();
    } catch (Exception e) {
        System.err.println("Failure reading from row with key " + startkey + ": " + e.getMessage());
        return Status.ERROR;
    }
    return Status.ERROR;
}
Also used : Scanner(org.hbase.async.Scanner) KeyValue(org.hbase.async.KeyValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DBException(com.yahoo.ycsb.DBException) IOException(java.io.IOException) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator)

Example 95 with KeyValue

use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.

the class Annotation method getGlobalAnnotations.

/**
   * Scans through the global annotation storage rows and returns a list of 
   * parsed annotation objects. If no annotations were found for the given
   * timespan, the resulting list will be empty.
   * @param tsdb The TSDB to use for storage access
   * @param start_time Start time to scan from. May be 0
   * @param end_time End time to scan to. Must be greater than 0
   * @return A list with detected annotations. May be empty.
   * @throws IllegalArgumentException if the end timestamp has not been set or 
   * the end time is less than the start time
   */
public static Deferred<List<Annotation>> getGlobalAnnotations(final TSDB tsdb, final long start_time, final long end_time) {
    if (end_time < 1) {
        throw new IllegalArgumentException("The end timestamp has not been set");
    }
    if (end_time < start_time) {
        throw new IllegalArgumentException("The end timestamp cannot be less than the start timestamp");
    }
    /**
     * Scanner that loops through the [0, 0, 0, timestamp] rows looking for
     * global annotations. Returns a list of parsed annotation objects.
     * The list may be empty.
     */
    final class ScannerCB implements Callback<Deferred<List<Annotation>>, ArrayList<ArrayList<KeyValue>>> {

        final Scanner scanner;

        final ArrayList<Annotation> annotations = new ArrayList<Annotation>();

        /**
       * Initializes the scanner
       */
        public ScannerCB() {
            final byte[] start = new byte[Const.SALT_WIDTH() + TSDB.metrics_width() + Const.TIMESTAMP_BYTES];
            final byte[] end = new byte[Const.SALT_WIDTH() + TSDB.metrics_width() + Const.TIMESTAMP_BYTES];
            final long normalized_start = (start_time - (start_time % Const.MAX_TIMESPAN));
            final long normalized_end = (end_time - (end_time % Const.MAX_TIMESPAN) + Const.MAX_TIMESPAN);
            Bytes.setInt(start, (int) normalized_start, Const.SALT_WIDTH() + TSDB.metrics_width());
            Bytes.setInt(end, (int) normalized_end, Const.SALT_WIDTH() + TSDB.metrics_width());
            scanner = tsdb.getClient().newScanner(tsdb.dataTable());
            scanner.setStartKey(start);
            scanner.setStopKey(end);
            scanner.setFamily(FAMILY);
        }

        public Deferred<List<Annotation>> scan() {
            return scanner.nextRows().addCallbackDeferring(this);
        }

        @Override
        public Deferred<List<Annotation>> call(final ArrayList<ArrayList<KeyValue>> rows) throws Exception {
            if (rows == null || rows.isEmpty()) {
                return Deferred.fromResult((List<Annotation>) annotations);
            }
            for (final ArrayList<KeyValue> row : rows) {
                for (KeyValue column : row) {
                    if ((column.qualifier().length == 3 || column.qualifier().length == 5) && column.qualifier()[0] == PREFIX()) {
                        Annotation note = JSON.parseToObject(column.value(), Annotation.class);
                        if (note.start_time < start_time || note.end_time > end_time) {
                            continue;
                        }
                        annotations.add(note);
                    }
                }
            }
            return scan();
        }
    }
    return new ScannerCB().scan();
}
Also used : Scanner(org.hbase.async.Scanner) KeyValue(org.hbase.async.KeyValue) Callback(com.stumbleupon.async.Callback) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

KeyValue (org.hbase.async.KeyValue)171 Test (org.junit.Test)127 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)121 ArrayList (java.util.ArrayList)101 Annotation (net.opentsdb.meta.Annotation)50 Callback (com.stumbleupon.async.Callback)30 GetRequest (org.hbase.async.GetRequest)21 Scanner (org.hbase.async.Scanner)19 Deferred (com.stumbleupon.async.Deferred)14 HBaseException (org.hbase.async.HBaseException)13 TSDB (net.opentsdb.core.TSDB)12 Matchers.anyString (org.mockito.Matchers.anyString)11 Config (net.opentsdb.utils.Config)10 UniqueIdType (net.opentsdb.uid.UniqueId.UniqueIdType)9 DeleteRequest (org.hbase.async.DeleteRequest)8 DeferredGroupException (com.stumbleupon.async.DeferredGroupException)7 Map (java.util.Map)7 HashMap (java.util.HashMap)6 PutRequest (org.hbase.async.PutRequest)6 List (java.util.List)5