System.out.println("Setting up put for key: " + key); Put p = new Put(Bytes.toBytes(key)); for (Map.Entry<String, ByteIterator> entry : values.entrySet()) { byte[] value = entry.getValue().toArray(); if (debug) { System.out.println("Adding field/value " + entry.getKey() + "/" + Bytes.toStringBinary(value) + " to put request"); p.add(columnFamilyBytes, Bytes.toBytes(entry.getKey()), value); hTable.put(p); } catch (IOException e) { if (debug) {
System.out.println("Doing read for key: " + key); Get g = new Get(Bytes.toBytes(key)); if (fields == null) { g.addFamily(columnFamilyBytes); } else { for (String field : fields) { g.addColumn(columnFamilyBytes, Bytes.toBytes(field)); r = hTable.get(g); } catch (IOException e) { System.err.println("Error doing get: " + e); for (KeyValue kv : r.raw()) { result.put( Bytes.toString(kv.getQualifier()), new ByteArrayByteIterator(kv.getValue())); if (debug) {
Scan s = new Scan(Bytes.toBytes(startkey)); s.setCaching(recordcount); if (this.usePageFilter) { s.setFilter(new PageFilter(recordcount)); } else { for (String field : fields) { s.addColumn(columnFamilyBytes, Bytes.toBytes(field)); try (ResultScanner scanner = hTable.getScanner(s)) { int numResults = 0; for (Result rr = scanner.next(); rr != null; rr = scanner.next()) { String key = Bytes.toString(rr.getRow()); if (debug) { System.out.println("Got scan result for key: " + key); for (KeyValue kv : rr.raw()) { rowResult.put( Bytes.toString(kv.getQualifier()),
@Override public void batch(List<Row> writes, Object[] results) throws IOException, InterruptedException { table.batch(writes, results); table.flushCommits(); }
Put m = new Put(row); m.add(Bytes.toBytes("family"), Bytes.toBytes("qual"), null); Multimap<HTableInterfaceReference, Mutation> indexUpdates = ArrayListMultimap.<HTableInterfaceReference, Mutation> create(); Mockito.when(table.batch(Mockito.anyList())).thenAnswer(new Answer<Void>() { Mockito.when(table.getTableName()).thenReturn(test.getTableName());
throws IOException Put p = new Put(Bytes.toBytes(rowKey)); Get g = new Get(Bytes.toBytes(rowKey)); Result r = hTable.get(g); byte[] value = r.getValue(Bytes.toBytes(Constants.FOREIGN_KEY_EMBEDDED_COLUMN_NAME), Bytes.toBytes(property)); String existingForeignKey = Bytes.toString(value); p.add(Bytes.toBytes(Constants.FOREIGN_KEY_EMBEDDED_COLUMN_NAME), Bytes.toBytes(property), Bytes.toBytes(keys)); isPresent = true; p.add(Bytes.toBytes(Constants.FOREIGN_KEY_EMBEDDED_COLUMN_NAME), Bytes.toBytes(property), Bytes.toBytes(existingForeignKey + Constants.FOREIGN_KEY_SEPARATOR + keys)); isPresent = true; hTable.put(p);
Scan scan = new Scan(); scan.setFilter(new KeyOnlyFilter()); ResultScanner scanner = table.getScanner(scan); for (Result r : scanner) { r.getRow(); // nothing to do nRows++; long nBytes = 0; for (int i = 0; i < N_ROWS; i++) { byte[] rowkey = Bytes.toBytes(i); Put put = new Put(rowkey); byte[] cell = randomBytes(); put.add(CF, QN, cell); table.put(put); nBytes += cell.length; dot(i, N_ROWS);
byte[] family = Bytes.toBytes("1"); byte[] uintCol = Bytes.toBytes("UINT_COL"); byte[] ulongCol = Bytes.toBytes("ULONG_COL"); byte[] key, bKey; Put put; key = ByteUtil.concat(Bytes.toBytes(10), Bytes.toBytes(100L), Bytes.toBytes("a")); put = new Put(key); put.add(family, uintCol, ts-2, Bytes.toBytes(5)); put.add(family, ulongCol, ts-2, Bytes.toBytes(50L)); mutations.add(put); put = new Put(key); mutations.add(put); hTable.batch(mutations); Result r = hTable.get(new Get(bKey)); assertFalse(r.isEmpty()); } finally { hTable.close();
@Override protected ArrayList<String> listResourcesImpl(String resPath) throws IOException { assert resPath.startsWith("/"); String lookForPrefix = resPath.endsWith("/") ? resPath : resPath + "/"; byte[] startRow = Bytes.toBytes(lookForPrefix); byte[] endRow = Bytes.toBytes(lookForPrefix); endRow[endRow.length - 1]++; ArrayList<String> result = new ArrayList<String>(); HTableInterface table = getConnection().getTable(getAllInOneTableName()); Scan scan = new Scan(startRow, endRow); scan.setFilter(new KeyOnlyFilter()); try { ResultScanner scanner = table.getScanner(scan); for (Result r : scanner) { String path = Bytes.toString(r.getRow()); assert path.startsWith(lookForPrefix); int cut = path.indexOf('/', lookForPrefix.length()); String child = cut < 0 ? path : path.substring(0, cut); if (result.contains(child) == false) result.add(child); } } finally { IOUtils.closeQuietly(table); } // return null to indicate not a folder return result.isEmpty() ? null : result; }
private static void initTableValues() throws Exception { ConnectionQueryServices services = driver.getConnectionQueryServices(getUrl(), TEST_PROPERTIES); HTableInterface hTable = services.getTable(SchemaUtil.getTableNameAsBytes(HBASE_DYNAMIC_COLUMNS_SCHEMA_NAME,HBASE_DYNAMIC_COLUMNS)); try { // Insert rows using standard HBase mechanism with standard HBase "types" List<Row> mutations = new ArrayList<Row>(); byte[] dv = Bytes.toBytes("DV"); byte[] first = Bytes.toBytes("F"); byte[] f1v1 = Bytes.toBytes("F1V1"); byte[] f1v2 = Bytes.toBytes("F1V2"); byte[] f2v1 = Bytes.toBytes("F2V1"); byte[] f2v2 = Bytes.toBytes("F2V2"); byte[] key = Bytes.toBytes("entry1"); Put put = new Put(key); put.add(QueryConstants.EMPTY_COLUMN_BYTES, dv, Bytes.toBytes("default")); put.add(QueryConstants.EMPTY_COLUMN_BYTES, first, Bytes.toBytes("first")); put.add(FAMILY_NAME, f1v1, Bytes.toBytes("f1value1")); put.add(FAMILY_NAME, f1v2, Bytes.toBytes("f1value2")); put.add(FAMILY_NAME2, f2v1, Bytes.toBytes("f2value1")); put.add(FAMILY_NAME2, f2v2, Bytes.toBytes("f2value2")); mutations.add(put); hTable.batch(mutations); } finally { hTable.close(); } // Create Phoenix table after HBase table was created through the native APIs // The timestamp of the table creation must be later than the timestamp of the data ensureTableCreated(getUrl(), HBASE_DYNAMIC_COLUMNS); }
Get g = new Get(rowKeyBytes); g.addFamily(Bytes.toBytes(columnFamily)); g.setFilter(filter); Result result = hTable.get(g); if (result != null && result.list() != null) for (KeyValue value : result.list()) data.setColumns(result.list()); results.add(data); scan = new Scan(); scanner = hTable.getScanner(scan); resultsIter = scanner.iterator();
private Result getByScan(String path, byte[] family, byte[] column) throws IOException { byte[] startRow = Bytes.toBytes(path); byte[] endRow = plusZero(startRow); Scan scan = new Scan(startRow, endRow); if (family == null || column == null) { scan.setFilter(new KeyOnlyFilter()); } else { scan.addColumn(family, column); } HTableInterface table = getConnection().getTable(getAllInOneTableName()); try { ResultScanner scanner = table.getScanner(scan); Result result = null; for (Result r : scanner) { result = r; } return result == null || result.isEmpty() ? null : result; } finally { IOUtils.closeQuietly(table); } }
@Override public Object[] scanRowKeys(final HTableInterface hTable, final Filter filter, final String columnFamilyName, final String columnName, final Class rowKeyClazz) throws IOException { List<Object> rowKeys = new ArrayList<Object>(); if (scanner == null) { Scan s = new Scan(); s.setFilter(filter); s.addColumn(Bytes.toBytes(columnFamilyName), Bytes.toBytes(columnName)); scanner = hTable.getScanner(s); resultsIter = scanner.iterator(); } if (fetchSize == null) { for (Result result : scanner) { for (KeyValue keyValue : result.list()) { rowKeys.add(HBaseUtils.fromBytes(keyValue.getRow(), rowKeyClazz)); } } } if (rowKeys != null && !rowKeys.isEmpty()) { return rowKeys.toArray(new Object[0]); } return null; }
byte[] columnFamily = Bytes.toBytes(columnBuilder.getColumnFamily()); byte[] columnQualifier = columnBuilder.getColumnQualifier("value"); List<Get> gets = new ArrayList<>(); for(ProfileMeasurement measurement: measurements) { byte[] rowKey = rowKeyBuilder.rowKey(measurement); Get get = new Get(rowKey).addColumn(columnFamily, columnQualifier); gets.add(get); Result[] results = table.get(gets); for(int i = 0; i < results.length; ++i) { Result result = results[i]; ProfileMeasurement measurement = measurements.get(i); boolean exists = result.containsColumn(columnFamily, columnQualifier); if(exists) { byte[] value = result.getValue(columnFamily, columnQualifier); measurement.withProfileValue(SerDeUtils.fromBytes(value, clazz)); values.add(measurement);
Scan scan = new Scan(HConstants.EMPTY_START_ROW, new KeyOnlyFilter()); ResultScanner scanner = hTable.getScanner(scan); try { Result r = scanner.next(); if (r != null) { minKey = r.getRow(); scanner.close(); Result r = hTable.getRowOrBefore(maxPossibleKey, tableRef.getTable().getColumnFamilies().iterator().next().getName().getBytes()); if (r != null) { maxKey = r.getRow(); } finally { try { hTable.close(); } catch (IOException e) { if (sqlE == null) {
@Test public void shouldFindAll() throws Exception { ResultScanner resultScanner = mock(ResultScanner.class); Result result1 = mock(Result.class); Result result2 = mock(Result.class); when(result1.getRow()).thenReturn("user1".getBytes()); when(result2.getRow()).thenReturn("user2".getBytes()); when(result1.getValue(cf, Bytes.toBytes("type"))).thenReturn("userSettings1String".getBytes()); when(result2.getValue(cf, Bytes.toBytes("type"))).thenReturn("userSettings2String".getBytes()); when(resultScanner.iterator()).thenReturn(Arrays.asList(result1, result2).iterator()); when(userSettingsTable.getScanner(any(Scan.class))).thenReturn(resultScanner); UserSettingsClient userSettingsClient = new UserSettingsClient(userSettingsTable, cf); assertEquals(new HashMap<String, Optional<String>>() {{ put("user1", Optional.of("userSettings1String")); put("user2", Optional.of("userSettings2String")); }}, userSettingsClient.findAll("type")); }
Scan scan = new Scan(); int limit = 20; conn = HConnectionManager.createConnection(hconf); table = conn.getTable(hbaseTable); scanner = table.getScanner(scan); int count = 0; for (Result r : scanner) { byte[] rowkey = r.getRow(); System.out.println(Bytes.toStringBinary(rowkey)); count++; if (count == limit) scanner.close(); table.close();