示例#1
0
 /**
  * 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 = KeyValue.parseColumn(tincrement.getColumn());
   if (famAndQf.length != 2) return null;
   inc.addColumn(famAndQf[0], famAndQf[1], tincrement.getAmmount());
   return inc;
 }
示例#2
0
 static int insertData(Configuration conf, TableName tableName, String column, double prob)
     throws IOException {
   Random rng = new Random();
   int count = 0;
   HTable table = new HTable(conf, tableName);
   byte[] k = new byte[3];
   byte[][] famAndQf = KeyValue.parseColumn(Bytes.toBytes(column));
   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.add(famAndQf[0], famAndQf[1], k);
           table.put(put);
           count++;
         }
       }
     }
   }
   table.flushCommits();
   table.close();
   return count;
 }
  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(response.getCode(), 201);
    String scannerURI = response.getLocation();
    assertNotNull(scannerURI);

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

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

    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(), kvs[idx].getRow()));
        byte[][] split = KeyValue.parseColumn(cell.getColumn());
        assertTrue("Family mismatch", Bytes.equals(split[0], kvs[idx].getFamily()));
        assertTrue("Qualifier mismatch", Bytes.equals(split[1], kvs[idx].getQualifier()));
        assertTrue("Value mismatch", Bytes.equals(cell.getValue(), kvs[idx].getValue()));
        idx++;
      }
    }
    assertEquals("Expected " + kvs.length + " total keys but scanned " + idx, kvs.length, idx);
  }
示例#4
0
  /**
   * 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 = KeyValue.parseColumn(getBytes(columns.get(i)));
      append.add(famAndQf[0], famAndQf[1], getBytes(values.get(i)));
    }
    return append;
  }
示例#5
0
  /**
   * This utility method creates a new Hbase HColumnDescriptor object based on a Thrift
   * ColumnDescriptor "struct".
   *
   * @param in Thrift ColumnDescriptor object
   * @return HColumnDescriptor
   * @throws IllegalArgument
   */
  public static HColumnDescriptor colDescFromThrift(ColumnDescriptor in) throws IllegalArgument {
    Compression.Algorithm comp =
        Compression.getCompressionAlgorithmByName(in.compression.toLowerCase());
    BloomType bt = BloomType.valueOf(in.bloomFilterType);

    if (in.name == null || !in.name.hasRemaining()) {
      throw new IllegalArgument("column name is empty");
    }
    byte[] parsedName = KeyValue.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;
  }
  @Override
  protected void setup(Context context) throws IOException, InterruptedException {
    super.setup(context);
    // load invalid characters
    invalid = new ArrayList<String>();
    BufferedReader br =
        new BufferedReader(
            new InputStreamReader(
                SearchKeywordMapper.class.getResourceAsStream(Const.INVALID_CHARACTOR_PATH)));
    String line = null;
    while (null != (line = br.readLine())) {
      line = line.trim();
      invalid.add(line);
    }
    br.close();

    String column = context.getConfiguration().get("conf.column");
    byte[][] colkey = KeyValue.parseColumn(Bytes.toBytes(column));
    family = colkey[0];
    if (colkey.length > 1) {
      qualifier = colkey[1];
    }
  }
示例#7
0
 /**
  * Delete the latest version of the specified column, given in <code>family:qualifier</code>
  * notation.
  *
  * @param column colon-delimited family and qualifier
  * @deprecated use {@link #deleteColumn(byte[], byte[])} instead
  * @return this for invocation chaining
  */
 public Delete deleteColumn(byte[] column) {
   byte[][] parts = KeyValue.parseColumn(column);
   this.deleteColumn(parts[0], parts[1], HConstants.LATEST_TIMESTAMP);
   return this;
 }
示例#8
0
 /**
  * Delete all versions of the specified column, given in <code>family:qualifier</code> notation,
  * and with a timestamp less than or equal to the specified timestamp.
  *
  * @param column colon-delimited family and qualifier
  * @param timestamp maximum version timestamp
  * @deprecated use {@link #deleteColumn(byte[], byte[], long)} instead
  * @return this for invocation chaining
  */
 public Delete deleteColumns(byte[] column, long timestamp) {
   byte[][] parts = KeyValue.parseColumn(column);
   this.deleteColumns(parts[0], parts[1], timestamp);
   return this;
 }