You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hbase.apache.org by "Bora, Venu" <ve...@epsilon.com> on 2014/11/04 22:30:55 UTC

How to findout Hbase PUT inserts a new Row or Update an exisiting row

Hello,
We have a requirement to determine whether a PUT will create a new row or update an existing one. I looked at using preBatchMutate in a co-processor and have the code below.

Few things I need to ask:
1) Is there a more efficient way of doing this?
2) Will region.getClosestRowBefore() add additional IO to go to disk? or will the row be in memory since the row lock was already acquired before preBatchMutate is called?
3) Will region.getClosestRowBefore() always give the correct result? Or are there scenarios where the previous state will not be visible?


    @Override
    public void preBatchMutate(ObserverContext<RegionCoprocessorEnvironment> c, MiniBatchOperationInProgress<Mutation> miniBatchOp) throws IOException {
        for (int i = 0; i < miniBatchOp.size(); i++) {
            Mutation operation = miniBatchOp.getOperation(i);
            byte[] rowKey = operation.getRow();
            NavigableMap<byte[], List<Cell>> familyCellMap = operation.getFamilyCellMap();

            for (Entry<byte[], List<Cell>> entry : familyCellMap.entrySet()) {
                for (Iterator<Cell> iterator = entry.getValue().iterator(); iterator.hasNext();) {
                    Cell cell = iterator.next();
                    byte[] family = CellUtil.cloneFamily(cell);
                    Result closestRowBefore = c.getEnvironment().getRegion().getClosestRowBefore(rowKey, family);
                    // closestRowBefore would return null if there is not record for the rowKey and family
                    if (closestRowBefore != null) {
                        // PUT is doing an update for the given rowKey, family
                    } else {
                        // PUT is doing an insert for the given rowKey, family
                    }
                }
            }
        }
        super.preBatchMutate(c, miniBatchOp);
    }


Thanks
Venu Bora


________________________________

This e-mail and files transmitted with it are confidential, and are intended solely for the use of the individual or entity to whom this e-mail is addressed. If you are not the intended recipient, or the employee or agent responsible to deliver it to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you are not one of the named recipient(s) or otherwise have reason to believe that you received this message in error, please immediately notify sender by e-mail, and destroy the original message. Thank You.