• Hbase1.1.x Java版之批量查删操作


    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.*;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    
    import java.io.IOException;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.List;
    
    public class HBaseDemo {
    
        private static final String TABLE_NAME = "speech";
        // private static final String CF_DEFAULT = "cf1";
    //  public static final byte[] QUALIFIER = "col1".getBytes();
    //  private static final byte[] ROWKEY = "999".getBytes();
    
        public static void main(String[] args) {
            Configuration config = HBaseConfiguration.create();
            String zkAddress = "hb-bp17t03zm5o7p75s1-002.hbase.rds.aliyuncs.com:2181,hb-bp17t03zm5o7p75s1-003.hbase.rds.aliyuncs.com:2181,hb-bp17t03zm5o7p75s1-004.hbase.rds.aliyuncs.com:2181";
            config.set(HConstants.ZOOKEEPER_QUORUM, zkAddress);
            Connection connection = null;
    
            try {
                 connection = ConnectionFactory.createConnection(config);
                //
                // System.out.println("==========Get data==========");
                Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
                try {
                    // // Get data
                    // Get get = new Get(ROWKEY);
                    // Result r = table.get(get);
                    // List<Cell> cs = r.listCells();
                    // for (Cell cell : cs){
                    // String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
                    // //取行键
                    // long timestamp = cell.getTimestamp(); //取时间戳
                    // String family = Bytes.toString(CellUtil.cloneFamily(cell));
                    // //取到族列
                    // String qualifier =
                    // Bytes.toString(CellUtil.cloneQualifier(cell)); //取到修饰名
                    // String value = Bytes.toString(CellUtil.cloneValue(cell));
                    // //取到值
                    //
                    // System.out.println("===> rowKey : " + rowKey +", timestamp :
                    // "+timestamp + ", family : "+ family+", qualifier :
                    // "+qualifier+", value : " +value );
                    // }
    
                    DateFormat df3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
                    
                    // Scan data
                    System.out.println("==========Scan data Start==========");
                    System.out.println(df3.format(System.currentTimeMillis()));
                    String start = "rokid-12016-12-18 11:37:23010116004126";
                    String end = "rokid-12016-12-18 16:30:48010116000832";
                    Scan scan = new Scan(start.getBytes(),end.getBytes());
                    ResultScanner rs = table.getScanner(scan);
                    for (Result result : rs) {
                        List<Cell> cs1 = result.listCells();
                        for (Cell cell : cs1) {
                            String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
                            long timestamp = cell.getTimestamp();
                            String family = Bytes.toString(CellUtil.cloneFamily(cell));
                            String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
                            String value = Bytes.toString(CellUtil.cloneValue(cell));
                            //System.out.println(" ===> rowKey : " + rowKey + ",  timestamp : " + timestamp + ", family : "
                           //         + family + ", qualifier : " + qualifier + ", value : " + value);
                        }
                    }
                    System.out.println(df3.format(System.currentTimeMillis()));
                    System.out.println("==========Scan data Over==========");
                    config.setLong(HConstants.HBASE_REGIONSERVER_LEASE_PERIOD_KEY,3000000); 
                    // Delete data
                    //System.out.println("==========Delete data Start==========");
                    //System.out.println(df3.format(System.currentTimeMillis()));
    
                    //String start = "rokid-112017-01-18 03:34:590201011650000021";
                    //String end = "rokid-12016-02-08 00:24:46010116000035";
    
                    //Scan scan1 = new Scan(start.getBytes(),end.getBytes());
                    //ResultScanner rs1 = table.getScanner(scan1);
                    //List<Delete> ld = new ArrayList<Delete>(); 
                    //for (Result result : rs1) {
                    //    ld.add(new Delete(result.getRow()));
    //              //    List<Cell> cs1 = result.listCells();
    //              //    for (Cell cell : cs1) {
    //              //        table.delete(new Delete(CellUtil.cloneRow(cell)));
    //              //    }
                    //}
                    //System.out.println(ld.size());
                    //System.out.println(df3.format(System.currentTimeMillis()));
                    //table.delete(ld);
                    //System.out.println(df3.format(System.currentTimeMillis()));
                    //System.out.println("==========Delete data Over==========");
                } finally {
                    if (table != null)
                        table.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    文章参考:Hbase新版本JavaAPI编程实战及基本操作方法封装:

    http://blog.csdn.net/tanggao1314/article/details/51408166

    待续。。。

  • 相关阅读:
    javascript 数组Array对象
    使div浮动层显示在Select组件上面
    CSS中expression使用简介
    Error: Error #2176: 某些动作(如显示弹出窗口的动作)只能通过用户交互来调用
    用.Net处理xmlHttp发送异步请求
    使用反射动态创建对象及调用对象方法
    Asp.net实现在线截图(大图截取为小图)
    一个webproxy代理类
    Asp.net如何截屏
    用 ASP.NET 做网站截图(代码示例)
  • 原文地址:https://www.cnblogs.com/zhzhang/p/6869628.html
Copyright © 2020-2023  润新知