Java Code Examples for org.apache.hadoop.hbase.client.RowMutations#add()

The following examples show how to use org.apache.hadoop.hbase.client.RowMutations#add() . 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: HBaseDataIndexWriter.java    From geowave with Apache License 2.0 6 votes vote down vote up
private RowMutations rowToMutation(final GeoWaveRow row) {
  final RowMutations mutation = new RowMutations(row.getDataId());
  for (final GeoWaveValue value : row.getFieldValues()) {
    final Put put = new Put(row.getDataId());
    // visibility is in the visibility column so no need to serialize it with the value
    put.addColumn(
        StringUtils.stringToBinary(ByteArrayUtils.shortToString(row.getAdapterId())),
        new byte[0],
        DataIndexUtils.serializeDataIndexValue(value, false));
    if ((value.getVisibility() != null) && (value.getVisibility().length > 0)) {
      put.setCellVisibility(
          new CellVisibility(StringUtils.stringFromBinary(value.getVisibility())));
    }
    try {
      mutation.add(put);
    } catch (final IOException e) {
      LOGGER.error("Error creating HBase row mutation: " + e.getMessage());
    }
  }

  return mutation;
}
 
Example 2
Source File: HBaseFsck.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Reset the split parent region info in meta table
 */
private void resetSplitParent(HbckRegionInfo hi) throws IOException {
  RowMutations mutations = new RowMutations(hi.getMetaEntry().getRegionInfo().getRegionName());
  Delete d = new Delete(hi.getMetaEntry().getRegionInfo().getRegionName());
  d.addColumn(HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER);
  d.addColumn(HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER);
  mutations.add(d);

  RegionInfo hri = RegionInfoBuilder.newBuilder(hi.getMetaEntry().getRegionInfo())
    .setOffline(false).setSplit(false).build();
  Put p = MetaTableAccessor.makePutFromRegionInfo(hri, EnvironmentEdgeManager.currentTime());
  mutations.add(p);

  meta.mutateRow(mutations);
  LOG.info("Reset split parent " + hi.getMetaEntry().getRegionInfo().getRegionNameAsString() +
    " in META");
}
 
Example 3
Source File: ThriftTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public boolean thenPut(Put put) throws IOException {
  preCheck();
  RowMutations rowMutations = new RowMutations(put.getRow());
  rowMutations.add(put);
  return checkAndMutate(row, family, qualifier, op, value, rowMutations);
}
 
Example 4
Source File: HBaseUtils.java    From geowave with Apache License 2.0 5 votes vote down vote up
public static RowMutations getDeleteMutations(
    final byte[] rowId,
    final byte[] columnFamily,
    final byte[] columnQualifier,
    final String[] authorizations) throws IOException {
  final RowMutations m = new RowMutations(rowId);
  final Delete d = new Delete(rowId);
  d.addColumns(columnFamily, columnQualifier);
  m.add(d);
  return m;
}
 
Example 5
Source File: PerformanceEvaluation.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
boolean testRow(final int i) throws IOException {
  final byte [] bytes = format(i);
  // checkAndXXX tests operate on only a single value
  // Put a known value so when we go to check it, it is there.
  Put put = new Put(bytes);
  put.addColumn(FAMILY_ZERO, getQualifier(), bytes);
  this.table.put(put);
  RowMutations mutations = new RowMutations(bytes);
  mutations.add(put);
  this.table.checkAndMutate(bytes, FAMILY_ZERO).qualifier(getQualifier())
      .ifEquals(bytes).thenMutate(mutations);
  return true;
}
 
Example 6
Source File: TestRegionObserverInterface.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testRowMutation() throws IOException {
  final TableName tableName = TableName.valueOf(TEST_TABLE.getNameAsString() + "." + name.getMethodName());
  Table table = util.createTable(tableName, new byte[][] { A, B, C });
  try {
    verifyMethodResult(SimpleRegionObserver.class,
      new String[] { "hadPreGet", "hadPostGet", "hadPrePut", "hadPostPut", "hadDeleted" },
      tableName, new Boolean[] { false, false, false, false, false });
    Put put = new Put(ROW);
    put.addColumn(A, A, A);
    put.addColumn(B, B, B);
    put.addColumn(C, C, C);

    Delete delete = new Delete(ROW);
    delete.addColumn(A, A);
    delete.addColumn(B, B);
    delete.addColumn(C, C);

    RowMutations arm = new RowMutations(ROW);
    arm.add(put);
    arm.add(delete);
    table.mutateRow(arm);

    verifyMethodResult(SimpleRegionObserver.class,
      new String[] { "hadPreGet", "hadPostGet", "hadPrePut", "hadPostPut", "hadDeleted" },
      tableName, new Boolean[] { false, false, true, true, true });
  } finally {
    util.deleteTable(tableName);
    table.close();
  }
}
 
Example 7
Source File: TestVisibilityLabels.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testFlushedFileWithVisibilityTags() throws Exception {
  final byte[] qual2 = Bytes.toBytes("qual2");
  TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(tableName);
  ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor familyDescriptor =
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(fam);
  tableDescriptor.setColumnFamily(familyDescriptor);
  TEST_UTIL.getAdmin().createTable(tableDescriptor);
  try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
    Put p1 = new Put(row1);
    p1.addColumn(fam, qual, value);
    p1.setCellVisibility(new CellVisibility(CONFIDENTIAL));

    Put p2 = new Put(row1);
    p2.addColumn(fam, qual2, value);
    p2.setCellVisibility(new CellVisibility(SECRET));

    RowMutations rm = new RowMutations(row1);
    rm.add(p1);
    rm.add(p2);

    table.mutateRow(rm);
  }
  TEST_UTIL.getAdmin().flush(tableName);
  List<HRegion> regions = TEST_UTIL.getHBaseCluster().getRegions(tableName);
  HStore store = regions.get(0).getStore(fam);
  Collection<HStoreFile> storefiles = store.getStorefiles();
  assertTrue(storefiles.size() > 0);
  for (HStoreFile storeFile : storefiles) {
    assertTrue(storeFile.getReader().getHFileReader().getFileContext().isIncludesTags());
  }
}
 
Example 8
Source File: TestVisibilityLabels.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testMutateRow() throws Exception {
  final byte[] qual2 = Bytes.toBytes("qual2");
  TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(tableName);
  ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor familyDescriptor =
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(fam);
  tableDescriptor.setColumnFamily(familyDescriptor);
  TEST_UTIL.getAdmin().createTable(tableDescriptor);
  try (Table table = TEST_UTIL.getConnection().getTable(tableName)){
    Put p1 = new Put(row1);
    p1.addColumn(fam, qual, value);
    p1.setCellVisibility(new CellVisibility(CONFIDENTIAL));

    Put p2 = new Put(row1);
    p2.addColumn(fam, qual2, value);
    p2.setCellVisibility(new CellVisibility(SECRET));

    RowMutations rm = new RowMutations(row1);
    rm.add(p1);
    rm.add(p2);

    table.mutateRow(rm);

    Get get = new Get(row1);
    get.setAuthorizations(new Authorizations(CONFIDENTIAL));
    Result result = table.get(get);
    assertTrue(result.containsColumn(fam, qual));
    assertFalse(result.containsColumn(fam, qual2));

    get.setAuthorizations(new Authorizations(SECRET));
    result = table.get(get);
    assertFalse(result.containsColumn(fam, qual));
    assertTrue(result.containsColumn(fam, qual2));
  }
}
 
Example 9
Source File: ThriftTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public boolean thenDelete(Delete delete) throws IOException {
  preCheck();
  RowMutations rowMutations = new RowMutations(delete.getRow());
  rowMutations.add(delete);
  return checkAndMutate(row, family, qualifier, op, value, rowMutations);
}
 
Example 10
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public void mutateRow(RowMutations rm) throws IOException {
  if (tx == null) {
    throw new IOException("Transaction not started");
  }
  RowMutations transactionalMutations = new RowMutations();
  for (Mutation mutation : rm.getMutations()) {
    if (mutation instanceof Put) {
      transactionalMutations.add(transactionalizeAction((Put) mutation));
    } else if (mutation instanceof Delete) {
      transactionalMutations.add(transactionalizeAction((Delete) mutation));
    }
  }
  hTable.mutateRow(transactionalMutations);
}
 
Example 11
Source File: ThriftUtilities.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link RowMutations} (HBase) from a {@link TRowMutations} (Thrift)
 *
 * @param in the <code>TRowMutations</code> to convert
 *
 * @return converted <code>RowMutations</code>
 */
public static RowMutations rowMutationsFromThrift(TRowMutations in) throws IOException {
  List<TMutation> mutations = in.getMutations();
  RowMutations out = new RowMutations(in.getRow(), mutations.size());
  for (TMutation mutation : mutations) {
    if (mutation.isSetPut()) {
      out.add(putFromThrift(mutation.getPut()));
    }
    if (mutation.isSetDeleteSingle()) {
      out.add(deleteFromThrift(mutation.getDeleteSingle()));
    }
  }
  return out;
}
 
Example 12
Source File: WriteConditionally.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void writeConditionally(String projectId, String instanceId, String tableId) {
  // String projectId = "my-project-id";
  // String instanceId = "my-instance-id";
  // String tableId = "mobile-time-series";

  try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
    Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId)));
    long timestamp = System.currentTimeMillis();

    String rowKey = "phone#4c410523#20190501";
    RowMutations mutations = new RowMutations(Bytes.toBytes(rowKey));

    Put put = new Put(Bytes.toBytes(rowKey));
    put.addColumn(
        COLUMN_FAMILY_NAME, Bytes.toBytes("os_name"), timestamp, Bytes.toBytes("android"));
    mutations.add(put);

    table.checkAndMutate(
        Bytes.toBytes(rowKey),
        COLUMN_FAMILY_NAME,
        Bytes.toBytes("os_build"),
        CompareOp.GREATER_OR_EQUAL,
        Bytes.toBytes("PQ2A.190405"),
        mutations);

    System.out.print("Successfully updated row's os_name");

  } catch (Exception e) {
    System.out.println("Error during WriteConditionally: \n" + e.toString());
    e.printStackTrace();
  }
}
 
Example 13
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public void mutateRow(RowMutations rm) throws IOException {
  if (tx == null) {
    throw new IOException("Transaction not started");
  }
  RowMutations transactionalMutations = new RowMutations();
  for (Mutation mutation : rm.getMutations()) {
    if (mutation instanceof Put) {
      transactionalMutations.add(transactionalizeAction((Put) mutation));
    } else if (mutation instanceof Delete) {
      transactionalMutations.add(transactionalizeAction((Delete) mutation));
    }
  }
  hTable.mutateRow(transactionalMutations);
}
 
Example 14
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public void mutateRow(RowMutations rm) throws IOException {
  if (tx == null) {
    throw new IOException("Transaction not started");
  }
  RowMutations transactionalMutations = new RowMutations();
  for (Mutation mutation : rm.getMutations()) {
    if (mutation instanceof Put) {
      transactionalMutations.add(transactionalizeAction((Put) mutation));
    } else if (mutation instanceof Delete) {
      transactionalMutations.add(transactionalizeAction((Delete) mutation));
    }
  }
  hTable.mutateRow(transactionalMutations);
}
 
Example 15
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public void mutateRow(RowMutations rm) throws IOException {
  if (tx == null) {
    throw new IOException("Transaction not started");
  }
  RowMutations transactionalMutations = new RowMutations();
  for (Mutation mutation : rm.getMutations()) {
    if (mutation instanceof Put) {
      transactionalMutations.add(transactionalizeAction((Put) mutation));
    } else if (mutation instanceof Delete) {
      transactionalMutations.add(transactionalizeAction((Delete) mutation));
    }
  }
  hTable.mutateRow(transactionalMutations);
}
 
Example 16
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public void mutateRow(RowMutations rm) throws IOException {
  if (tx == null) {
    throw new IOException("Transaction not started");
  }
  RowMutations transactionalMutations = new RowMutations();
  for (Mutation mutation : rm.getMutations()) {
    if (mutation instanceof Put) {
      transactionalMutations.add(transactionalizeAction((Put) mutation));
    } else if (mutation instanceof Delete) {
      transactionalMutations.add(transactionalizeAction((Delete) mutation));
    }
  }
  hTable.mutateRow(transactionalMutations);
}
 
Example 17
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public void mutateRow(RowMutations rm) throws IOException {
  if (tx == null) {
    throw new IOException("Transaction not started");
  }
  RowMutations transactionalMutations = new RowMutations(rm.getRow());
  for (Mutation mutation : rm.getMutations()) {
    if (mutation instanceof Put) {
      transactionalMutations.add(transactionalizeAction((Put) mutation));
    } else if (mutation instanceof Delete) {
      transactionalMutations.add(transactionalizeAction((Delete) mutation));
    }
  }
  hTable.mutateRow(transactionalMutations);
}
 
Example 18
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public void mutateRow(RowMutations rm) throws IOException {
  if (tx == null) {
    throw new IOException("Transaction not started");
  }
  RowMutations transactionalMutations = new RowMutations();
  for (Mutation mutation : rm.getMutations()) {
    if (mutation instanceof Put) {
      transactionalMutations.add(transactionalizeAction((Put) mutation));
    } else if (mutation instanceof Delete) {
      transactionalMutations.add(transactionalizeAction((Delete) mutation));
    }
  }
  hTable.mutateRow(transactionalMutations);
}
 
Example 19
Source File: TransactionAwareHTable.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@Override
public void mutateRow(RowMutations rm) throws IOException {
  if (tx == null) {
    throw new IOException("Transaction not started");
  }
  RowMutations transactionalMutations = new RowMutations();
  for (Mutation mutation : rm.getMutations()) {
    if (mutation instanceof Put) {
      transactionalMutations.add(transactionalizeAction((Put) mutation));
    } else if (mutation instanceof Delete) {
      transactionalMutations.add(transactionalizeAction((Delete) mutation));
    }
  }
  hTable.mutateRow(transactionalMutations);
}
 
Example 20
Source File: TestMutateRowsRecovery.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void MutateRowsAndCheckPostKill() throws IOException, InterruptedException {
  final TableName tableName = TableName.valueOf("test");
  Admin admin = null;
  Table hTable = null;
  try {
    admin = connection.getAdmin();
    hTable = connection.getTable(tableName);
    TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
      new TableDescriptorBuilder.ModifyableTableDescriptor(tableName);
    tableDescriptor.setColumnFamily(
      new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(fam1));
    admin.createTable(tableDescriptor);

    // Add a multi
    RowMutations rm = new RowMutations(row1);
    Put p1 = new Put(row1);
    p1.addColumn(fam1, qual1, value1);
    p1.setDurability(Durability.SYNC_WAL);
    rm.add(p1);
    hTable.mutateRow(rm);

    // Add a put
    Put p2 = new Put(row1);
    p2.addColumn(fam1, qual2, value2);
    p2.setDurability(Durability.SYNC_WAL);
    hTable.put(p2);

    HRegionServer rs1 = TESTING_UTIL.getRSForFirstRegionInTable(tableName);
    long now = EnvironmentEdgeManager.currentTime();
    // Send the RS Load to ensure correct lastflushedseqid for stores
    rs1.tryRegionServerReport(now - 30000, now);
    // Kill the RS to trigger wal replay
    cluster.killRegionServer(rs1.serverName);

    // Ensure correct data exists
    Get g1 = new Get(row1);
    Result result = hTable.get(g1);
    assertTrue(result.getValue(fam1, qual1) != null);
    assertEquals(0, Bytes.compareTo(result.getValue(fam1, qual1), value1));
    assertTrue(result.getValue(fam1, qual2) != null);
    assertEquals(0, Bytes.compareTo(result.getValue(fam1, qual2), value2));
  } finally {
    if (admin != null) {
      admin.close();
    }
    if (hTable != null) {
      hTable.close();
    }
  }
}