Java Code Examples for org.apache.hadoop.hbase.client.Delete#getFamilyCellMap()

The following examples show how to use org.apache.hadoop.hbase.client.Delete#getFamilyCellMap() . 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: TestDefaultHBaseSerde.java    From envelope with Apache License 2.0 6 votes vote down vote up
@Test
public void testConvertToDelete() {
  byte[] rowKey = Bytes.add(Bytes.toBytes("GOOG:"), Bytes.toBytes(1000L));
  byte[] cf = Bytes.toBytes("cf1");
  byte[] clordid = Bytes.toBytes("clordid");
  byte[] orderqty = Bytes.toBytes("orderqty");
  byte[] leavesqty = Bytes.toBytes("leavesqty");
  byte[] cumqty = Bytes.toBytes("cumqty");
  Row row = new RowWithSchema(fullSchema, "GOOG", 1000L, "abcd", 100, 10, 5);
  Delete delete = serde.convertToDelete(row);

  Map<byte[], List<Cell>> contents = delete.getFamilyCellMap();

  assertArrayEquals("Row key should be GOOG:1000L", rowKey, delete.getRow());
  assertTrue("Delete contains cf1", contents.containsKey(cf));
  List<Cell> cells = contents.get(cf);
  assertEquals("Delete should have four cells", 4, cells.size());
  assertArrayEquals("Cell 0 should be cf1:clordid", clordid, CellUtil.cloneQualifier(cells.get(0)));
  assertArrayEquals("Cell 1 should be cf1:cumqty", cumqty, CellUtil.cloneQualifier(cells.get(1)));
  assertArrayEquals("Cell 2 should be cf1:leavesqty", leavesqty, CellUtil.cloneQualifier(cells.get(2)));
  assertArrayEquals("Cell 3 should be cf1:orderqty", orderqty, CellUtil.cloneQualifier(cells.get(3)));
}
 
Example 2
Source File: AccessController.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public void preDelete(final ObserverContext<RegionCoprocessorEnvironment> c,
    final Delete delete, final WALEdit edit, final Durability durability)
    throws IOException {
  // An ACL on a delete is useless, we shouldn't allow it
  if (delete.getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL) != null) {
    throw new DoNotRetryIOException("ACL on delete has no effect: " + delete.toString());
  }
  // Require WRITE permissions on all cells covered by the delete. Unlike
  // for Puts we need to check all visible prior versions, because a major
  // compaction could remove them. If the user doesn't have permission to
  // overwrite any of the visible versions ('visible' defined as not covered
  // by a tombstone already) then we have to disallow this operation.
  RegionCoprocessorEnvironment env = c.getEnvironment();
  Map<byte[],? extends Collection<Cell>> families = delete.getFamilyCellMap();
  User user = getActiveUser(c);
  AuthResult authResult = permissionGranted(OpType.DELETE,
      user, env, families, Action.WRITE);
  AccessChecker.logResult(authResult);
  if (!authResult.isAllowed()) {
    if (cellFeaturesEnabled && !compatibleEarlyTermination) {
      delete.setAttribute(CHECK_COVERING_PERM, TRUE);
    } else if (authorizationEnabled) {
      throw new AccessDeniedException("Insufficient permissions " +
        authResult.toContextString());
    }
  }
}
 
Example 3
Source File: SimpleRegionObserver.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public void preDelete(final ObserverContext<RegionCoprocessorEnvironment> c,
    final Delete delete, final WALEdit edit,
    final Durability durability) throws IOException {
  Map<byte[], List<Cell>> familyMap  = delete.getFamilyCellMap();
  RegionCoprocessorEnvironment e = c.getEnvironment();
  assertNotNull(e);
  assertNotNull(e.getRegion());
  assertNotNull(familyMap);
  if (ctBeforeDelete.get() > 0) {
    ctPreDeleted.incrementAndGet();
  }
}
 
Example 4
Source File: SimpleRegionObserver.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public void postDelete(final ObserverContext<RegionCoprocessorEnvironment> c,
    final Delete delete, final WALEdit edit,
    final Durability durability) throws IOException {
  Map<byte[], List<Cell>> familyMap  = delete.getFamilyCellMap();
  RegionCoprocessorEnvironment e = c.getEnvironment();
  assertNotNull(e);
  assertNotNull(e.getRegion());
  assertNotNull(familyMap);
  ctBeforeDelete.set(0);
  ctPostDeleted.incrementAndGet();
}
 
Example 5
Source File: RemoteHTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
private boolean doCheckAndDelete(byte[] row, byte[] family, byte[] qualifier, byte[] value,
    Delete delete) throws IOException {
  Put put = new Put(row, HConstants.LATEST_TIMESTAMP, delete.getFamilyCellMap());
  // column to check-the-value
  put.add(new KeyValue(row, family, qualifier, value));
  CellSetModel model = buildModelFromPut(put);
  StringBuilder sb = new StringBuilder();
  sb.append('/');
  sb.append(Bytes.toString(name));
  sb.append('/');
  sb.append(toURLEncodedBytes(row));
  sb.append("?check=delete");

  for (int i = 0; i < maxRetries; i++) {
    Response response =
      client.put(sb.toString(), Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput());
    int code = response.getCode();
    switch (code) {
      case 200:
        return true;
      case 304: // NOT-MODIFIED
        return false;
      case 509:
        try {
          Thread.sleep(sleepTime);
        } catch (final InterruptedException e) {
          throw (InterruptedIOException) new InterruptedIOException().initCause(e);
        }
        break;
      default:
        throw new IOException("checkAndDelete request failed with " + code);
    }
  }
  throw new IOException("checkAndDelete request timed out");
}
 
Example 6
Source File: MetaDataUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static Put cloneDeleteToPutAndAddColumn(Delete delete, byte[] family, byte[] qualifier, byte[] value) {
    NavigableMap<byte[], List<Cell>> familyCellMap = delete.getFamilyCellMap();
    List<Cell> cells = familyCellMap.get(family);
    Cell cell = Iterables.getFirst(cells, null);
    if (cell == null) {
        throw new RuntimeException("Empty cells for delete for family: " + Bytes.toStringBinary(family));
    }
    byte[] rowArray = new byte[cell.getRowLength()];
    System.arraycopy(cell.getRowArray(), cell.getRowOffset(), rowArray, 0, cell.getRowLength());
    Put put = new Put(rowArray, delete.getTimeStamp());
    put.addColumn(family, qualifier, delete.getTimeStamp(), value);
    return put;
}
 
Example 7
Source File: TTable.java    From phoenix-omid with Apache License 2.0 4 votes vote down vote up
private Put deleteInternal(Transaction tx, Delete delete) throws IOException {

        throwExceptionIfOpSetsTimerange(delete);

        HBaseTransaction transaction = enforceHBaseTransactionAsParam(tx);

        final long writeTimestamp = transaction.getWriteTimestamp();
        boolean deleteFamily = false;

        final Put deleteP = new Put(delete.getRow(), writeTimestamp);
        final Get deleteG = new Get(delete.getRow());
        propagateAttributes(delete, deleteP);
        propagateAttributes(delete, deleteG);
        Map<byte[], List<Cell>> fmap = delete.getFamilyCellMap();
        if (fmap.isEmpty()) {
            familyQualifierBasedDeletion(transaction, deleteP, deleteG);
        }

        for (List<Cell> cells : fmap.values()) {
            for (Cell cell : cells) {
                CellUtils.validateCell(cell, writeTimestamp);
                switch (KeyValue.Type.codeToType(cell.getTypeByte())) {
                    case DeleteColumn:
                        deleteP.addColumn(CellUtil.cloneFamily(cell),
                                    CellUtil.cloneQualifier(cell),
                                    writeTimestamp,
                                    CellUtils.DELETE_TOMBSTONE);
                        addWriteSetElement(transaction,
                            new HBaseCellId(this,
                                            delete.getRow(),
                                            CellUtil.cloneFamily(cell),
                                            CellUtil.cloneQualifier(cell),
                                            writeTimestamp));
                        break;
                    case DeleteFamily:
                        deleteG.addFamily(CellUtil.cloneFamily(cell));
                        deleteFamily = true;
                        break;
                    case Delete:
                        if (cell.getTimestamp() == HConstants.LATEST_TIMESTAMP) {
                            deleteP.addColumn(CellUtil.cloneFamily(cell),
                                        CellUtil.cloneQualifier(cell),
                                        writeTimestamp,
                                        CellUtils.DELETE_TOMBSTONE);
                            addWriteSetElement(transaction,
                                new HBaseCellId(this,
                                                delete.getRow(),
                                                CellUtil.cloneFamily(cell),
                                                CellUtil.cloneQualifier(cell),
                                                writeTimestamp));
                            break;
                        } else {
                            throw new UnsupportedOperationException(
                                "Cannot delete specific versions on Snapshot Isolation.");
                        }
                    default:
                        break;
                }
            }
        }
        if (deleteFamily) {
            if (enforceHBaseTransactionManagerAsParam(transaction.getTransactionManager()).
                    getConflictDetectionLevel() == ConflictDetectionLevel.ROW) {
                familyQualifierBasedDeletionWithOutRead(transaction, deleteP, deleteG);
            } else {
                familyQualifierBasedDeletion(transaction, deleteP, deleteG);
            }
        }

        return deleteP;
    }
 
Example 8
Source File: SolrRegionObserver.java    From SolrCoprocessor with Apache License 2.0 4 votes vote down vote up
@Override
public void postDelete(ObserverContext<RegionCoprocessorEnvironment> e, Delete delete, WALEdit edit,
    Durability durability) throws IOException {
	String tableName = e.getEnvironment().getRegion().getRegionInfo().getTable().getNameAsString();
	if (tableName.startsWith("hbase:")) { //Ԫ���ݱ�,����!
		return;
	}
	String rowKey = new String(delete.getRow());

	String cFamily = null;
	String cQualifier = null;
	NavigableMap<byte[], List<Cell>> map = delete.getFamilyCellMap();
	JsonObject jsonSet = new JsonObject();
	for (List<Cell> cells : map.values()) {
		for (Cell cell : cells) {
			cFamily = new String(CellUtil.cloneFamily(cell));
			cQualifier = new String(CellUtil.cloneQualifier(cell));
			if (cQualifier.endsWith("_s")) { //string
				jsonSet.putObject(cFamily + F_SEPARATOR + cQualifier, (new JsonObject()).putString("set", null));
			} else if (cQualifier.endsWith("_t")) { //text_general
				jsonSet.putObject(cFamily + F_SEPARATOR + cQualifier, (new JsonObject()).putString("set", null));
			} else if (cQualifier.endsWith("_dt")) { //date
				jsonSet.putObject(cFamily + F_SEPARATOR + cQualifier, (new JsonObject()).putString("set", null));
			} else if (cQualifier.endsWith("_i")) { //int
				jsonSet.putObject(cFamily + F_SEPARATOR + cQualifier, (new JsonObject()).putString("set", null));
			} else if (cQualifier.endsWith("_l")) { //long
				jsonSet.putObject(cFamily + F_SEPARATOR + cQualifier, (new JsonObject()).putString("set", null));
			} else if (cQualifier.endsWith("_f")) { //float
				jsonSet.putObject(cFamily + F_SEPARATOR + cQualifier, (new JsonObject()).putString("set", null));
			} else if (cQualifier.endsWith("_d")) { //double
				jsonSet.putObject(cFamily + F_SEPARATOR + cQualifier, (new JsonObject()).putString("set", null));
			} else if (cQualifier.endsWith("_b")) { //boolean
				jsonSet.putObject(cFamily + F_SEPARATOR + cQualifier, (new JsonObject()).putString("set", null));
			} else { //������Ҫ������,����!
				continue;
			}
		}
	}
	if (jsonSet.size() == 0) { //˵��û��solr�ֶ�
		if (delete.numFamilies() == e.getEnvironment().getRegion().getTableDesc().getFamilies().size()) { //˵����ɾ����
			JsonObject jsonDel = new JsonObject();
			jsonDel.putObject("delete", (new JsonObject()).putString("query", F_ID + ":\"" + tableName + F_SEPARATOR + rowKey + "\""));

			log.debug("postDelete!!! Row:" + jsonDel.encode());

			_bqDelete.enqueue(jsonDel.encode().getBytes(SolrTools.UTF_8));
		} else { //˵������ɾ����
			return;
		}
	} else {
		jsonSet.putString(F_ID, tableName + F_SEPARATOR + rowKey);
		jsonSet.putObject(F_UPDATETIME, (new JsonObject()).putString("set", SolrTools.solrDateFormat.format(new java.util.Date())));

		log.debug("postDelete!!! Column:" + jsonSet.encode());
		_bqUpdate.enqueue(jsonSet.encode().getBytes(SolrTools.UTF_8));
	}
}
 
Example 9
Source File: CoveredColumnsIndexBuilder.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<Pair<Mutation, byte[]>> getIndexUpdate(Delete d) throws IOException {
  // stores all the return values
  IndexUpdateManager updateMap = new IndexUpdateManager();

  // We have to figure out which kind of delete it is, since we need to do different things if its
  // a general (row) delete, versus a delete of just a single column or family
  Map<byte[], List<Cell>> families = d.getFamilyCellMap();

  /*
   * Option 1: its a row delete marker, so we just need to delete the most recent state for each
   * group, as of the specified timestamp in the delete. This can happen if we have a single row
   * update and it is part of a batch mutation (prepare doesn't happen until later... maybe a
   * bug?). In a single delete, this delete gets all the column families appended, so the family
   * map won't be empty by the time it gets here.
   */
  if (families.size() == 0) {
    LocalTableState state = new LocalTableState(env, localTable, d);
    // get a consistent view of name
    long now = d.getTimeStamp();
    if (now == HConstants.LATEST_TIMESTAMP) {
      now = EnvironmentEdgeManager.currentTimeMillis();
      // update the delete's idea of 'now' to be consistent with the index
      d.setTimestamp(now);
    }
    // get deletes from the codec
    // we only need to get deletes and not add puts because this delete covers all columns
    addDeleteUpdatesToMap(updateMap, state, now);

    /*
     * Update the current state for all the kvs in the delete. Generally, we would just iterate
     * the family map, but since we go here, the family map is empty! Therefore, we need to fake a
     * bunch of family deletes (just like hos HRegion#prepareDelete works). This is just needed
     * for current version of HBase that has an issue where the batch update doesn't update the
     * deletes before calling the hook.
     */
    byte[] deleteRow = d.getRow();
    for (byte[] family : this.env.getRegion().getTableDesc().getFamiliesKeys()) {
      state.addPendingUpdates(new KeyValue(deleteRow, family, null, now,
          KeyValue.Type.DeleteFamily));
    }
  } else {
    // Option 2: Its actually a bunch single updates, which can have different timestamps.
    // Therefore, we need to do something similar to the put case and batch by timestamp
    batchMutationAndAddUpdates(updateMap, d);
  }

  if (LOG.isDebugEnabled()) {
    LOG.debug("Found index updates for Delete: " + d + "\n" + updateMap);
  }

  return updateMap.toMap();
}