Hbase-java-api

maven依赖

<dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.4.12</version>
    </dependency>

    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-client</artifactId>
      <version>1.3.6</version>
    </dependency>

    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-common</artifactId>
      <version>1.3.6</version>
    </dependency>

    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-protocol</artifactId>
      <version>1.3.6</version>
    </dependency>

    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-server</artifactId>
      <version>1.3.6</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>compile</scope>
    </dependency>
package com.cnnc.HbaseDemo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

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

public class Demo1 {
    Admin admin;
    String  tablename = "testt1";
    HTable table;
    @Before
    public void init() throws IOException {
        Configuration configuration = new Configuration();
        configuration.set("hbase.zookeeper.quorum","hadoop1,hadoop2,hadoop3");
//        admin = new HBaseAdmin(configuration);
        table = new HTable(configuration,tablename);
        admin = ConnectionFactory.createConnection(configuration).getAdmin();
    }

    @Test
    public void createTable() throws IOException {
//        判断表存在
        if(admin.tableExists(TableName.valueOf(tablename))){
//            删除前要先进行下线
            admin.disableTable(TableName.valueOf(tablename));
//            删除表
            admin.deleteTable(TableName.valueOf(tablename));
        }

        //表描述
        HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tablename));
//        列族描述
        HColumnDescriptor cdesc = new HColumnDescriptor("cf");
//        将列族添加进来
        cdesc.setMaxVersions(3);
//        设置最大的保留版本数
        desc.addFamily(cdesc);
        
        admin.createTable(desc);
    }

    @Test
    public void insertDB() throws IOException {
//        可增加可修改
        String rowkey = "1234567";
        Put put = new Put(rowkey.getBytes());
        put.addColumn("cf".getBytes(),"name".getBytes(),"xiaodong".getBytes());
        put.addColumn("cf".getBytes(),"gender".getBytes(),"male".getBytes());
        put.addColumn("cf".getBytes(),"age".getBytes(),"24".getBytes());
        table.put(put);
    }



    @Test
    public void deleteDB() throws IOException {
        String rowkey = "123456";
        Delete delete = new Delete(rowkey.getBytes());
        delete.addColumn("cf".getBytes(),"name".getBytes());
        table.delete(delete);
    }


    @Test
    public void getDB() throws IOException {
        String rowkey1 = "123456";
        String rowkey2 = "1234567";
        Collection<Get> list = new ArrayList<>();
        list.add(new Get(rowkey1.getBytes()));
        list.add(new Get(rowkey2.getBytes()));
//        get方法有两个构造方法,一个传列表List<Get>,一个传get对象引用
        Result[] res = table.get((List<Get>) list);
        for (Result i :res) {
            Cell cell = i.getColumnLatestCell("cf".getBytes(),"age".getBytes());
//            在这里引用cell的工具类cellUtil,使用工具类的cloneValue方法,获得对应cell的值,结果为Byte[],需要转化为String才能输出。
            System.out.println(new String(CellUtil.cloneValue(cell)));
        }
    }


    @Test
    public void getDB2() throws IOException {
        String rowkey1 = "123456";
        Get get = new Get(rowkey1.getBytes());
//        默认情况下会获取这个行的所有的数据,但是可以通过addColumn方式来。一般情况下是需要加addColumn方法的,只获取需要的列。
//        这样就只获取name的数据,其他的数据不获取。(age的值取不到)
        get.addColumn("cf".getBytes(),"name".getBytes());
        Result res = table.get(get);
        Cell cell1 = res.getColumnLatestCell("cf".getBytes(),"name".getBytes());
        System.out.println(new String(CellUtil.cloneValue(cell1)));
        Cell cell2 = res.getColumnLatestCell("cf".getBytes(),"age".getBytes());
        System.out.println(new String(CellUtil.cloneValue(cell2)));
    }


    @Test
//    浏览全表
    public void scanDB() throws IOException {
        Scan scan = new Scan();
        scan.setStartRow("123456".getBytes());
        scan.setStopRow("1234568".getBytes());
        ResultScanner res = table.getScanner(scan);
        //        第一种方式
//        Iterator<Result> iter= res.iterator();
//        while (iter.hasNext()){
//            Result r =  iter.next();
//            System.out.println(r.cellScanner());
//        }
//        第二种方式
//        for (Result r:res) {
//            System.out.println(r.advance());
//            System.out.println(r.cellScanner());
//        }

//        循环查看所有的列与值,把NavigableMap当作map一样处理
//        for (Result r:res) {
//            byte[] bytes ="cf".getBytes() ;
//            NavigableMap<byte[], byte[]> rFamilyMap = r.getFamilyMap(bytes);
//            Set<byte[]> e= rFamilyMap.keySet();
//            for (byte[] i:e){
//                System.out.println(new String(i));
//                System.out.println(new String(rFamilyMap.get(i)));
//            }
//        }
//        第二种方式
//        for (Result r:res) {
//            List<Cell> e = r.listCells();
//            Iterator<Cell> a = e.iterator();
//            while (a.hasNext()) {
//                Cell c = a.next();
//                System.out.println(new String(c.getQualifier()));
//                System.out.println(new String(c.getValue()));
//            }
//        }


    }


    @After
    public void destory() throws IOException {
        if(admin!=null){
            admin.close();
        }
    }
}

也可以通过HbaseAdmin来获取HbaseAdmin的对象来实现增删改查。但是与Admin的操作略有不同。但是影响不大,因为这个方法过期了,就不演示了。

api可以在hbase-1.3.6-bin\hbase-1.3.6\docs\apidocs\index.html中查到。
想把这些方法封装一下,看看有没有时间把。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值