public Result[] batchGet(List<Get> gets) throws Exception { try { return table.get(gets); } catch (Exception e) { LOG.warn("Could not perform HBASE lookup.", e); throw e; } }
table.put(put); Get get = new Get(row); Result result = table.get(get); put.addColumn(family, qualifier, newVal); table.put(put); result = table.get(get); LOG.debug("Result: " + Bytes.toString(result.getValue(family, qualifier))); Assert.assertArrayEquals(newVal, result.getValue(family, qualifier));
@Override public Result get(final Get get) throws IOException { return get(get, get.isCheckExistenceOnly()); }
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 Result[] get(List<Get> gets) throws IOException { if (gets.size() == 1) { return new Result[]{get(gets.get(0))}; } try { Object[] r1 = new Object[gets.size()]; batch((List<? extends Row>)gets, r1, readRpcTimeoutMs); // Translate. Result [] results = new Result[r1.length]; int i = 0; for (Object obj: r1) { // Batch ensures if there is a failure we get an exception instead results[i++] = (Result)obj; } return results; } catch (InterruptedException e) { throw (InterruptedIOException)new InterruptedIOException().initCause(e); } }
Result result = table.get(get);
@Override public boolean exists(final Get get) throws IOException { Result r = get(get, true); assert r.getExists() != null; return r.getExists(); }
@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; }
@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; }
@Override public Object get(String key) { WindowsStore.Entry.nonNullCheckForKey(key); byte[] effectiveKey = effectiveKey(key); Get get = new Get(effectiveKey); Result result = null; try { result = htable().get(get); } catch (IOException e) { throw new RuntimeException(e); } if (result.isEmpty()) { return null; } return windowKryoSerializer().deserialize(result.getValue(family, qualifier)); }
gets.add(new Get(DUMMY_BYTES_2)); try { ht.get(gets); } catch (ClassCastException e) {
@Test public void testBasicOperation() throws Exception { TableName tn = TableName.valueOf(name.getMethodName()); // create table Connection conn = TEST_UTIL.getConnection(); Admin admin = conn.getAdmin(); HTable table = (HTable) conn.getTable(tn); try { ColumnFamilyDescriptorBuilder cfDescBuilder = ColumnFamilyDescriptorBuilder.newBuilder(family); TableDescriptorBuilder tableDescBuilder = TableDescriptorBuilder.newBuilder(tn).setColumnFamily(cfDescBuilder.build()); admin.createTable(tableDescBuilder.build()); // test simple get and put Put put = new Put(row); put.addColumn(family, qualifier, value); table.put(put); Get get = new Get(row); Result result = table.get(get); LOG.debug("Result: " + Bytes.toString(result.getValue(family, qualifier))); Assert.assertArrayEquals(value, result.getValue(family, qualifier)); } finally { admin.close(); table.close(); } }
Result readResult = table.get(new Get(ROW)); cells = readResult.rawCells(); assertEquals(1, cells.length);
Result readResult = table.get(new Get(ROW)); cells = readResult.rawCells(); assertEquals(1, cells.length);
Result result = table.get(get); LOG.debug("Result: " + Bytes.toString(result.getValue(family, qualifier))); Assert.assertArrayEquals(value, result.getValue(family, qualifier));
@Override public Result get(final Get get) throws IOException { return get(get, get.isCheckExistenceOnly()); }
public String evaluate(Map<String, String> config, String key) { try { HTable table = HTableFactory.getHTable(config); Get theGet = new Get(key.getBytes()); Result res = table.get(theGet); byte[] valBytes = res.getValue(config.get(HTableFactory.FAMILY_TAG).getBytes(), config.get(HTableFactory.QUALIFIER_TAG).getBytes()); if (valBytes != null) { return new String(valBytes); } return null; } catch (Exception exc) { ///LOG.error(" Error while trying HBase PUT ",exc); throw new RuntimeException(exc); } }
public String loadString(String key) throws Exception { Get keyGet = new Get(key.getBytes()); HTable htable = HTableFactory.getHTable(configMap); Result res = htable.get(keyGet); KeyValue kv = res.getColumnLatest(configMap.get(HTableFactory.FAMILY_TAG).getBytes(), configMap.get(HTableFactory.QUALIFIER_TAG).getBytes()); if (kv == null) { throw new NoSuchElementException("No value found for " + key); } byte[] bytes = kv.getValue(); String jsonStr = new String(bytes); return jsonStr; }
@Override public Result[] get(List<Get> gets) throws IOException { if (gets.size() == 1) { return new Result[]{get(gets.get(0))}; } try { Object[] r1 = new Object[gets.size()]; batch((List<? extends Row>)gets, r1, readRpcTimeoutMs); // Translate. Result [] results = new Result[r1.length]; int i = 0; for (Object obj: r1) { // Batch ensures if there is a failure we get an exception instead results[i++] = (Result)obj; } return results; } catch (InterruptedException e) { throw (InterruptedIOException)new InterruptedIOException().initCause(e); } }
@Override public boolean exists(final Get get) throws IOException { Result r = get(get, true); assert r.getExists() != null; return r.getExists(); }