Hbase Java API使用

本文介绍了使用HBase Java API进行操作的实践案例,包括如何建表、增删改查及应用过滤器。建议参考《HBase权威指南》,尽管部分内容可能已过时,需注意更新方法。
摘要由CSDN通过智能技术生成

一、推荐书目:HBase权威指南

里面示例和说明挺详细的,但实际使用时会有个别过期方法,记得更换。


二、实例和说明

1.配置项
public static Configuration configuration;
static {
    configuration = HBaseConfiguration.create();
    //端口号8021
    configuration.set("hbase.zookeeper.property.clientPort", "8021");
    //设置zookeeper主机(单数性能好),主机之间以逗号分隔
    configuration.set("hbase.zookeeper.quorum", "192.168.1.100");
    //配置hbase的master主机名和端口
    configuration.set("hbase.master", "192.168.1.100:600000");
}


2.建表
public void createTable(String tableName, List<String> columnFamilyList) {
    try {
        //获取连接,连接可以在一批操作前创建并复用,此处为方便演示
        Connection connection = ConnectionFactory.createConnection(config);
        //实例化admin--------------------1
        //过期用法:HBaseAdmin admin = new HBaseAdmin(config);
        Admin admin = connection.getAdmin();
        //创建表TableDescriptor----------2
        HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
        //增加列族-----------------------3
        for (String columnFamilyName : columnFamilyList) {
            tableDescriptor.addFamily(new HColumnDescriptor(columnFamilyName));
        }
        //建表---------------------------4
        admin.createTable(tableDescriptor);
    } catch (ZooKeeperConnectionException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}


3.添加数据
3.1添加一行
public static void addRecord(String tableName, String rowKey, String family, String qualifier1, String value1,
        String qualifier2, String value2, String qualifier3, String value3) throws Exception {
    try {
        //获取连接
        Connection connection = ConnectionFactory.createConnection(config);
        //建立表对象,用于和HBase通信-------1
        //过期获取方式:HTable table = new HTable(configuration, tablename);
        Table table = connection.getTable(TableName.valueOf(tableName));
        //创建Put对象-----------------------2
        Put put = new Put(Bytes.toBytes(rowKey));
        //----------------------------------3
        //添加数据数据,add()函数三个参数分别代表:列族、列、值
        //put.add()方法已过期,最新使用addColumn()代替
        put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier1), Bytes.toBytes(value1));
        put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier2), Bytes.toBytes(value2));
        put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier3), Bytes.toBytes(value3));
        //保存------------------------------4
        table.put(put);
        //别忘了关闭------------------------5
        table.close();
        System.out.println("insert recored " + rowKey + " to table " + tableName + " ok.");
    } catch (IOException e) {
        e.printStackTrace();
    }
}


4.删除
4.1删除一行(多行)
 
 public static void deleteRow(String tablename, String rowkey) {
       try {
           //获取连接
           Connection connection = ConnectionFactory.createConnection(config);
           //建立表对象,用于和HBase通信-------1
           Table table = connection.getTable(TableName.valueOf(tableName));
           List list = new ArrayList();
           Delete d1 = new Delete(rowkey.getBytes());
           //删除多行则将多行rowkey加入列表----2
           list.add(d1);
           //删除------------------------------3
           table.delete(list);
           //----------------------------------4
           table.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }


4.2删表
     public static void dropTable(String tableName) {
        try {
            //获取连接,连接可以在一批操作前创建并复用,此处为方便演示
            Connection connection = ConnectionFactory.createConnection(config);
            //实例化admin--------------------1
            Admin admin = connection.getAdmin();
            //关闭表-------------------------2
            admin.disableTable(tableName);
            //删除表-------------------------3
            admin.deleteTable(tableName);
        } catch (MasterNotRunningException e) {
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


5.修改数据
   //修改表格数据(表中某一行的某一列)
    public void updateTable() throws IOException {
        //表名
        String tbName = "tableName";
        //列族
        String colFamily = "colFamily";
        //列名
        String column = "columnName";
        //rowKey
        String rowKey = "rowKey";
        //更新值
        String strNewValue = "NewValues";
        //获取连接
        Connection connection = ConnectionFactory.createConnection(config);
        //建立表对象,用于和HBase通信-------1
        Table table = connection.getTable(TableName.valueOf(tableName));
        //----------------------------------2
        Put put = new Put(Bytes.toBytes(rowKey));
        //插入操作(列族,列,新值)--------3
        put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(column), Bytes.toBytes(strNewValue));
        //----------------------------------4
        table.put(put);
        //----------------------------------5
        table.close();
    }


6.查询
6.1根据RowKey
   public void getRow() throws IOException {
        //表名
        String tbName = "tableName";
        //rowKey
        String rowKey = "rowKey";
        //-----------------------------------1
        HTable table = new HTable(configuration, tbName);
         
        //查询器,查询指定行-----------------2
        Get get = new Get(Bytes.toBytes(rowKey));
        //获取结果---------------------------3
        Result result = table.get(get);
        //指定行的所有列
        List<Cell> listCells = result.listCells();
         
        //遍历取值---------------------------4
        for (Cell cell : listCells) {
            System.out.println("列  族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列  名:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("列  值:" + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("时间戳:" + cell.getTimestamp());
        }
        //关闭table-------------------------5
        table.close();
    }


7.扫描
public  void scan(String tableName) throws IOException{
    //获取连接
    Connection connection = ConnectionFactory.createConnection(config);
     
    //建立表对象,用于和HBase通信--------1
    Table table = connection.getTable(TableName.valueOf(tableName));
    //建立Scan对象----------------------2
    Scan scan = new Scan();
    // 设置要查询的列族、列--------------3
    scan.addColumn(Bytes.toBytes("cardInfo"), Bytes.toBytes("cardNo"));
    // 获取结果-------------------------4
    ResultScanner resultScanner = table.getScanner(scan);
    // 读取结果-------------------------5
    for (Result result:resultScanner)
    System.out.println("Found row : " + result);
     
    //关闭ResultScanner----------------6
    resultScanner.close();
 }


8.过滤器
public void scan(String tableName) throws IOException {
    //获取连接
    Connection connection = ConnectionFactory.createConnection(config);
    //建立表对象,用于和HBase通信--------1
    Table table = connection.getTable(TableName.valueOf(tableName));
    //建立Scan对象----------------------2
    Scan scan = new Scan();
    // 设置要查询的列簇、列--------------3
    scan.addColumn(Bytes.toBytes("cardInfo"), Bytes.toBytes("cardNo"));
     
    //创建Filter------------------------3.1
    Filter filter = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
            new BinaryComparator(Bytes.toBytes("0")));
    //----------------------------------3.2
    scan.setFilter(filter);
     
    // 获取结果-------------------------4
    ResultScanner resultScanner = table.getScanner(scan);
    // 读取结果-------------------------5
    for (Result result : resultScanner)
        System.out.println("Found row : " + result);
    //关闭ResultScanner----------------6
    resultScanner.close();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值