Java操作HBase

Java操作HBase

导入配置文件

在这里插入图片描述

代码

public class Demo
{
    Admin admin = null;
    Connection conn = null;

	//构造代码块
    {
        try
        {
            //创建配置
            Configuration conf = HBaseConfiguration.create();
            //设置zookeeper地址
            conf.set("hbase.zookeeper.quorum", "hadoop");
            conf.set("hbase.zookeeper.property.clientPort", "2181");
            //创建连接
            conn = ConnectionFactory.createConnection(conf);
            //创建admin
            admin = conn.getAdmin();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    //判断表是否存在
    @Test
    public void isTableExists() throws IOException
    {
        boolean exists = admin.tableExists(TableName.valueOf("tmpTable"));
        System.out.println(exists);
    }


    //创建表
    @Test
    public void createTable() throws IOException
    {
        //创建表描述信息
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("tmpTable"));
        //添加列簇1
        hTableDescriptor.addFamily(new HColumnDescriptor("cf1"));
        //添加列簇2
        hTableDescriptor.addFamily(new HColumnDescriptor("cf2"));
        //创建
        admin.createTable(hTableDescriptor);
    }


    //查看所有表信息
    @Test
    public void listTable() throws IOException
    {
        HTableDescriptor[] hTableDescriptors = admin.listTables();
        for (HTableDescriptor tableDescriptor : hTableDescriptors)
        {
            System.out.println(tableDescriptor.getNameAsString());
        }
    }


    //插入数据
    @Test
    public void putData2Tabel() throws IOException
    {
        //获取table
        Table table = conn.getTable(TableName.valueOf("tmpTable"));
        //创建一个put,添加row key
        Put put = new Put(Bytes.toBytes("1001"));
        //添加 列簇 , 列 , 值
        put.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan"));
        //插入数据
        table.put(put);
    }


    //查看一条数据
    @Test
    public void getTable() throws IOException
    {
        //获取table
        Table table = conn.getTable(TableName.valueOf("tmpTable"));
        //创建一个get
        Get get = new Get(Bytes.toBytes("1001"));
        //调用api执行get方法
        Result result = table.get(get);
        //遍历
        Cell[] cells = result.rawCells();
        for (Cell cell : cells)
        {
            System.out.println("row_key : " + Bytes.toString(CellUtil.cloneRow(cell)));
            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("---------------------------------------------");
        }
    }


    //扫描表
    @Test
    public void scanTable() throws IOException
    {
        //获取table
        Table table = conn.getTable(TableName.valueOf("tmpTable"));
        ResultScanner scanner = table.getScanner(new Scan());
        for (Result result : scanner)
        {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells)
            {
                System.out.println("row_key : " + Bytes.toString(CellUtil.cloneRow(cell)));
                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("---------------------------------------------");
            }
        }
    }


    //删除表
    @Test
    public void dropTable() throws IOException
    {
        //禁用表
        admin.disableTable(TableName.valueOf("tmpTable"));
        //删除
        admin.deleteTable(TableName.valueOf("tmpTable"));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值