读取HFile 按Timestamp 删除 HBase 数据

  1. import java.io.IOException;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Calendar;  
  4. import java.util.LinkedList;  
  5. import java.util.concurrent.ExecutorService;  
  6. import java.util.concurrent.Executors;  
  7.   
  8. import org.apache.hadoop.conf.Configuration;  
  9. import org.apache.hadoop.fs.FileStatus;  
  10. import org.apache.hadoop.fs.FileSystem;  
  11. import org.apache.hadoop.fs.Path;  
  12. import org.apache.hadoop.hbase.HBaseConfiguration;  
  13. import org.apache.hadoop.hbase.HColumnDescriptor;  
  14. import org.apache.hadoop.hbase.HTableDescriptor;  
  15. import org.apache.hadoop.hbase.KeyValue;  
  16. import org.apache.hadoop.hbase.client.Delete;  
  17. import org.apache.hadoop.hbase.client.HTable;  
  18. import org.apache.hadoop.hbase.client.HTablePool;  
  19. import org.apache.hadoop.hbase.io.hfile.HFile;  
  20. import org.apache.hadoop.hbase.io.hfile.HFileScanner;  
  21. import org.apache.hadoop.hbase.util.Bytes;  
  22.   
  23. /** 
  24.  *  
  25.  * @author eryk 
  26.  * 
  27.  */  
  28. public class HFileDeleteData {  
  29.   
  30.     private FileSystem _fs;  
  31.   
  32.     public static HTablePool pool = null;  
  33.   
  34.     private LinkedList<Path> hfileList = new LinkedList<Path>();   
  35.   
  36.     public HFileDeleteData(FileSystem fs, String tableName) {  
  37.         this._fs = fs;  
  38.         _tableName = tableName;  
  39.     }  
  40.   
  41.     private final String HBASE_DIR = "/hbase/";  
  42.   
  43.     private String _tableName;  
  44.   
  45.     /** 
  46.      *  
  47.      * @param args 
  48.      * @throws IOException 
  49.      */  
  50.     public static void main(String[] args) throws IOException {  
  51.   
  52.         Configuration conf = new Configuration();  
  53.         FileSystem fs = FileSystem.get(conf);  
  54.   
  55.         // XXX args  
  56.         HFileDeleteData hd = new HFileDeleteData(fs, args[2]);  
  57.   
  58.         ExecutorService es = Executors.newFixedThreadPool(Integer.parseInt(args[1]));  
  59.   
  60.         Calendar calendar = Calendar.getInstance();  
  61.         calendar.add(Calendar.DAY_OF_MONTH, Integer.parseInt("-"+args[0]));  
  62.   
  63.         String date = new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());  
  64.           
  65.         System.out.println(date+"   ----    "+calendar.getTimeInMillis());  
  66.           
  67.         Configuration hbase_conf = HBaseConfiguration.create();  
  68.         HTable table = new HTable(hbase_conf, hd._tableName);  
  69.         HTableDescriptor desc = table.getTableDescriptor();  
  70.         HColumnDescriptor[] hcd = desc.getColumnFamilies();  
  71.         for (int i = 0; i < hcd.length; i++) {  
  72.             hd.fileList(hd.HBASE_DIR + hd._tableName, Bytes.toString(hcd[i].getName()));  
  73.         }  
  74.   
  75.         System.out.println("total hfile:" + hd.hfileList.size());  
  76.         HTable htable = getTable(hd._tableName);  
  77.         long time = calendar.getTimeInMillis();  
  78.         for (Path path : hd.hfileList) {  
  79.             Path pathNew = path;  
  80.             es.execute(hd.new DelRow(fs, htable, pathNew, time));  
  81.         }  
  82.   
  83.         table.flushCommits();  
  84.         table.close();  
  85.         es.shutdown();  
  86.     }  
  87.   
  88.     // read region hile path to hfileList  
  89.     /** 
  90.      *  
  91.      * @param path 
  92.      *            /hbase/tableName 
  93.      * @param cf 
  94.      *            table 
  95.      * @throws IOException 
  96.      */  
  97.     public void fileList(String path, String cf) throws IOException {  
  98.         FileStatus[] fileList = _fs.listStatus(new Path(path));  
  99.         for (int i = 0; i < fileList.length; i++) {  
  100.             FileStatus[] tmpList = _fs.listStatus(new Path(fileList[i].getPath().toString() + "/"  
  101.                     + cf));  
  102.             for (int j = 0; j < tmpList.length; j++) {  
  103.                 hfileList.add(tmpList[j].getPath());  
  104.             }  
  105.         }  
  106.     }  
  107.   
  108.     private static HTable getTable(String tableName) {  
  109.         Configuration conf = HBaseConfiguration.create();  
  110.         if (pool == null) {  
  111.             pool = new HTablePool(conf, 500);  
  112.         }  
  113.         return (HTable) pool.getTable(tableName);  
  114.     }  
  115.   
  116.     public class DelRow extends Thread {  
  117.   
  118.         private long deadline;  
  119.         private HTable table;  
  120.         private Path path;  
  121.         private FileSystem fs;  
  122.   
  123.         public DelRow(FileSystem fs, HTable table, Path hfile, long deadline) {  
  124.             this.fs = fs;  
  125.             this.table = table;  
  126.             this.path = hfile;  
  127.             this.deadline = deadline;  
  128.         }  
  129.   
  130.         @Override  
  131.         public void run() {  
  132.             long count = 0;  
  133.             HFile.Reader hreader;  
  134.             try {  
  135.                 hreader = new HFile.Reader(fs, path, nullfalse);  
  136.   
  137.                 hreader.loadFileInfo();  
  138.   
  139.                 HFileScanner hscanner = hreader.getScanner(falsefalse);  
  140.                 hscanner.seekTo();  
  141.                 while (hscanner.next()) {  
  142.                     KeyValue kv = hscanner.getKeyValue();  
  143.                     if (kv.getTimestamp() < deadline || kv.getValueLength() == 0) {  
  144.                         Delete d = new Delete(kv.getRow());  
  145.                         table.delete(d);  
  146.                         count++;  
  147.                     }  
  148.                 }  
  149.             } catch (IOException e) {  
  150.                 e.printStackTrace();  
  151.             }  
  152.             System.out.println("total delete:" + count);  
  153.         }  
  154.   
  155.     }  
  156. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值