编程语言
首页 > 编程语言> > HBase - JAVA -API

HBase - JAVA -API

作者:互联网

文章目录

一、几个主要的HBase API类和数据模型之间的对应关系

在这里插入图片描述

二、使用java代码操作HBase

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.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

1、初始化连接

public class HBaseAPI {
    Connection conn;
    TableName table = TableName.valueOf("test_cre");
    @Before
    public void init() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","node1,node2,master");
        //获取连接
        conn = ConnectionFactory.createConnection(conf);

    }

2、创建表

@Test
public void create() throws IOException {
    Admin admin = conn.getAdmin();
    if (!admin.tableExists(TableName.valueOf("test_cre"))){
        //创建一个表结构
        HTableDescriptor test_cre = new HTableDescriptor(TableName.valueOf("test_cre"));
        //创建一个列簇
        HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
        //设置版本
        cf1.setMaxVersions(3);
        //设置过期时间
        cf1.setTimeToLive(1000);
        test_cre.addFamily(cf1);
        admin.createTable(test_cre);
    }else{
        System.out.println("表已存在");
    }
}

3、删除表

@Test
public void deleteTable() throws IOException {
    Admin admin = conn.getAdmin();
    if (admin.tableExists(TableName.valueOf("test_cre"))){
        admin.disableTable(TableName.valueOf("test_cre"));
        admin.deleteTable(TableName.valueOf("test_cre"));
    }else{
        System.out.println("表不存在");
    }
}

4、修改表

@Test
public void alterTable() throws IOException {
    Admin admin = conn.getAdmin();
    //获取表的原有结构
    HTableDescriptor tableDescriptor = admin.getTableDescriptor(table);
    //获取所有列簇构成的HColumnDescriptor数组
    HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();
    //遍历所有的列簇
    for (HColumnDescriptor columnFamily : columnFamilies) {
        //获取列簇的名称
        String cfName = columnFamily.getNameAsString();
        //对名字为cf1的列簇进行修改
        if ("cf1".equals(cfName)){
            columnFamily.setTimeToLive(10000);
        }
        //修改表结构(modifyTable)
        admin.modifyTable(table,tableDescriptor);
    }
}

5、添加数据put

@Test
public void Put() throws IOException {
Table test_cre = conn.getTable(table);
Put put = new Put(“001”.getBytes());
put.addColumn(“cf1”.getBytes(),“name”.getBytes(),“zhangsan”.getBytes());
test_cre.put(put);
}

6、putall 读取students.txt 并将数据写入Hbase

   @Test
    public void PutAll() throws IOException {
        //创建表
        Admin admin = conn.getAdmin();
        TableName studentsT = TableName.valueOf("students");
        //判断表是否存在
        if (!admin.tableExists(studentsT)){
            //创建表结构
            HTableDescriptor students = new HTableDescriptor(studentsT);
            //创建一个列簇
            HColumnDescriptor info = new HColumnDescriptor("info");
            //添加列簇
            students.addFamily(info);
            admin.createTable(students);
        }
        //向表中添加数据
        Table stu = conn.getTable(studentsT);
        //读取一个文本文件
        BufferedReader br = new BufferedReader(new FileReader("D:\\bigdata\\ownbigdata\\data\\students.txt"));
        String line = null;
        while((line=br.readLine())!=null){
            //读取每一行数据以 ,切分
            String[] split = line.split(",");
            String id = split[0];
            String name = split[1];
            String age = split[2];
            String sex = split[3];
            String clazz = split[4];
            Put put = new Put(id.getBytes());
            put.addColumn("info".getBytes(),"name".getBytes(),name.getBytes());
            put.addColumn("info".getBytes(),"age".getBytes(),age.getBytes());
            put.addColumn("info".getBytes(),"sex".getBytes(),sex.getBytes());
            put.addColumn("info".getBytes(),"clazz".getBytes(),clazz.getBytes());
            stu.put(put);
        }

    }

6、获取数据

@Test
public void Get() throws IOException {
    Table test_cre = conn.getTable(this.table);
    Get get = new Get("001".getBytes());
    Result result = test_cre.get(get);
    byte[] value = result.getValue("cf1".getBytes(), "name".getBytes());
    System.out.println(Bytes.toString(value));
}

7、查询数据

    @Test
    public void Scan() throws IOException {
        Table stu = conn.getTable(TableName.valueOf("students"));
        Scan scan = new Scan();
//        scan.setLimit(5);
        scan.withStartRow("1500100010".getBytes());
        scan.withStopRow("1500100020".getBytes());
        ResultScanner sc = stu.getScanner(scan);
        for (Result result : sc) {
            String id = Bytes.toString(result.getRow());
            String name = Bytes.toString(result.getValue("info".getBytes(), "name".getBytes()));
            String age = Bytes.toString(result.getValue("info".getBytes(), "age".getBytes()));
            String sex = Bytes.toString(result.getValue("info".getBytes(), "sex".getBytes()));
            String clazz = Bytes.toString(result.getValue("info".getBytes(), "clazz".getBytes()));
            System.out.println(id + "," + name + "," + age + "," + sex + "," + clazz);
        }
    }

8、获取数据第二种方式

//适用于每条数据结构不唯一的情况下,直接遍历每条数据包含的所有的cell
    @Test
    public void scanWithUtil() throws IOException {
        Table students = conn.getTable(TableName.valueOf("students"));
        Scan scan = new Scan();
//        scan.setLimit(5);
        scan.withStartRow("1500100010".getBytes());
        scan.withStopRow("1500100020".getBytes());
        ResultScanner sc = students.getScanner(scan);
        for (Result result : sc) {
            //通过获取的一条数据去获取每一列的单元格
            for (Cell cell : result.listCells()) {
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                //列名
                String qua = Bytes.toString(CellUtil.cloneQualifier(cell));
                //列簇名
                String cf = Bytes.toString(CellUtil.cloneFamily(cell));
                if("age".equals(qua)){
                    if (Integer.parseInt(value)>=18){
                        value ="成年";
                    }else{
                        value ="未成年";
                    }
                }
                System.out.print(value+" ");
            }
            System.out.println();
        }

    }

9、关闭连接

 @After
    public void close() throws IOException {
        conn.close();
    }
}

标签:JAVA,String,admin,put,API,cre,test,HBase,getBytes
来源: https://blog.csdn.net/weixin_58078092/article/details/120811073