throw new DBException("No columnfamily specified"); columnFamilyBytes = Bytes.toBytes(columnFamily); HTableInterface ht = hConn.getTable(table); ht.getTableDescriptor(); } catch (IOException e) { throw new DBException(e);
@Override public void close() throws IOException { cnx.close(); } }
@Override public void clearTableRegionCache(byte[] tableName) throws SQLException { connection.clearRegionCache(tableName); }
@Override protected void putResourceImpl(String resPath, InputStream content, long ts) throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); IOUtils.copy(content, bout); bout.close(); HTableInterface table = getConnection().getTable(getAllInOneTableName()); try { byte[] row = Bytes.toBytes(resPath); Put put = buildPut(resPath, ts, row, bout.toByteArray(), table); table.put(put); table.flushCommits(); } finally { IOUtils.closeQuietly(table); } }
private static void prepareData(HConnection conn) throws IOException { HTableInterface table = conn.getTable(TEST_TABLE); 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); IOUtils.closeQuietly(table);
private static void fullScan(HConnection conn, boolean[] hits, Stats stats) throws IOException { HTableInterface table = conn.getTable(TEST_TABLE); try { stats.markStart(); Scan scan = new Scan(); scan.addFamily(CF); ResultScanner scanner = table.getScanner(scan); int i = 0; for (Result r : scanner) { if (hits[i]) stats.consume(r); dot(i, N_ROWS); i++; } stats.markEnd(); } finally { IOUtils.closeQuietly(table); } }
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 protected void deleteResourceImpl(String resPath) throws IOException { HTableInterface table = getConnection().getTable(getAllInOneTableName()); try { Delete del = new Delete(Bytes.toBytes(resPath)); table.delete(del); table.flushCommits(); } finally { IOUtils.closeQuietly(table); } }
@Override protected long checkAndPutResourceImpl(String resPath, byte[] content, long oldTS, long newTS) throws IOException, IllegalStateException { HTableInterface table = getConnection().getTable(getAllInOneTableName()); try { byte[] row = Bytes.toBytes(resPath); byte[] bOldTS = oldTS == 0 ? null : Bytes.toBytes(oldTS); Put put = buildPut(resPath, newTS, row, content, table); boolean ok = table.checkAndPut(row, B_FAMILY, B_COLUMN_TS, bOldTS, put); if (!ok) throw new IllegalStateException("Overwriting conflict " + resPath + ", expect old TS " + oldTS + ", but it is " + getResourceTimestamp(resPath)); table.flushCommits(); return newTS; } finally { IOUtils.closeQuietly(table); } }
@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; }
case CHANGE_HTABLE_HOST: { String tableName = (String) opt.params[0]; HTableDescriptor desc = hbaseAdmin.getTableDescriptor(TableName.valueOf(tableName)); hbaseAdmin.disableTable(tableName); desc.setValue(IRealizationConstants.HTableTag, dstConfig.getMetadataUrlPrefix()); HTableInterface destAclHtable = null; try { srcAclHtable = HBaseConnection.get(srcConfig.getMetadataUrl()).getTable(srcConfig.getMetadataUrlPrefix() + "_acl"); destAclHtable = HBaseConnection.get(dstConfig.getMetadataUrl()).getTable(dstConfig.getMetadataUrlPrefix() + "_acl"); Result result = srcAclHtable.get(new Get(Bytes.toBytes(cubeId))); if (result.listCells() != null) { for (Cell cell : result.listCells()) { byte[] family = CellUtil.cloneFamily(cell); byte[] column = CellUtil.cloneQualifier(cell); destAclHtable.put(put); destAclHtable.flushCommits(); } finally { IOUtils.closeQuietly(srcAclHtable); IOUtils.closeQuietly(destAclHtable);
public void writeContent(HttpServletResponse resp, String tableName, String url, String date14digit) throws IOException { String key = UrlUtils.urlToKey(url); HTableInterface table = hbaseConnection.getTable(tableName); Get get = new Get(Bytes.toBytes(key)); try { get.setTimeStamp(ArchiveUtils.parse14DigitDate(date14digit).getTime()); } catch (ParseException e) { e.printStackTrace(); } Result result = table.get(get); Cell[] cells = result.rawCells(); if (cells.length == 1) { // We should have exactly one result here... byte[] data = CellUtil.cloneValue(cells[0]); String type = Bytes.toString(CellUtil.cloneQualifier(cells[0])); LOG.info("Fetching " + key + " at " + date14digit); resp.setHeader("Content-Type", type); resp.setContentLength(data.length); resp.getOutputStream().write(data); } table.close(); } }
boolean success = false; try { TransactionAwareHTable table = new TransactionAwareHTable(conn.getTable(TABLE)); TransactionContext context = new TransactionContext(txClient, table); try { for (Result r : scanner) { if (!r.isEmpty()) { int rowId = Bytes.toInt(r.getRow()); long balance = Bytes.toLong(r.getValue(FAMILY, COL)); totalBalance += balance; LOG.info("Client #{}: balance = ${}", rowId, balance);
byte[] rowKey = HBaseUtils.convertKeyToRowKey(HBaseTableMetaModel.profileCacheTableName, key); LOG.info("Getting " + key + " from HBase with rowkey{" + Bytes.toString(rowKey) + "}"); HTableInterface table = hConnection.getTable(HBaseTableMetaModel.profileCacheTableName); Result result = table.get(get); .getFamilyMap(HBaseTableMetaModel.profileCacheColumnFamily); put.add(HBaseTableMetaModel.profileCacheColumnFamily, HBaseTableMetaModel.profileCacheJsonColumn, Bytes.toBytes(userProfile.getJSONObject().toString())); put.add(HBaseTableMetaModel.profileCacheColumnFamily, HBaseTableMetaModel.profileCacheTsColumn, Bytes.toBytes(System.currentTimeMillis())); table.put(put); table.close();
public static void populateValidationRules(HConnection connection, ValidationRules rules) throws Exception { HTableInterface table = connection.getTable(HBaseTableMetaModel.profileCacheTableName); try { Put put = new Put(HBaseTableMetaModel.validationRulesRowKey); put.add(HBaseTableMetaModel.profileCacheColumnFamily, HBaseTableMetaModel.validationRulesRowKey, Bytes.toBytes(rules.getJSONObject().toString())); table.put(put); } finally { table.close(); } } }
ThemisPut put = new ThemisPut(JOE).add(FAMILY, CASH, Bytes.toBytes(20)); transaction.put(CASHTABLE, put); put = new ThemisPut(BOB).add(FAMILY, CASH, Bytes.toBytes(9)); transaction.put(CASHTABLE, put); transaction.commit(); int cashOfJoe = Bytes.toInt(transaction.get(CASHTABLE, get).getValue(FAMILY, CASH)); get = new ThemisGet(BOB).addColumn(FAMILY, CASH); int cashOfBob = Bytes.toInt(transaction.get(CASHTABLE, get).getValue(FAMILY, CASH)); System.out.println("firstly, read out cash of the two users, Joe's cash is : $" + cashOfJoe + ", Bob's cash is : $" + cashOfBob); cashOfJoe = Bytes.toInt(transaction.get(CASHTABLE, get).getValue(FAMILY, CASH)); get = new ThemisGet(BOB).addColumn(FAMILY, CASH); cashOfBob = Bytes.toInt(transaction.get(CASHTABLE, get).getValue(FAMILY, CASH)); connection.close(); Transaction.destroy(); System.out.println("\n###############################Example End###############################");
HTableDescriptor hTableDesc = new HTableDescriptor(TableName.valueOf("external_hbase_table")); hTableDesc.addFamily(new HColumnDescriptor("col1")); hTableDesc.addFamily(new HColumnDescriptor("col2")); try (HTableInterface htable = hconn.getTable("external_hbase_table")) { for (int i = 0; i < 100; i++) { Put put = new Put(Bytes.toBytes((long) i)); put.add("col2".getBytes(), "k2".getBytes(), ("k2-" + i).getBytes()); put.add("col3".getBytes(), "b".getBytes(), Bytes.toBytes(i)); htable.put(put);
.getRegionsInRange(Bytes.toBytes(""), Bytes.toBytes("")).get(0) .getRegionInfo().getEncodedName(); List<HRegionInfo> newRegions = new ArrayList<HRegionInfo>(startKeys.length); Bytes.toStringBinary(row)); meta.delete(new Delete(row)); conn.clearRegionCache();