Java Code Examples for org.apache.hadoop.hbase.CellUtil#parseColumn()

The following examples show how to use org.apache.hadoop.hbase.CellUtil#parseColumn() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThriftUtilities.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: RemoteHTable.java    From hbase with Apache License 2.0 6 votes vote down vote up
protected Result[] buildResultFromModel(final CellSetModel model) {
  List<Result> results = new ArrayList<>();
  for (RowModel row : model.getRows()) {
    List<Cell> kvs = new ArrayList<>(row.getCells().size());
    for (CellModel cell : row.getCells()) {
      byte[][] split = CellUtil.parseColumn(cell.getColumn());
      byte[] column = split[0];
      byte[] qualifier = null;
      if (split.length == 1) {
        qualifier = HConstants.EMPTY_BYTE_ARRAY;
      } else if (split.length == 2) {
        qualifier = split[1];
      } else {
        throw new IllegalArgumentException("Invalid familyAndQualifier provided.");
      }
      kvs
        .add(new KeyValue(row.getKey(), column, qualifier, cell.getTimestamp(), cell.getValue()));
    }
    results.add(Result.create(kvs));
  }
  return results.toArray(new Result[results.size()]);
}
 
Example 3
Source File: TestScannersWithLabels.java    From hbase with Apache License 2.0 6 votes vote down vote up
private static int insertData(TableName tableName, String column, double prob)
    throws IOException {
  byte[] k = new byte[3];
  byte[][] famAndQf = CellUtil.parseColumn(Bytes.toBytes(column));

  List<Put> puts = new ArrayList<>(9);
  for (int i = 0; i < 9; i++) {
    Put put = new Put(Bytes.toBytes("row" + i));
    put.setDurability(Durability.SKIP_WAL);
    put.addColumn(famAndQf[0], famAndQf[1], k);
    put.setCellVisibility(new CellVisibility("(" + SECRET + "|" + CONFIDENTIAL + ")" + "&" + "!"
        + TOPSECRET));
    puts.add(put);
  }
  try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
    table.put(puts);
  }
  return puts.size();
}
 
Example 4
Source File: ThriftHBaseServiceHandler.java    From hbase with Apache License 2.0 6 votes vote down vote up
@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);
  }
}
 
Example 5
Source File: TableInputFormat.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a combined family and qualifier and adds either both or just the
 * family in case there is no qualifier. This assumes the older colon
 * divided notation, e.g. "family:qualifier".
 *
 * @param scan The Scan to update.
 * @param familyAndQualifier family and qualifier
 * @throws IllegalArgumentException When familyAndQualifier is invalid.
 */
private static void addColumn(Scan scan, byte[] familyAndQualifier) {
  byte [][] fq = CellUtil.parseColumn(familyAndQualifier);
  if (fq.length == 1) {
    scan.addFamily(fq[0]);
  } else if (fq.length == 2) {
    scan.addColumn(fq[0], fq[1]);
  } else {
    throw new IllegalArgumentException("Invalid familyAndQualifier provided.");
  }
}
 
Example 6
Source File: ThriftHBaseServiceHandler.java    From hbase with Apache License 2.0 5 votes vote down vote up
@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().withStartRow(getBytes(startRow)).withStopRow(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);
  }
}
 
Example 7
Source File: ThriftHBaseServiceHandler.java    From hbase with Apache License 2.0 5 votes vote down vote up
@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().withStartRow(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);
  }
}
 
Example 8
Source File: ThriftHBaseServiceHandler.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public int scannerOpenWithPrefix(ByteBuffer tableName,
    ByteBuffer startAndPrefix,
    List<ByteBuffer> columns,
    Map<ByteBuffer, ByteBuffer> attributes)
    throws IOError, TException {

  Table table = null;
  try {
    table = getTable(tableName);
    Scan scan = new Scan().withStartRow(getBytes(startAndPrefix));
    addAttributes(scan, attributes);
    Filter f = new WhileMatchFilter(
        new PrefixFilter(getBytes(startAndPrefix)));
    scan.setFilter(f);
    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);
  }
}
 
Example 9
Source File: TestScannerResource.java    From hbase with Apache License 2.0 5 votes vote down vote up
static int insertData(Configuration conf, TableName tableName, String column, double prob)
    throws IOException {
  Random rng = new Random();
  byte[] k = new byte[3];
  byte [][] famAndQf = CellUtil.parseColumn(Bytes.toBytes(column));
  List<Put> puts = new ArrayList<>();
  for (byte b1 = 'a'; b1 < 'z'; b1++) {
    for (byte b2 = 'a'; b2 < 'z'; b2++) {
      for (byte b3 = 'a'; b3 < 'z'; b3++) {
        if (rng.nextDouble() < prob) {
          k[0] = b1;
          k[1] = b2;
          k[2] = b3;
          Put put = new Put(k);
          put.setDurability(Durability.SKIP_WAL);
          put.addColumn(famAndQf[0], famAndQf[1], k);
          puts.add(put);
        }
      }
    }
  }
  try (Connection conn = ConnectionFactory.createConnection(conf);
      Table table = conn.getTable(tableName)) {
    table.put(puts);
  }
  return puts.size();
}
 
Example 10
Source File: ThriftHBaseServiceHandler.java    From hbase with Apache License 2.0 5 votes vote down vote up
@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().withStartRow(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);
  }
}
 
Example 11
Source File: ThriftHBaseServiceHandler.java    From hbase with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 12
Source File: ThriftHBaseServiceHandler.java    From hbase with Apache License 2.0 5 votes vote down vote up
@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.");

}
 
Example 13
Source File: ThriftHBaseServiceHandler.java    From hbase with Apache License 2.0 5 votes vote down vote up
@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.");
}
 
Example 14
Source File: IncrementCoalescer.java    From hbase with Apache License 2.0 5 votes vote down vote up
private boolean internalQueueTincrement(TIncrement inc) {
  byte[][] famAndQf = CellUtil.parseColumn(inc.getColumn());

  if (famAndQf.length != 2) {
    return false;
  }

  return internalQueueIncrement(inc.getTable(), inc.getRow(), famAndQf[0], famAndQf[1],
    inc.getAmmount());
}
 
Example 15
Source File: ThriftUtilities.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * From a {@link TIncrement} create an {@link Increment}.
 * @param tincrement the Thrift version of an increment
 * @return an increment that the {@link TIncrement} represented.
 */
public static Increment incrementFromThrift(TIncrement tincrement) {
  Increment inc = new Increment(tincrement.getRow());
  byte[][] famAndQf = CellUtil.parseColumn(tincrement.getColumn());

  if (famAndQf.length != 2) {
    return null;
  }

  inc.addColumn(famAndQf[0], famAndQf[1], tincrement.getAmmount());
  return inc;
}
 
Example 16
Source File: RowResource.java    From hbase with Apache License 2.0 4 votes vote down vote up
Response updateBinary(final byte[] message, final HttpHeaders headers,
    final boolean replace) {
  servlet.getMetrics().incrementRequests(1);
  if (servlet.isReadOnly()) {
    servlet.getMetrics().incrementFailedPutRequests(1);
    return Response.status(Response.Status.FORBIDDEN)
      .type(MIMETYPE_TEXT).entity("Forbidden" + CRLF)
      .build();
  }
  Table table = null;
  try {
    byte[] row = rowspec.getRow();
    byte[][] columns = rowspec.getColumns();
    byte[] column = null;
    if (columns != null) {
      column = columns[0];
    }
    long timestamp = HConstants.LATEST_TIMESTAMP;
    List<String> vals = headers.getRequestHeader("X-Row");
    if (vals != null && !vals.isEmpty()) {
      row = Bytes.toBytes(vals.get(0));
    }
    vals = headers.getRequestHeader("X-Column");
    if (vals != null && !vals.isEmpty()) {
      column = Bytes.toBytes(vals.get(0));
    }
    vals = headers.getRequestHeader("X-Timestamp");
    if (vals != null && !vals.isEmpty()) {
      timestamp = Long.parseLong(vals.get(0));
    }
    if (column == null) {
      servlet.getMetrics().incrementFailedPutRequests(1);
      return Response.status(Response.Status.BAD_REQUEST)
          .type(MIMETYPE_TEXT).entity("Bad request: Column found to be null." + CRLF)
          .build();
    }
    Put put = new Put(row);
    byte parts[][] = CellUtil.parseColumn(column);
    if (parts.length != 2) {
      return Response.status(Response.Status.BAD_REQUEST)
        .type(MIMETYPE_TEXT).entity("Bad request" + CRLF)
        .build();
    }
    put.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY)
      .setRow(put.getRow())
      .setFamily(parts[0])
      .setQualifier(parts[1])
      .setTimestamp(timestamp)
      .setType(Type.Put)
      .setValue(message)
      .build());
    table = servlet.getTable(tableResource.getName());
    table.put(put);
    if (LOG.isTraceEnabled()) {
      LOG.trace("PUT " + put.toString());
    }
    servlet.getMetrics().incrementSucessfulPutRequests(1);
    return Response.ok().build();
  } catch (Exception e) {
    servlet.getMetrics().incrementFailedPutRequests(1);
    return processException(e);
  } finally {
    if (table != null) try {
      table.close();
    } catch (IOException ioe) {
      LOG.debug("Exception received while closing the table", ioe);
    }
  }
}
 
Example 17
Source File: ScannerResultGenerator.java    From hbase with Apache License 2.0 4 votes vote down vote up
public ScannerResultGenerator(final String tableName, final RowSpec rowspec,
  final Filter filter, final int caching ,final boolean cacheBlocks, int limit) throws IOException {
  Table table = RESTServlet.getInstance().getTable(tableName);
  try {
    Scan scan;
    if (rowspec.hasEndRow()) {
      scan = new Scan().withStartRow(rowspec.getStartRow()).withStopRow(rowspec.getEndRow());
    } else {
      scan = new Scan().withStartRow(rowspec.getStartRow());
    }
    if (rowspec.hasColumns()) {
      byte[][] columns = rowspec.getColumns();
      for (byte[] column: columns) {
        byte[][] split = CellUtil.parseColumn(column);
        if (split.length == 1) {
          scan.addFamily(split[0]);
        } else if (split.length == 2) {
          scan.addColumn(split[0], split[1]);
        } else {
          throw new IllegalArgumentException("Invalid familyAndQualifier provided.");
        }
      }
    }
    scan.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime());
    scan.readVersions(rowspec.getMaxVersions());
    if (filter != null) {
      scan.setFilter(filter);
    }
    if (caching > 0 ) {
      scan.setCaching(caching);
    }
    if (limit > 0) {
      scan.setLimit(limit);
    }
    scan.setCacheBlocks(cacheBlocks);
    if (rowspec.hasLabels()) {
      scan.setAuthorizations(new Authorizations(rowspec.getLabels()));
    }
    scanner = table.getScanner(scan);
    cached = null;
    id = Long.toString(System.currentTimeMillis()) +
           Integer.toHexString(scanner.hashCode());
  } finally {
    table.close();
  }
}
 
Example 18
Source File: RowResultGenerator.java    From hbase with Apache License 2.0 4 votes vote down vote up
public RowResultGenerator(final String tableName, final RowSpec rowspec,
    final Filter filter, final boolean cacheBlocks)
    throws IllegalArgumentException, IOException {
  try (Table table = RESTServlet.getInstance().getTable(tableName)) {
    Get get = new Get(rowspec.getRow());
    if (rowspec.hasColumns()) {
      for (byte[] col : rowspec.getColumns()) {
        byte[][] split = CellUtil.parseColumn(col);
        if (split.length == 1) {
          get.addFamily(split[0]);
        } else if (split.length == 2) {
          get.addColumn(split[0], split[1]);
        } else {
          throw new IllegalArgumentException("Invalid column specifier.");
        }
      }
    }
    get.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime());
    get.readVersions(rowspec.getMaxVersions());
    if (filter != null) {
      get.setFilter(filter);
    }
    get.setCacheBlocks(cacheBlocks);
    Result result = table.get(get);
    if (result != null && !result.isEmpty()) {
      valuesI = result.listCells().iterator();
    }
  } catch (DoNotRetryIOException e) {
    // Warn here because Stargate will return 404 in the case if multiple
    // column families were specified but one did not exist -- currently
    // HBase will fail the whole Get.
    // Specifying multiple columns in a URI should be uncommon usage but
    // help to avoid confusion by leaving a record of what happened here in
    // the log.
    LOG.warn(StringUtils.stringifyException(e));
    // Lets get the exception rethrown to get a more meaningful error message than 404
    if (e instanceof AccessDeniedException) {
      throw e;
    }
  }
}
 
Example 19
Source File: ThriftHBaseServiceHandler.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public int scannerOpenWithScan(ByteBuffer tableName, TScan tScan,
    Map<ByteBuffer, ByteBuffer> attributes)
    throws IOError {

  Table table = null;
  try {
    table = getTable(tableName);
    Scan scan = new Scan();
    addAttributes(scan, attributes);
    if (tScan.isSetStartRow()) {
      scan.withStartRow(tScan.getStartRow());
    }
    if (tScan.isSetStopRow()) {
      scan.withStopRow(tScan.getStopRow());
    }
    if (tScan.isSetTimestamp()) {
      scan.setTimeRange(0, tScan.getTimestamp());
    }
    if (tScan.isSetCaching()) {
      scan.setCaching(tScan.getCaching());
    }
    if (tScan.isSetBatchSize()) {
      scan.setBatch(tScan.getBatchSize());
    }
    if (tScan.isSetColumns() && !tScan.getColumns().isEmpty()) {
      for(ByteBuffer column : tScan.getColumns()) {
        byte [][] famQf = CellUtil.parseColumn(getBytes(column));
        if(famQf.length == 1) {
          scan.addFamily(famQf[0]);
        } else {
          scan.addColumn(famQf[0], famQf[1]);
        }
      }
    }
    if (tScan.isSetFilterString()) {
      ParseFilter parseFilter = new ParseFilter();
      scan.setFilter(
          parseFilter.parseFilterString(tScan.getFilterString()));
    }
    if (tScan.isSetReversed()) {
      scan.setReversed(tScan.isReversed());
    }
    if (tScan.isSetCacheBlocks()) {
      scan.setCacheBlocks(tScan.isCacheBlocks());
    }
    return addScanner(table.getScanner(scan), tScan.sortColumns);
  } catch (IOException e) {
    LOG.warn(e.getMessage(), e);
    throw getIOError(e);
  } finally{
    closeTable(table);
  }
}
 
Example 20
Source File: TestScannersWithFilters.java    From hbase with Apache License 2.0 4 votes vote down vote up
private static void verifyScanFull(Scan s, KeyValue [] kvs) throws Exception {
  ScannerModel model = ScannerModel.fromScan(s);
  model.setBatch(Integer.MAX_VALUE); // fetch it all at once
  StringWriter writer = new StringWriter();
  marshaller.marshal(model, writer);
  LOG.debug(writer.toString());
  byte[] body = Bytes.toBytes(writer.toString());
  Response response = client.put("/" + TABLE + "/scanner",
    Constants.MIMETYPE_XML, body);
  assertEquals(201, response.getCode());
  String scannerURI = response.getLocation();
  assertNotNull(scannerURI);

  // get a cell set
  response = client.get(scannerURI, Constants.MIMETYPE_XML);
  assertEquals(200, response.getCode());
  assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
  CellSetModel cellSet = (CellSetModel)
    unmarshaller.unmarshal(new ByteArrayInputStream(response.getBody()));

  // delete the scanner
  response = client.delete(scannerURI);
  assertEquals(200, response.getCode());

  int row = 0;
  int idx = 0;
  Iterator<RowModel> i = cellSet.getRows().iterator();
  for (boolean done = true; done; row++) {
    done = i.hasNext();
    if (!done) {
      break;
    }

    RowModel rowModel = i.next();
    List<CellModel> cells = rowModel.getCells();
    if (cells.isEmpty()) {
      break;
    }

    assertTrue("Scanned too many keys! Only expected " + kvs.length +
      " total but already scanned " + (cells.size() + idx),
      kvs.length >= idx + cells.size());
    for (CellModel cell: cells) {
      assertTrue("Row mismatch",
          Bytes.equals(rowModel.getKey(), CellUtil.cloneRow(kvs[idx])));
      byte[][] split = CellUtil.parseColumn(cell.getColumn());
      assertTrue("Family mismatch",
          Bytes.equals(split[0], CellUtil.cloneFamily(kvs[idx])));
      assertTrue("Qualifier mismatch",
          Bytes.equals(split[1], CellUtil.cloneQualifier(kvs[idx])));
      assertTrue("Value mismatch",
          Bytes.equals(cell.getValue(), CellUtil.cloneValue(kvs[idx])));
      idx++;
    }
  }
  assertEquals("Expected " + kvs.length + " total keys but scanned " + idx,
    kvs.length, idx);
}