private static TableName getTableName(ByteBuffer buffer) { return TableName.valueOf(getBytes(buffer)); }
/** * Adds all the attributes into the Operation object */ private static void addAttributes(OperationWithAttributes op, Map<ByteBuffer, ByteBuffer> attributes) { if (attributes == null || attributes.isEmpty()) { return; } for (Map.Entry<ByteBuffer, ByteBuffer> entry : attributes.entrySet()) { String name = Bytes.toStringBinary(getBytes(entry.getKey())); byte[] value = getBytes(entry.getValue()); op.setAttribute(name, value); } }
protected Table getTable(final ByteBuffer tableName) throws IOException { return getTable(Bytes.getBytes(tableName)); }
/** * Adds all the attributes into the Operation object */ private static void addAttributes(OperationWithAttributes op, Map<ByteBuffer, ByteBuffer> attributes) { if (attributes == null || attributes.isEmpty()) { return; } for (Map.Entry<ByteBuffer, ByteBuffer> entry : attributes.entrySet()) { String name = Bytes.toStringBinary(getBytes(entry.getKey())); byte[] value = getBytes(entry.getValue()); op.setAttribute(name, value); } }
@Override public void majorCompact(ByteBuffer tableNameOrRegionName) throws IOError { try { try { getAdmin().compactRegion(getBytes(tableNameOrRegionName)); } catch (IllegalArgumentException e) { // Invalid region, try table getAdmin().compact(TableName.valueOf(getBytes(tableNameOrRegionName))); } } catch (IOException e) { LOG.warn(e.getMessage(), e); throw getIOError(e); } }
@Override public void compact(ByteBuffer tableNameOrRegionName) throws IOError { try { try { getAdmin().compactRegion(getBytes(tableNameOrRegionName)); } catch (IllegalArgumentException e) { // Invalid region, try table getAdmin().compact(TableName.valueOf(getBytes(tableNameOrRegionName))); } } catch (IOException e) { LOG.warn(e.getMessage(), e); throw getIOError(e); } }
@Override public List<TCell> getVer(ByteBuffer tableName, ByteBuffer row, ByteBuffer column, int numVersions, Map<ByteBuffer, ByteBuffer> attributes) throws IOError { byte [][] famAndQf = CellUtil.parseColumn(getBytes(column)); if(famAndQf.length == 1) { return getVer(tableName, row, famAndQf[0], null, numVersions, attributes); } if (famAndQf.length == 2) { return getVer(tableName, row, famAndQf[0], famAndQf[1], numVersions, attributes); } throw new IllegalArgumentException("Invalid familyAndQualifier provided."); }
@Override public List<TCell> getVerTs(ByteBuffer tableName, ByteBuffer row, ByteBuffer column, long timestamp, int numVersions, Map<ByteBuffer, ByteBuffer> attributes) throws IOError { byte [][] famAndQf = CellUtil.parseColumn(getBytes(column)); if (famAndQf.length == 1) { return getVerTs(tableName, row, famAndQf[0], null, timestamp, numVersions, attributes); } if (famAndQf.length == 2) { return getVerTs(tableName, row, famAndQf[0], famAndQf[1], timestamp, numVersions, attributes); } throw new IllegalArgumentException("Invalid familyAndQualifier provided."); }
@Override public List<TCell> get( ByteBuffer tableName, ByteBuffer row, ByteBuffer column, Map<ByteBuffer, ByteBuffer> attributes) throws IOError { byte [][] famAndQf = CellUtil.parseColumn(getBytes(column)); if (famAndQf.length == 1) { return get(tableName, row, famAndQf[0], null, attributes); } if (famAndQf.length == 2) { return get(tableName, row, famAndQf[0], famAndQf[1], attributes); } throw new IllegalArgumentException("Invalid familyAndQualifier provided."); }
/** * From a {@link TAppend} create an {@link Append}. * @param tappend the Thrift version of an append. * @return an increment that the {@link TAppend} represented. */ public static Append appendFromThrift(TAppend tappend) { Append append = new Append(tappend.getRow()); List<ByteBuffer> columns = tappend.getColumns(); List<ByteBuffer> values = tappend.getValues(); if (columns.size() != values.size()) { throw new IllegalArgumentException( "Sizes of columns and values in tappend object are not matching"); } int length = columns.size(); for (int i = 0; i < length; i++) { byte[][] famAndQf = CellUtil.parseColumn(getBytes(columns.get(i))); append.addColumn(famAndQf[0], famAndQf[1], getBytes(values.get(i))); } return append; } }
protected long atomicIncrement(ByteBuffer tableName, ByteBuffer row, byte [] family, byte [] qualifier, long amount) throws IOError, IllegalArgument, TException { Table table = null; try { table = getTable(tableName); return table.incrementColumnValue( getBytes(row), family, qualifier, amount); } catch (IOException e) { LOG.warn(e.getMessage(), e); throw getIOError(e); } finally { closeTable(table); } }
@Override public long atomicIncrement( ByteBuffer tableName, ByteBuffer row, ByteBuffer column, long amount) throws IOError, IllegalArgument, TException { byte [][] famAndQf = CellUtil.parseColumn(getBytes(column)); if(famAndQf.length == 1) { return atomicIncrement(tableName, row, famAndQf[0], HConstants.EMPTY_BYTE_ARRAY, amount); } return atomicIncrement(tableName, row, famAndQf[0], famAndQf[1], amount); }
@Override public void deleteAllRowTs( ByteBuffer tableName, ByteBuffer row, long timestamp, Map<ByteBuffer, ByteBuffer> attributes) throws IOError { Table table = null; try { table = getTable(tableName); Delete delete = new Delete(getBytes(row), timestamp); addAttributes(delete, attributes); table.delete(delete); } catch (IOException e) { LOG.warn(e.getMessage(), e); throw getIOError(e); } finally { closeTable(table); } }
@Override public int scannerOpenWithStop(ByteBuffer tableName, ByteBuffer startRow, ByteBuffer stopRow, List<ByteBuffer> columns, Map<ByteBuffer, ByteBuffer> attributes) throws IOError, TException { Table table = null; try { table = getTable(tableName); Scan scan = new Scan(getBytes(startRow), getBytes(stopRow)); addAttributes(scan, attributes); if(columns != null && !columns.isEmpty()) { for(ByteBuffer column : columns) { byte [][] famQf = CellUtil.parseColumn(getBytes(column)); if(famQf.length == 1) { scan.addFamily(famQf[0]); } else { scan.addColumn(famQf[0], famQf[1]); } } } return addScanner(table.getScanner(scan), false); } catch (IOException e) { LOG.warn(e.getMessage(), e); throw getIOError(e); } finally{ closeTable(table); } }
@Override public void deleteAllTs(ByteBuffer tableName, ByteBuffer row, ByteBuffer column, long timestamp, Map<ByteBuffer, ByteBuffer> attributes) throws IOError { Table table = null; try { table = getTable(tableName); Delete delete = new Delete(getBytes(row)); addAttributes(delete, attributes); byte [][] famAndQf = CellUtil.parseColumn(getBytes(column)); if (famAndQf.length == 1) { delete.addFamily(famAndQf[0], timestamp); } else { delete.addColumns(famAndQf[0], famAndQf[1], timestamp); } table.delete(delete); } catch (IOException e) { LOG.warn(e.getMessage(), e); throw getIOError(e); } finally { closeTable(table); } }
public void testGetBytesForByteBuffer() { byte[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; ByteBuffer target = ByteBuffer.wrap(array); target.position(2); target.limit(7); byte[] actual = Bytes.getBytes(target); byte[] expected = { 2, 3, 4, 5, 6 }; assertTrue(Arrays.equals(expected, actual)); assertEquals(2, target.position()); assertEquals(7, target.limit()); }
@Override public int scannerOpenWithStopTs(ByteBuffer tableName, ByteBuffer startRow, ByteBuffer stopRow, List<ByteBuffer> columns, long timestamp, Map<ByteBuffer, ByteBuffer> attributes) throws IOError, TException { Table table = null; try { table = getTable(tableName); Scan scan = new Scan(getBytes(startRow), getBytes(stopRow)); addAttributes(scan, attributes); scan.setTimeRange(0, timestamp); if (columns != null && !columns.isEmpty()) { for (ByteBuffer column : columns) { byte [][] famQf = CellUtil.parseColumn(getBytes(column)); if(famQf.length == 1) { scan.addFamily(famQf[0]); } else { scan.addColumn(famQf[0], famQf[1]); } } } scan.setTimeRange(0, timestamp); return addScanner(table.getScanner(scan), false); } catch (IOException e) { LOG.warn(e.getMessage(), e); throw getIOError(e); } finally{ closeTable(table); } }
@Override public int scannerOpen(ByteBuffer tableName, ByteBuffer startRow, List<ByteBuffer> columns, Map<ByteBuffer, ByteBuffer> attributes) throws IOError { Table table = null; try { table = getTable(tableName); Scan scan = new Scan(getBytes(startRow)); addAttributes(scan, attributes); if(columns != null && !columns.isEmpty()) { for(ByteBuffer column : columns) { byte [][] famQf = CellUtil.parseColumn(getBytes(column)); if(famQf.length == 1) { scan.addFamily(famQf[0]); } else { scan.addColumn(famQf[0], famQf[1]); } } } return addScanner(table.getScanner(scan), false); } catch (IOException e) { LOG.warn(e.getMessage(), e); throw getIOError(e); } finally{ closeTable(table); } }
@Override public int scannerOpenTs(ByteBuffer tableName, ByteBuffer startRow, List<ByteBuffer> columns, long timestamp, Map<ByteBuffer, ByteBuffer> attributes) throws IOError, TException { Table table = null; try { table = getTable(tableName); Scan scan = new Scan(getBytes(startRow)); addAttributes(scan, attributes); scan.setTimeRange(0, timestamp); if (columns != null && !columns.isEmpty()) { for (ByteBuffer column : columns) { byte [][] famQf = CellUtil.parseColumn(getBytes(column)); if(famQf.length == 1) { scan.addFamily(famQf[0]); } else { scan.addColumn(famQf[0], famQf[1]); } } } return addScanner(table.getScanner(scan), false); } catch (IOException e) { LOG.warn(e.getMessage(), e); throw getIOError(e); } finally{ closeTable(table); } }
/** * This utility method creates a new Hbase HColumnDescriptor object based on a * Thrift ColumnDescriptor "struct". * * @param in Thrift ColumnDescriptor object * @return HColumnDescriptor * @throws IllegalArgument if the column name is empty */ static public HColumnDescriptor colDescFromThrift(ColumnDescriptor in) throws IllegalArgument { Compression.Algorithm comp = Compression.getCompressionAlgorithmByName(in.compression.toLowerCase(Locale.ROOT)); BloomType bt = BloomType.valueOf(in.bloomFilterType); if (in.name == null || !in.name.hasRemaining()) { throw new IllegalArgument("column name is empty"); } byte [] parsedName = CellUtil.parseColumn(Bytes.getBytes(in.name))[0]; HColumnDescriptor col = new HColumnDescriptor(parsedName) .setMaxVersions(in.maxVersions) .setCompressionType(comp) .setInMemory(in.inMemory) .setBlockCacheEnabled(in.blockCacheEnabled) .setTimeToLive(in.timeToLive > 0 ? in.timeToLive : Integer.MAX_VALUE) .setBloomFilterType(bt); return col; }