ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

HBase - JAVA -API

2021-10-17 15:30:59  阅读:191  来源: 互联网

标签:JAVA String admin put API cre test HBase getBytes


文章目录

一、几个主要的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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有