Hbase shell - call hbase command on scan results

2019-07-27 06:40发布

I ran into a small issue here : - I can scan a table using filters from the shell - I can delete a row using hbase shell But I can't find a way to scan a table, iterate on the results and delete them. Is it even possible to 'pipe' commands in hbase shell or do I have to do a ruby script (note: I don't know how to do that)

What I want to do :

scan 'mytable', {COLUMNS => ['c:1],FILTER => "SingleColumnValueFilter('c', '1', =, 'binary:-1', true, true)"} | delete 'mytable', $result_i, 'c:1'

Thanks

标签: hbase
1条回答
走好不送
2楼-- · 2019-07-27 07:08

ok, I got it, I post the rb script here

require 'java'
require 'net/http'

import java.io.IOException

import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.HConstants
import org.apache.hadoop.hbase.MasterNotRunningException
import org.apache.hadoop.hbase.client.HBaseAdmin
import org.apache.hadoop.hbase.client.HTable
import org.apache.hadoop.hbase.client.Scan
import org.apache.hadoop.hbase.client.Delete
import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter
import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.util.Bytes
import org.apache.hadoop.hbase.util.Writables
import org.apache.hadoop.hbase.HTableDescriptor
import org.apache.hadoop.hbase.HColumnDescriptor

log_level = org.apache.log4j.Level::ERROR
org.apache.log4j.Logger.getLogger("org.apache.zookeeper").setLevel(log_level)
org.apache.log4j.Logger.getLogger("org.apache.hadoop.hbase").setLevel(log_level)

config = HBaseConfiguration.create
config.set 'fs.default.name', config.get(HConstants::HBASE_DIR)

TAB = 'mytable'.to_java_bytes
FAM = 'c'.to_java_bytes
ID = '1'.to_java_bytes

scan = Scan.new()

scan.cache_blocks = false
scan.caching = 10
scan.addColumn FAM, ID


filter = SingleColumnValueFilter.new(FAM, ID, CompareFilter::CompareOp.valueOf('EQUAL'), '-1'.to_java_bytes)
filter.setFilterIfMissing(true)

scan.setFilter(filter)


iter = nil 
table = nil
scanner = nil
while true
  begin
    table = HTable.new config, TAB
    scanner = table.getScanner(scan)    
    iter = scanner.iterator
    break
  rescue IOException => ioe
    print "Exception trying to scan #{TAB}: #{ioe}"
    sleep 1
  end
end

start = Time.at(java.util.Date.new.getTime/1000)
print "Start Time : " + start.inspect

while iter.hasNext
  result = iter.next
  delete = Delete.new result.getRow()
  delete.deleteColumn(FAM, ID)
  table.delete delete

end

scanner.close


ending = Time.at(java.util.Date.new.getTime/1000)
print "Start Time : " + ending.inspect
查看更多
登录 后发表回答