hbase 之java简单操作

相关依赖:

     <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>


    <!--hbase-->
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-client</artifactId>
      <version>2.2.4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-protocol</artifactId>
      <version>2.2.4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-common</artifactId>
      <version>2.2.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-mapreduce -->
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-mapreduce</artifactId>
      <version>2.2.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-zookeeper -->
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-zookeeper</artifactId>
      <version>2.2.4</version>
    </dependency>

代码:

package com.yuan;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Unit test for simple App.
 */

public class AppTest {

    private Configuration configuration = null;
    Connection connection = null;

    @Before
    public void setUp() throws Exception {

        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.220.128:2181");
        connection = ConnectionFactory.createConnection(configuration);

        System.out.println(" 开启连接");
    }


    /**
     *
     * 声明 :以下只是简单使用, 不做逻辑判断
     *
     * @throws IOException
     *
     */

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


    /**
     * 创建表
     *
     * @throws IOException
     */
    @Test
    public void create() throws IOException {
        //创建表描述器,表名需要转字节
        TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TableName.valueOf("table2"));
        //添加列族
        for (int i = 0; i < 5; i++) {
            ColumnFamilyDescriptor descriptor = ColumnFamilyDescriptorBuilder.of("yuan" + i);
            builder.setColumnFamily(descriptor);
        }

        //根据对表的配置,创建表
        connection.getAdmin().createTable(builder.build());

        System.out.println("表创建成功!");

    }

    /**
     * 启用表
     *
     * @throws IOException
     */
    @Test
    public void enabletable() throws IOException {
        //启用之前先禁用
       /* disabletable();*/
        connection.getAdmin().enableTable(TableName.valueOf("table2"));
        System.out.println("已经启用!");
    }

    /**
     * 禁用表
     *
     * @throws IOException
     */
    @Test
    public void disabletable() throws IOException {
        connection.getAdmin().disableTable(TableName.valueOf("table2"));
        System.out.println("已禁用!");
    }


    /**
     * 删除表
     *
     * @throws IOException
     */
    @Test
    public void delettable() throws IOException {

        //禁用表
        disabletable();

        //删除表
        connection.getAdmin().deleteTable(TableName.valueOf("table2"));
        System.out.println("删除成功!!");

    }

    /**
     * 列出所以表
     *
     * @throws IOException
     */
    @Test
    public void listTable() throws IOException {

        TableName[] tableNames = connection.getAdmin().listTableNames();
        for (TableName name : tableNames) {
            System.out.println(name);

        }

    }

    /**
     * 指定表中插入数据
     *
     * @throws IOException
     */
    @Test
    public void addData() throws IOException {
     //创建HTable对象
        Table table = connection.getTable(TableName.valueOf("table"));
        //向表中插入数据
        Put put = new Put(Bytes.toBytes("row2"));
        //向Put对象中组装数据
        put.addColumn(Bytes.toBytes("c1"), Bytes.toBytes("c1"), Bytes.toBytes("hhaha"));
        table.put(put);
        table.close();
        System.out.println("插入数据成功");


    }

    /**
     * 表中添加多条数据
     *
     * @throws IOException
     */
    @Test
    public void addSomeData() throws IOException {

        //创建HTable对象
        Table table = connection.getTable(TableName.valueOf("table"));

        Put put = null;
        for (int i = 0; i < 100; i++) {
            put = new Put(Bytes.toBytes("row" + i));
            //向Put对象中组装数据
            put.addColumn(Bytes.toBytes("c1"), Bytes.toBytes("c" + i), Bytes.toBytes("" + i));
            table.put(put);

        }

        table.close();
        System.out.println("插入数据成功");


    }

    /**
     * 获取某一行指定列的数据
     *
     * @throws IOException
     */
    @Test
    public void getData() throws IOException {
        //创建HTable对象
        Table table = connection.getTable(TableName.valueOf("table"));
        Get get = new Get(Bytes.toBytes("row1"));
        get.addColumn(Bytes.toBytes("c1"), Bytes.toBytes("c1"));
        Result result = table.get(get);
        for (Cell cell : result.rawCells()) {
            System.out.println("行键:" + Bytes.toString(result.getRow()));
            System.out.println("\t列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("\t列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("\t值:" + Bytes.toString(CellUtil.cloneValue(cell)));
        }
        table.close();

    }

    /**
     * 获取一行数据
     *
     * @throws IOException
     */
    @Test
    public void getRow() throws IOException {
        Table table = connection.getTable(TableName.valueOf("table"));

        Get get = new Get(Bytes.toBytes("row1"));
        Result result = table.get(get);
        for (Cell cell : result.rawCells()) {
            System.out.print("行键:" + Bytes.toString(result.getRow()));
            System.out.print("\t列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.print("\t列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.print("\t值:" + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("\t时间戳:" + cell.getTimestamp());
        }
        table.close();

    }

    /**
     * 获取所有数据
     *
     * @throws IOException
     */
    @Test
    public void getAllRows() throws IOException {
        Table table = connection.getTable(TableName.valueOf("table"));
        //得到用于扫描region的对象
        Scan scan = new Scan();
        //获取所有版本的数据
        scan.readAllVersions();
        //使用HTable得到resultcanner实现类的对象
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                //得到rowkey
                System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));
                //得到列族
                System.out.print("\t列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.print("\t列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("\t值:" + Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
        table.close();

    }

    /**
     * 删除一行数据
     */
    @Test
    public void deletDate() throws IOException {
        //创建HTable对象
        Table table = connection.getTable(TableName.valueOf("table"));

        Delete delete = new Delete(Bytes.toBytes("row1"));

        table.delete(delete);
        table.close();
        System.out.println("删除数据成功");

    }


    /**
     * 自定义统计记录
     */

    @Test
    public void conut() throws IOException {
        //创建HTable对象
        Table table = connection.getTable(TableName.valueOf("table"));
        Scan scan = new Scan();
        //获取所有版本的数据
        scan.readAllVersions();
        int n = 0; //统计数量
        //使用HTable得到resultcanner实现类的对象
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result res : resultScanner) {

            n++;
        }
        System.out.println("共:"+n+" 条记录!");
    }



    /**
     * 删除多条数据
     *
     * @throws IOException
     */
    @Test
    public void deletSomeDate() throws IOException {
        //创建HTable对象
        Table table = connection.getTable(TableName.valueOf("table"));
        List<String> rows = new ArrayList<>();
        rows.add("row0");
        rows.add("row1");
        rows.add("row2");
        rows.add("row3");
        rows.add("row4");
        rows.add("row5");
        rows.add("row6");

        List<Delete> deleteList = new ArrayList<>();
        for (String row : rows) {
            Delete delete = new Delete(Bytes.toBytes(row));
            deleteList.add(delete);
        }
        table.delete(deleteList);
        table.close();
        System.out.println("删除数据成功");

    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

缘不易

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值