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());
}
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());
}
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;
}
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;
}
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();
}
Aggregations