Hbase javaAPI编程

这里主要是针对Hbase的java Api编程做一个记录。以便以后复习回顾。

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;

public class Hbase1 {
    //指定列族名和表名
    private static String SERIES = "info";
    private static String TABLENAME = "user";
    private static Connection conn;
    private static String hbaseIp = "192.168.1.11";

    public static void init() {
        Configuration config = HBaseConfiguration.create();
        //指定zookeeper的ip和端口
        config.set("hbase.zookeeper.quorum", hbaseIp);
        config.set("hbase.zookeeper.property.clientPort","2181");
        try {
            conn = ConnectionFactory.createConnection(config);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //创建hbase表
    public static void createTable(String tableName, String seriesStr) throws IllegalArgumentException, IOException {
        Admin admin = null;
        TableName table = TableName.valueOf(tableName);
            try {
            admin = conn.getAdmin();
            if (!admin.tableExists(table)) {
                System.out.println(tableName + " table not Exists");
                HTableDescriptor descriptor = new HTableDescriptor(table);
                String[] series = seriesStr.split(",");
                for (String s : series) {
                    descriptor.addFamily(new HColumnDescriptor(s.getBytes()));
                }
                admin.createTable(descriptor);
            }
        } catch(Exception e){
            e.printStackTrace();
            }
        finally {
            IOUtils.closeQuietly(admin);
        }
    }

    //添加数据
    public static void add(String rowKey, Map<String, Object> columns) throws IOException  {
        Table table = null;
        try {
            table = conn.getTable(TableName.valueOf(TABLENAME));
            Put put = new Put(Bytes.toBytes(rowKey));
            for (Map.Entry<String, Object> entry : columns.entrySet()) {
                put.addColumn(SERIES.getBytes(), Bytes.toBytes(entry.getKey()),
                        Bytes.toBytes(entry.getValue().toString()));
            }
            table.put(put);
        } finally {
            IOUtils.closeQuietly(table);
        }
    }

    //根据table查询所有数据
    public static void  getValueByTable() throws Exception {
        Table table = null;
        try {
            table = conn.getTable(TableName.valueOf(TABLENAME));
            ResultScanner rs = table.getScanner(new Scan());
            for (Result r : rs) {
                System.out.println("获得到rowkey:" + new String(r.getRow()));
                for (KeyValue keyValue : r.raw()) {
                    System.out.println(
                            "列:" + new String(keyValue.getFamily()) + "====值:" + new String(keyValue.getValue()));
                }
            }
        } finally {
            IOUtils.closeQuietly(table);
        }
    }

     //根据table查询所有数据另一种实现
    public static void  getValueByTable1() throws Exception {
        Table table = null;
        try {
            table = conn.getTable(TableName.valueOf(TABLENAME));
            ResultScanner rs = table.getScanner(new Scan());
            for (Result r : rs) {
                System.out.println("获得到rowkey:" + new String(r.getRow()));
                for (Cell cell: r.listCells()) {
                    System.out.println(
                            "列:" + new String(cell.getFamilyArray(),"utf-8"));
                }
            }
        } finally {
            IOUtils.closeQuietly(table);
        }
    }


    //根据rowkey获取数据
    public static Map<String, String> getAllValue(String rowKey) throws IllegalArgumentException, IOException {
        Table table = null;
        Map<String, String> resultMap;
        try {
            table = conn.getTable(TableName.valueOf(TABLENAME));
            Get get = new Get(Bytes.toBytes(rowKey));
            get.addFamily(SERIES.getBytes());
            Result res = table.get(get);
            Map<byte[], byte[]> result = res.getFamilyMap(SERIES.getBytes());
            Iterator<Entry<byte[], byte[]>> it = result.entrySet().iterator();
            resultMap = new HashMap<String, String>();
            while (it.hasNext()) {
                Entry<byte[], byte[]> entry = it.next();
                resultMap.put(Bytes.toString(entry.getKey()), Bytes.toString(entry.getValue()));
            }
        } finally {
            IOUtils.closeQuietly(table);
        }
        return resultMap;
    }

    //根据rowkey和column获取数据
    public static String getValueBySeries(String rowKey, String column) throws IllegalArgumentException, IOException {
        Table table = null;
        String resultStr = null;
        try {
            table = conn.getTable(TableName.valueOf(TABLENAME));
            Get get = new Get(Bytes.toBytes(rowKey));
            get.addColumn(Bytes.toBytes(SERIES), Bytes.toBytes(column));
            Result res = table.get(get);
            byte[] result = res.getValue(Bytes.toBytes(SERIES), Bytes.toBytes(column));
            resultStr = Bytes.toString(result);
        } finally {
            IOUtils.closeQuietly(table);
        }
        return resultStr;
    }


    //删除表
    public static void dropTable(String tableName) throws IOException {
        Admin admin = null;
        TableName table = TableName.valueOf(tableName);
        try {
            admin = conn.getAdmin();
            if (admin.tableExists(table)) {
                admin.disableTable(table);
                admin.deleteTable(table);
            }
        } finally {
            IOUtils.closeQuietly(admin);
        }
    }

    //根据过滤器查询
    public static void filterData(){
        Table table = null;
        try{
            table = conn.getTable(TableName.valueOf(TABLENAME));
            Scan scan = new Scan();
            //前缀过滤器
            Filter filter = new PrefixFilter(Bytes.toBytes("rk"));
            //分页过滤器,指定记录条数
            //filter = new PageFilter(3);

            //值比较器
            /*ByteArrayComparable comparable = null;
            comparable = new SubstringComparator("jiangyi");
            Filter filter = new SingleColumnValueFilter(Bytes.toBytes("info"),Bytes.toBytes("name"),
                    CompareFilter.CompareOp.EQUAL,comparable);
            scan.addColumn("info".getBytes(),"name".getBytes());*/
            scan.setFilter(filter);
            ResultScanner results = table.getScanner(scan);
            for(Result result : results){
                printData(result);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(table != null){
                org.apache.hadoop.io.IOUtils.closeStream(table);
            }
        }
    }

    //根据扫描器查询
    public static void scanData(){
        Table table = null;
        try{
            table = conn.getTable(TableName.valueOf(TABLENAME));
            Scan scan = new Scan();
            //查询范围内数据
//            scan.setStartRow(Bytes.toBytes("rowkey001"));
//            scan.setStopRow(Bytes.toBytes("rowkey002"));
            //指定列族
            scan.addFamily(Bytes.toBytes("info"));
            ResultScanner results = table.getScanner(scan);
            for(Result result : results){
                printData(result);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(table != null){
                org.apache.hadoop.io.IOUtils.closeStream(table);
            }
        }
    }

    public static void printData(Result result){
        for(Cell cell : result.rawCells()){
            System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println(new String(CellUtil.cloneRow(cell)));
            System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println(new String(CellUtil.cloneValue(cell)));
        }
    }


    public static void main(String[] args) throws Exception{
        init();

        //创建表
        createTable(TABLENAME, SERIES);

        //添加数据
//        String rowkey = "rowkey001";
//        Map<String,Object> columnMap = new HashMap<String, Object>();
//        columnMap.put("name","jiangyi");
//        columnMap.put("age","28");
//        add(rowkey,columnMap);


        //根据表查询所有数据
//        getValueByTable();
//
//
//        getValueByTable1();

       //根据行健获取所有数据
       /*Map<String, String> tempMap = getAllValue("rowkey001");
        System.out.println(tempMap);

       //根据行健和列名获取数据
        System.out.println(getValueBySeries("rowkey001","name"));*/

        //根据过滤器查询
        //filterData();

        //根据扫描器查询
//        scanData();

    }
}

以上就是对API编程的一些简单介绍。

<think>好的,用户想要查找关于HBase Java API编程实践和使用指南,包括示例代码。首先,我需要回顾之前的引用内容,看看有哪些相关的信息可以整合。 引用[1]提到将HBase Java API的操作封装成工具类,提高代码的可维护性。引用[2]和[3]提供了编程示例,对应HBase Shell的命令,如create、put、get等。引用[4]则涉及环境启动步骤。用户需要具体的实践指导和代码示例,所以应该结合这些引用内容,整理出步骤化的教程。 接下来,我需要确定用户可能的背景。他们可能已经安装了Hadoop和HBase,但需要确认环境配置是否正确。然后,用户可能需要从创建表、插入数据到查询等基本操作,以及如何封装工具类。此外,用户可能还关心代码结构和最佳实践,比如如何解耦工具类和业务逻辑,这点引用[1]已经提到。 需要确保示例代码符合Java API的使用方法,比如正确使用Configuration对象、Connection的管理,以及各个类如Admin、Table的正确用法。同时,要提醒用户异常处理和资源关闭,避免连接泄漏。 可能还需要注意版本兼容性,但引用中没有提到具体版本,所以保持通用。最后,用户可能需要进一步的问题,比如性能优化或高级功能,所以相关问题的部分需要涵盖这些方面。</think>### HBase Java API编程实践指南 #### 一、环境准备(前置条件) 1. 启动HDFS和HBase服务(需配置环境变量) ```shell # 根据引用[4]的操作指引 cd /usr/local/hadoop ./sbin/start-dfs.sh cd /usr/local/hbase ./bin/start-hbase.sh ``` 2. Maven依赖配置(核心依赖项) ```xml <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.4.9</version> </dependency> ``` #### 二、工具类封装实践(引用[1]最佳实践) ```java public class HBaseJavaAPIUtils { private static Connection connection; // 初始化连接池 static { Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "node1,node2,node3"); try { connection = ConnectionFactory.createConnection(config); } catch (IOException e) { e.printStackTrace(); } } // 创建表方法 public static void createTable(String tableName, String... colFamilies) throws IOException { try (Admin admin = connection.getAdmin()) { TableDescriptorBuilder tableDesc = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName)); for (String cf : colFamilies) { ColumnFamilyDescriptorBuilder colDesc = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf)); tableDesc.setColumnFamily(colDesc.build()); } admin.createTable(tableDesc.build()); } } // 其他方法(put/get/scan等)按相同模式封装 } ``` #### 三、基础操作示例(对应Shell命令实现) 1. **创建表**(对应`create`命令) ```java public class HBaseDemo { public static void main(String[] args) throws IOException { HBaseJavaAPIUtils.createTable("Student", "Info", "Grades"); } } ``` 2. **插入数据**(对应`put`命令) ```java public static void putData(String tableName, String rowKey, String colFamily, String qualifier, String value) throws IOException { try (Table table = connection.getTable(TableName.valueOf(tableName))) { Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(qualifier), Bytes.toBytes(value)); table.put(put); } } ``` 3. **查询数据**(对应`get`命令) ```java public static Result getData(String tableName, String rowKey) throws IOException { try (Table table = connection.getTable(TableName.valueOf(tableName))) { Get get = new Get(Bytes.toBytes(rowKey)); return table.get(get); } } ``` #### 四、工程化实践要点 1. **连接管理**:使用单例模式维护Connection对象(创建成本高) 2. **资源释放**:所有`Table`和`Admin`对象必须放在try-with-resources语句中 3. **字节转换**:统一使用`Bytes.toBytes()`进行数据类型转换 4. **异常处理**:建议封装自定义异常体系(如HBaseOpsException) #### 五、调试与验证(引用[3]方法) 1. 在Eclipse/IntelliJ中通过`Run As > Java Application`执行程序 2. 使用HBase Shell验证操作结果: ```shell hbase> list hbase> scan 'Student' ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值