public static void main(String[] args) throws Exception { Configuration config = HBaseConfiguration.create(); if (args.length > 0) { config.set("hbase.rootdir", args[0]); } HTable table = new HTable(config, "WordCount"); for (String word : WordSpout.words) { Get get = new Get(Bytes.toBytes(word)); Result result = table.get(get); byte[] countBytes = result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("count")); byte[] wordBytes = result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("word")); String wordStr = Bytes.toString(wordBytes); System.out.println(wordStr); long count = Bytes.toLong(countBytes); System.out.println("Word: '" + wordStr + "', Count: " + count); } } }
@Override public List<T> multiGet(List<List<Object>> keys) { List<Get> gets = new ArrayList<Get>(); for (List<Object> key : keys) { byte[] hbaseKey = this.options.mapMapper.rowKey(key); String qualifier = this.options.mapMapper.qualifier(key); LOG.info("Partition: {}, GET: {}", this.partitionNum, new String(hbaseKey)); Get get = new Get(hbaseKey); get.addColumn(this.options.columnFamily.getBytes(), qualifier.getBytes()); gets.add(get); } List<T> retval = new ArrayList<T>(); try { Result[] results = this.table.get(gets); for (int i = 0; i < keys.size(); i++) { String qualifier = this.options.mapMapper.qualifier(keys.get(i)); Result result = results[i]; byte[] value = result.getValue(this.options.columnFamily.getBytes(), qualifier.getBytes()); if (value != null) { retval.add(this.serializer.deserialize(value)); } else { retval.add(null); } } } catch (IOException e) { throw new FailedException("IOException while reading from HBase.", e); } return retval; }
private void assertResultEquals(Result result, int i) { assertEquals(String.format("%03d", i), Bytes.toString(result.getRow())); assertEquals(i, Bytes.toInt(result.getValue(FAMILY, CQ1))); assertEquals(i * i, Bytes.toInt(result.getValue(FAMILY, CQ2))); }
private void run(AtomicBoolean stop) throws InterruptedException, ExecutionException { while (!stop.get()) { for (int i = 0; i < COUNT; i++) { assertEquals(i, Bytes.toInt(TABLE.get(new Get(Bytes.toBytes(String.format("%03d", i)))) .get().getValue(FAMILY, QUALIFIER))); } } }
@Test public void testBatchDoNotAllowPartial() throws InterruptedException, ExecutionException { // we set batch to 2 and max result size to 1, then server will only returns one result per call // but we should get 2 + 1 for every row. List<Result> results = TABLE.scanAll(new Scan().setBatch(2).setMaxResultSize(1)).get(); assertEquals(2 * COUNT, results.size()); for (int i = 0; i < COUNT; i++) { Result firstTwo = results.get(2 * i); assertEquals(String.format("%02d", i), Bytes.toString(firstTwo.getRow())); assertEquals(2, firstTwo.size()); assertEquals(i, Bytes.toInt(firstTwo.getValue(FAMILY, CQS[0]))); assertEquals(2 * i, Bytes.toInt(firstTwo.getValue(FAMILY, CQS[1]))); Result secondOne = results.get(2 * i + 1); assertEquals(String.format("%02d", i), Bytes.toString(secondOne.getRow())); assertEquals(1, secondOne.size()); assertEquals(3 * i, Bytes.toInt(secondOne.getValue(FAMILY, CQS[2]))); } }
@Test public void testAppend() throws InterruptedException, ExecutionException { AsyncTable<?> table = ASYNC_CONN.getTable(TABLE_NAME); Result result = table.append(new Append(row).addColumn(FAMILY, QUALIFIER, VALUE)).get(); assertArrayEquals(VALUE, result.getValue(FAMILY, QUALIFIER)); result = table.append(new Append(row).addColumn(FAMILY, QUALIFIER, VALUE)).get(); // the second call should have no effect as we always generate the same nonce. assertArrayEquals(VALUE, result.getValue(FAMILY, QUALIFIER)); result = table.get(new Get(row)).get(); assertArrayEquals(VALUE, result.getValue(FAMILY, QUALIFIER)); }
private void assertResultEquals(Result result, int key, int start, int to) { assertEquals(to - start, result.size()); for (int i = start; i < to; i++) { assertEquals(key, Bytes.toInt(result.getValue(CF, Bytes.toBytes("cq" + i)))); } assertEquals(to - start == 4, result.mayHaveMoreCellsInRow()); }
protected final void assertSum() throws IOException { Result result = TABLE.get(new Get(ROW).addColumn(FAMILY, CQ1).addColumn(FAMILY, CQ2)); assertEquals(THREADS * (1 + UPPER) * UPPER / 2, Bytes.toLong(result.getValue(FAMILY, CQ1))); assertEquals(THREADS * (1 + UPPER) * UPPER, Bytes.toLong(result.getValue(FAMILY, CQ2))); }
/** * Implements mapper logic for use across APIs. */ protected static Put map(ImmutableBytesWritable key, Result value) throws IOException { if (value.size() != 1) { throw new IOException("There should only be one input column"); } Map<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> cf = value.getMap(); if(!cf.containsKey(INPUT_FAMILY)) { throw new IOException("Wrong input columns. Missing: '" + Bytes.toString(INPUT_FAMILY) + "'."); } // Get the original value and reverse it String originalValue = Bytes.toString(value.getValue(INPUT_FAMILY, INPUT_FAMILY)); StringBuilder newValue = new StringBuilder(originalValue); newValue.reverse(); // Now set the value to be collected Put outval = new Put(key.get()); outval.addColumn(OUTPUT_FAMILY, null, Bytes.toBytes(newValue.toString())); return outval; }
private void assertValueEquals(int start, int end) throws IOException { for (int i = start; i < end; i++) { assertEquals(i, Bytes.toInt(TABLE.get(new Get(Bytes.toBytes(i))).getValue(FAMILY, QUALIFIER))); } }
@Test public void test() throws Throwable { RenewLeaseConsumer consumer = new RenewLeaseConsumer(); TABLE.scan(new Scan(), consumer); List<Result> results = consumer.get(); // should not see the newly added value assertEquals(10, results.size()); IntStream.range(0, 10).forEach(i -> { Result result = results.get(i); assertEquals(String.format("%02d", i), Bytes.toString(result.getRow())); assertEquals(i, Bytes.toInt(result.getValue(FAMILY, CQ))); }); // now we can see the newly added value List<Result> results2 = TABLE.scanAll(new Scan()).get(); assertEquals(11, results2.size()); IntStream.range(0, 11).forEach(i -> { Result result = results2.get(i); assertEquals(String.format("%02d", i), Bytes.toString(result.getRow())); assertEquals(i, Bytes.toInt(result.getValue(FAMILY, CQ))); }); } }
@Test public void testPartialSuccess() throws IOException, InterruptedException, ExecutionException { Admin admin = TEST_UTIL.getAdmin(); TableDescriptor htd = TableDescriptorBuilder.newBuilder(admin.getDescriptor(TABLE_NAME)) .setCoprocessor(ErrorInjectObserver.class.getName()).build(); admin.modifyTable(htd); AsyncTable<?> table = tableGetter.apply(TABLE_NAME); table.putAll(Arrays.asList(SPLIT_KEYS).stream().map(k -> new Put(k).addColumn(FAMILY, CQ, k)) .collect(Collectors.toList())).get(); List<CompletableFuture<Result>> futures = table .get(Arrays.asList(SPLIT_KEYS).stream().map(k -> new Get(k)).collect(Collectors.toList())); for (int i = 0; i < SPLIT_KEYS.length - 1; i++) { assertArrayEquals(SPLIT_KEYS[i], futures.get(i).get().getValue(FAMILY, CQ)); } try { futures.get(SPLIT_KEYS.length - 1).get(); } catch (ExecutionException e) { assertThat(e.getCause(), instanceOf(RetriesExhaustedException.class)); } } }
private void assertResultEquals(Result result, int i) { assertEquals(String.format("%03d", i), Bytes.toString(result.getRow())); assertEquals(i, Bytes.toInt(result.getValue(FAMILY, CQ1))); assertEquals(i * i, Bytes.toInt(result.getValue(FAMILY, CQ2))); }
private void assertResult(TableName tableName, int expectedRowCount) throws IOException { try (Table table = TEST_UTIL.getConnection().getTable(tableName)) { Scan scan = new Scan(); try (ResultScanner scanner = table.getScanner(scan)) { Result result; int rowCount = 0; while ((result = scanner.next()) != null) { assertArrayEquals(result.getRow(), Bytes.toBytes(rowCount)); assertArrayEquals(result.getValue(Bytes.toBytes("f1"), Bytes.toBytes("cq")), Bytes.toBytes(rowCount)); rowCount += 1; } assertEquals(rowCount, expectedRowCount); } } }
@Override public void run() { for (int i=0; i<numOps; i++) { try { Append a = new Append(row); a.addColumn(fam1, qual1, val); a.addColumn(fam1, qual2, val); a.addColumn(fam2, qual3, val); a.setDurability(Durability.ASYNC_WAL); region.append(a, HConstants.NO_NONCE, HConstants.NO_NONCE); Get g = new Get(row); Result result = region.get(g); assertEquals(result.getValue(fam1, qual1).length, result.getValue(fam1, qual2).length); assertEquals(result.getValue(fam1, qual1).length, result.getValue(fam2, qual3).length); } catch (IOException e) { e.printStackTrace(); failures.incrementAndGet(); fail(); } } } };
@Override public void map(ImmutableBytesWritable row, Result result, OutputCollector<ImmutableBytesWritable, Put> outCollector, Reporter reporter) throws IOException { String rowKey = Bytes.toString(result.getRow()); final ImmutableBytesWritable pKey = new ImmutableBytesWritable( Bytes.toBytes(PRESIDENT_PATTERN)); final ImmutableBytesWritable aKey = new ImmutableBytesWritable( Bytes.toBytes(ACTOR_PATTERN)); ImmutableBytesWritable outKey = null; if (rowKey.startsWith(PRESIDENT_PATTERN)) { outKey = pKey; } else if (rowKey.startsWith(ACTOR_PATTERN)) { outKey = aKey; } else { throw new AssertionError("unexpected rowKey"); } String name = Bytes.toString(result.getValue(COLUMN_FAMILY, COLUMN_QUALIFIER)); outCollector.collect(outKey, new Put(Bytes.toBytes("rowKey2")) .addColumn(COLUMN_FAMILY, COLUMN_QUALIFIER, Bytes.toBytes(name))); } }
@Override public Iterable<Object> get(List<String> keys) { List<Get> gets = new ArrayList<>(); for (String key : keys) { WindowsStore.Entry.nonNullCheckForKey(key); byte[] effectiveKey = effectiveKey(key); gets.add(new Get(effectiveKey)); } Result[] results = null; try { results = htable().get(gets); } catch (IOException e) { throw new RuntimeException(e); } List<Object> values = new ArrayList<>(); for (int i = 0; i < results.length; i++) { Result result = results[i]; if (result.isEmpty()) { LOG.error("Got empty result for key [{}]", keys.get(i)); throw new RuntimeException("Received empty result for key: " + keys.get(i)); } Object resultObject = windowKryoSerializer().deserialize(result.getValue(family, qualifier)); values.add(resultObject); } return values; }
@Test public void testIncrement() throws InterruptedException, ExecutionException { AsyncTable<?> table = ASYNC_CONN.getTable(TABLE_NAME); assertEquals(1L, table.incrementColumnValue(row, FAMILY, QUALIFIER, 1L).get().longValue()); // the second call should have no effect as we always generate the same nonce. assertEquals(1L, table.incrementColumnValue(row, FAMILY, QUALIFIER, 1L).get().longValue()); Result result = table.get(new Get(row)).get(); assertEquals(1L, Bytes.toLong(result.getValue(FAMILY, QUALIFIER))); } }
@Test public void testReversedBatchDoNotAllowPartial() throws InterruptedException, ExecutionException { // we set batch to 2 and max result size to 1, then server will only returns one result per call // but we should get 2 + 1 for every row. List<Result> results = TABLE.scanAll(new Scan().setBatch(2).setMaxResultSize(1).setReversed(true)).get(); assertEquals(2 * COUNT, results.size()); for (int i = 0; i < COUNT; i++) { int row = COUNT - i - 1; Result firstTwo = results.get(2 * i); assertEquals(String.format("%02d", row), Bytes.toString(firstTwo.getRow())); assertEquals(2, firstTwo.size()); assertEquals(row, Bytes.toInt(firstTwo.getValue(FAMILY, CQS[0]))); assertEquals(2 * row, Bytes.toInt(firstTwo.getValue(FAMILY, CQS[1]))); Result secondOne = results.get(2 * i + 1); assertEquals(String.format("%02d", row), Bytes.toString(secondOne.getRow())); assertEquals(1, secondOne.size()); assertEquals(3 * row, Bytes.toInt(secondOne.getValue(FAMILY, CQS[2]))); } } }