java 调用hbase的api进行表操作

本文提供了一个HBase客户端操作的示例代码,包括表的创建、数据的增删查改等功能实现。通过该示例可以了解如何使用Java API与HBase进行交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package client;

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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class HBaseClientTest {

    private static final Logger logger = LoggerFactory.getLogger(HBaseClientTest.class);

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

    public static void main(String[] args) {
        System.out.println("Start ");
        init();

//        String tableName = "person";
////        String[] colsValue = {"food", "traffic", "work"};

        String tableName = "animal";
        String[] colsValue = {"food",  "action"};

        try {
            //创建一个表
//            createTable(tableName, colsValue);
            //列出所有的表
            listTables();

//            scanData(tableName, "ssf", "lbr");

//            getData(tableName, "ssf", "food", "breakfast");

//            deleRow(tableName, "ssf", "traffic", "work");

//            deleteTable(tableName);

        } catch (IOException e) {
            e.printStackTrace();
        }

        close();

        System.out.println("End ");
        //和test里一样测试即可
    }



    //删表
    public static void deleteTable(String tableName) throws IOException {
        init();
        TableName tn = TableName.valueOf(tableName);
        if (admin.tableExists(tn)) {
            admin.disableTable(tn);
            admin.deleteTable(tn);
        }
        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 deleRow(String tableName,String rowkey,String colFamily,String col) throws IOException {

        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{

        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 scanData(String tableName,String startRow,String stopRow)throws IOException{

        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();

        ResultScanner resultScanner = table.getScanner(scan);
        for(Result result : resultScanner){

            System.out.println("RowName Next ");
            showCell(result);
        }
        table.close();
        close();
    }



    public static void listTables() throws IOException {
        HTableDescriptor hTableDescriptors[] = admin.listTables();
        for(HTableDescriptor hTableDescriptor :hTableDescriptors){
            System.out.println(hTableDescriptor.getNameAsString());
        }
    }



    public static void testInsert(){
        try {
            insterRow("person", "lbr", "food", "breakfast", "milk and  bread");
            insterRow("person", "lbr", "food", "lunch", "noodle");
            insterRow("person", "lbr", "traffic", "work", "car");
            insterRow("person", "lbr", "traffic", "workoff", "Bicycle");
            insterRow("person", "lbr", "work", "morning", "coding");
            insterRow("person", "lbr", "work", "morning", "listening");

            insterRow("person", "tjw", "food", "breakfast", "milk and  bread");
            insterRow("person", "tjw", "food", "lunch", "noodle");
            insterRow("person", "tjw", "traffic", "work", "car");
            insterRow("person", "tjw", "traffic", "workoff", "Bicycle");
            insterRow("person", "tjw", "work", "morning", "coding");
            insterRow("person", "tjw", "work", "morning", "listening");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //初始化链接
    public static void init(){
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","10.154.2.192,10.154.2.193,10.154.2.194");
        configuration.set("hbase.zookeeper.property.clientPort","2181");
        configuration.set("zookeeper.znode.parent","/hbase-unsecure");

        try {
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    //建表
    public static void createTable(String tableNmae,String[] cols) throws IOException {

        TableName tableName = TableName.valueOf(tableNmae);

        if(admin.tableExists(tableName)){
            System.out.println("talbe is exists!");
        }else {
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            for(String col:cols){
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);
                hTableDescriptor.addFamily(hColumnDescriptor);
            }
            admin.createTable(hTableDescriptor);
        }
        close();
    }


    //插入数据
    public static void insterRow(String tableName,String rowkey, String colFamily, String col, String val) throws IOException {

        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);*/
        table.close();

    }







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

    }





}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值