public Optional<byte[]> get( Optional<String> table, Optional<String> family, Optional<String> qualifier, Optional<String> key) { if (!valid) { Logger.error("CANNOT GET! NO VALID CONNECTION"); return Optional.empty(); } if (table.isPresent() && family.isPresent() && qualifier.isPresent() && key.isPresent() && !key.get().isEmpty()) { try { final Table htable = connection.getTable(TableName.valueOf(table.get())); Result result = htable.get(new Get(key.get().getBytes("UTF8"))); return Optional.ofNullable( result.getValue(family.get().getBytes("UTF8"), qualifier.get().getBytes("UTF8"))); } catch (IOException e) { e.printStackTrace(); } } return Optional.empty(); }
public Map<String, Long> getRegionSizes(String tableName) { Map<String, Long> regions = new HashMap<>(); try { final Table table = connection.getTable(TableName.valueOf(tableName)); RegionLocator regionLocator = connection.getRegionLocator(table.getName()); List<HRegionLocation> tableRegionInfos = regionLocator.getAllRegionLocations(); Set<byte[]> tableRegions = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR); for (HRegionLocation regionInfo : tableRegionInfos) { tableRegions.add(regionInfo.getRegionInfo().getRegionName()); } ClusterStatus clusterStatus = connection.getAdmin().getClusterStatus(); Collection<ServerName> servers = clusterStatus.getServers(); final long megaByte = 1024L * 1024L; for (ServerName serverName : servers) { ServerLoad serverLoad = clusterStatus.getLoad(serverName); for (RegionLoad regionLoad : serverLoad.getRegionsLoad().values()) { byte[] regionId = regionLoad.getName(); if (tableRegions.contains(regionId)) { long regionSizeBytes = regionLoad.getStorefileSizeMB() * megaByte; regions.put(regionLoad.getNameAsString(), regionSizeBytes); } } } } catch (IOException e) { e.printStackTrace(); } return regions; }
public void putBatch(Optional<List<Request>> putRequests, boolean optimize) { if (!valid) { Logger.error("CANNOT PUT! NO VALID CONNECTION"); return; } List<Put> puts = new ArrayList<>(); if (putRequests.isPresent() && !putRequests.get().isEmpty()) { String tableName = putRequests.get().get(0).table; putRequests .get() .forEach( pr -> pr.getPut() .ifPresent( p -> { if (optimize) { p.setDurability(Durability.SKIP_WAL); } puts.add(p); })); try { final Table table = connection.getTable(TableName.valueOf(tableName)); if (optimize && table instanceof HTable) { ((HTable) table).setAutoFlush(false, true); } table.put(puts); table.close(); } catch (IOException e) { e.printStackTrace(); } } }
public Optional<Response> get(Optional<Request> request) { if (!valid) { Logger.error("CANNOT GET! NO VALID CONNECTION"); return Optional.empty(); } Response response = new Response(); if (request.isPresent()) { Request r = request.get(); response.key = r.key; response.table = r.table; try { final Table htable = connection.getTable(TableName.valueOf(r.table)); Result result = htable.get(new Get(r.key)); if (result == null || result.isEmpty()) { return Optional.empty(); } r.columns.forEach( c -> response.columns.add( new Request.Column( c.family, c.qualifier, result.getValue(c.family.getBytes(), c.qualifier.getBytes())))); } catch (IOException e) { e.printStackTrace(); } } return Optional.of(response); }
public void put(String tablename, Put p) { try { final Table table = connection.getTable(TableName.valueOf(tablename)); table.put(p); table.close(); } catch (IOException e) { e.printStackTrace(); } }
public Result get(String table, String family, byte[] key) { final Table htable; try { htable = connection.getTable(TableName.valueOf(table)); return htable.get(new Get(key)); } catch (IOException e) { e.printStackTrace(); } return null; }
public static void verifyMobRowCount( final HBaseTestingUtility util, final TableName tableName, long expectedRows) throws IOException { Table table = ConnectionFactory.createConnection(util.getConfiguration()).getTable(tableName); try { assertEquals(expectedRows, countMobRows(table)); } finally { table.close(); } }
public void scan( Consumer<Result> callback, String tableName, String columnFamily, String... qualifiers) { if (callback != null && tableName != null && columnFamily != null && qualifiers != null) { Scan s = new Scan(); byte[] family = Bytes.toBytes(columnFamily); for (String qualifier : qualifiers) { s.addColumn(family, Bytes.toBytes(qualifier)); } try { final Table table = connection.getTable(TableName.valueOf(tableName)); ResultScanner scanner = table.getScanner(s); for (Result r = scanner.next(); r != null; r = scanner.next()) { callback.accept(r); } } catch (IOException e) { e.printStackTrace(); } } }
public void put(Optional<Request> putRequest) { if (!valid) { Logger.error("CANNOT PUT! NO VALID CONNECTION"); return; } putRequest.ifPresent( pr -> pr.getPut() .ifPresent( p -> { try { final Table table = connection.getTable(TableName.valueOf(pr.table)); table.put(p); table.close(); } catch (IOException e) { e.printStackTrace(); } })); }
/** Return the number of rows in the given table. */ public static int countMobRows(final Table table) throws IOException { Scan scan = new Scan(); ResultScanner results = table.getScanner(scan); int count = 0; for (Result res : results) { count++; List<Cell> cells = res.listCells(); for (Cell cell : cells) { // Verify the value Assert.assertTrue(CellUtil.cloneValue(cell).length > 0); } } results.close(); return count; }