DEMO-JavaAPI连接HBase

适用模块

客户端

具体说明

JavaAPI连接HBase

使用示例

##### 依赖配置
对应pom.xml文件依赖:

<dependency>
       <groupId>org.apache.hbase</groupId>
       <artifactId>hbase-client</artifactId>
       <version>${hbase.version}</version>
</dependency>
##### 代码示例
使用JavaAPI连接HBase的简单demo代码

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.security.UserGroupInformation;
import sun.security.krb5.internal.ktab.KeyTab;

import java.io.IOException;

import org.apache.hadoop.hbase.client.*;

public class ConnectHBaseDemo {

    public static Configuration conf;
    public static Connection connection;
    public static Admin admin;

    public static void main(String[] args) throws IOException {

        //初始化操作:kerberos认证、创建连接
        init();

        //获取所有表名
        admin = new HBaseAdmin(conf);
        HTableDescriptor[] tableDescriptor = admin.listTables();

        for (int i = 0; i < tableDescriptor.length; i++) {
            System.out.println(tableDescriptor[i].getNameAsString());
        }

        //关闭链接
        close();
}



    //初始化链接
    public static void init() {
        conf = HBaseConfiguration.create();
        conf.set("hadoop.security.authentication", "Kerberos");

        //指定keytab 、principal 以及 krb5.conf文件
        System.setProperty("java.security.krb5.conf", "src/main/resources/krb5.conf");
        System.setProperty("keytab", "src/main/resources/bdms.keytab");
        String keytab = System.getProperty("hbase_keytab");
        String prinpical = KeyTab.getInstance(new File(basePath.concat(System.getProperty("keytab")))).getOneName().getName();

        System.out.println(keytab);

        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("hbase.zookeeper.quorum", "bigdata-demo3.jd.163.org,bigdata-demo4.jd.163.org,bigdata-demo5.jd.163.org");
        conf.set("zookeeper.znode.parent","/hbase-secure");

        conf.set("hbase.security.authentication", "kerberos");
        conf.set("hbase.master.kerberos.principal", prinpical);
        conf.set("hbase.regionserver.kerberos.principal", prinpical);

//        conf.set("hbase.rpc.timeout", "2000");
//        conf.set("hbase.client.operation.timeout", "5000");
//        conf.set("hbase.client.scanner.timeout.period", "10000");


        //进行kerberos认证
        try {
            UserGroupInformation.setConfiguration(conf);
            UserGroupInformation.loginUserFromKeytab(principal, keytab);
            System.out.println("Kerberos 认证通过~~");
        } catch (IOException e) {
            //TODO Auto-generated catch block
            e.printStackTrace();
        }

        //创建连接
        try {
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //关闭连接
    public static void close() {
        try {
            if (null != connection)
                connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    //插入数据
    public static void addRow(String tableName,String rowkey,String colFamily,String col,String val) throws IOException {
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(Bytes.toBytes(rowkey));
        put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
        table.put(put);

        //批量插入
       /* List<Put> putList = new ArrayList<Put>();
        puts.add(put);
        table.put(putList);*/
        //可以不用每次都close,最后close掉。提高效率。
        table.close();
    }

    //删除数据
    public static void deleRow(String tableName,String rowkey,String colFamily,String col) throws IOException {
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(Bytes.toBytes(rowkey));
        //删除指定列族
        //delete.addFamily(Bytes.toBytes(colFamily));
        //删除指定列
        //delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
        table.delete(delete);
        //批量删除
       /* List<Delete> deleteList = new ArrayList<Delete>();
        deleteList.add(delete);
        table.delete(deleteList);*/
        table.close();
    }

    //根据rowkey查找数据
    public static void getData(String tableName,String rowkey,String colFamily,String col)throws  IOException{
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(Bytes.toBytes(rowkey));
        //获取指定列族数据
        //get.addFamily(Bytes.toBytes(colFamily));
        //获取指定列数据
        //get.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
        Result result = table.get(get);

        showCell(result);
        table.close();
    }

    //格式化输出
    public static void showCell(Result result){
        Cell[] cells = result.rawCells();
        for(Cell cell:cells){
            System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
            System.out.println("Timetamp:"+cell.getTimestamp()+" ");
            System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
            System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
            System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
        }
    }

    //批量查找数据
    public static void scanData(String tableName,String startRow,String stopRow)throws IOException{
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        //scan.setStartRow(Bytes.toBytes(startRow));
        //scan.setStopRow(Bytes.toBytes(stopRow));
        ResultScanner resultScanner = table.getScanner(scan);
        for(Result result : resultScanner){
            showCell(result);
        }
        table.close();
    }
}

作者:wangsong