Hbase API 操作

24 篇文章 0 订阅
6 篇文章 0 订阅
package com.modesty;

import javax.xml.bind.PrintConversionEvent;

/**
 * @author Modesty.P.Gao
 * @version 1.0
 * @description: TODO
 * @date 2021/12/9 13:11
 */
public class Student {
    private  String  id;
    private  String  name;
    private  String  age;
    private  String  gender;
    private  String  phone;
    private  String email;

    public Student() {
    }

    public Student(String id, String name, String age, String gender, String phone, String email) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.phone = phone;
        this.email = email;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", gender='" + gender + '\'' +
                ", phone='" + phone + '\'' +
                ", email='" + email + '\'' +
                '}';
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

package com.modesty;

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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Modesty.P.Gao
 * @version 1.0
 * @description: TODO
 * @date 2021/12/9 13:13
 */
public class HbaeAPI {
    private static Admin admin;


    public static void main(String[] args) throws IOException {
        System.out.println("创建表-----");
        createTable("tableGPS", new String[]{"information", "contact"});
        System.out.println("插入数据!---------------");
        Student student1 = new Student("001", "gps1", "221", "M", "110", "1231@.com");
        insertData("tableGPS", student1);
        Student student2 = new Student("002", "gps2", "222", "M", "114", "1232@.com");
        insertData("tableGPS", student2);
        Student student3 = new Student("003", "gps3", "223", "M", "120", "1233@.com");
        insertData("tableGPS", student3);
        System.out.println("获取表中的所有数据-------------------");
        List<Student> list = getAllData("tableGPS");
        for (Student student : list) {
            System.out.println(student.toString());
        }
        System.out.println("获取表中的原始数据--------------------");
        getNoDealData("tableGPS");
        System.out.println("根据rowkey查询某一条数据");
        Student student = getDataByRowKey("tableGPS", "stu-003");
        System.out.println(student.toString());
        System.out.println("获取指定单个数据的某个字段-----------------");
        String stuPhone = getCellData("tableGPS", "stu-001", "contact", "phone");
        System.out.println(stuPhone);
        System.out.println("插入一条测试数据----------------");
        Student student5 = new Student("test010", "gps10", "223", "M", "112320", "121231233@.com");
        insertData("tableGPS", student5);
        System.out.println("获取插入test010后的所有数据");
        List<Student> studentList = getAllData("tableGPS");
        for (Student student4 : studentList) {
            System.out.println(student4.toString());
        }
        System.out.println("删除测试数据-----------------");
        deleteByRowKey("tableGPS","stu-test010");
        System.out.println("获取删除est010后的所有数据");
        List<Student> studentList1 = getAllData("tableGPS");
        for (Student student4 : studentList1) {
            System.out.println(student4.toString());
        }

    }

    /**
     * 连接集群
     *
     * @param
     */
    private static Connection initHabase() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "10.16.78.133");
        //集群配置
        Connection connection = ConnectionFactory.createConnection(configuration);
        return connection;
    }

    //创建表
    public static void createTable(String tName, String[] columnFamily) throws IOException {
        TableName tableName = TableName.valueOf(tName);
        admin = initHabase().getAdmin();
        if (admin.tableExists(tableName)) {
            System.out.println("表已存在!");
            return;
        }
        //创建表属性对象,表名需要转字节
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
        //创建多个列族
        for (String cf : columnFamily) {
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        //根据对表的配置,创建表
        admin.createTable(hTableDescriptor);
        System.out.println("表" + tableName + "创建成功!");

    }

    /**
     * 插入数据
     *
     * @param tName
     * @param student
     * @throws IOException
     */
    public static void insertData(String tName, Student student) throws IOException {
        TableName tableName = TableName.valueOf(tName);
        //放数据
        Put put = new Put(("stu-" + student.getId()).getBytes());
        //参数:1.列族名  2.列名 3.值
        put.addColumn("information".getBytes(), "name".getBytes(), student.getName().getBytes());
        put.addColumn("information".getBytes(), "age".getBytes(), student.getAge().getBytes());
        put.addColumn("information".getBytes(), "gender".getBytes(), student.getGender().getBytes());
        put.addColumn("contact".getBytes(), "phone".getBytes(), student.getPhone().getBytes());
        put.addColumn("contact".getBytes(), "email".getBytes(), student.getEmail().getBytes());
        Table table = initHabase().getTable(tableName);
        table.put(put);
        System.out.println(student.getId() + "数据插入成功!");
        table.close();
    }

    /**
     * 获取原始的数据
     *
     * @param tName
     * @throws IOException
     */
    public static void getNoDealData(String tName) throws IOException {
        Table table = initHabase().getTable(TableName.valueOf(tName));
        //得到用于扫描region对象
        Scan scan = new Scan();
        //得到ResultScanner实现类对象
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner) {
            System.out.println("scan: " + result);
        }
    }

    /**
     * 删除表
     *
     * @param tName
     */
    public static void deleteTable(String tName) throws IOException {
        TableName tableName = TableName.valueOf(tName);
        admin = initHabase().getAdmin();
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
        System.out.println(tableName + "表已删除");
    }

    /**
     * 删除指定cell数据
     *
     * @param tName
     * @param rowKey
     * @throws IOException
     */
    public static void deleteByRowKey(String tName, String rowKey) throws IOException {
        Table table = initHabase().getTable(TableName.valueOf(tName));
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        //删除指定列
        table.delete(delete);
        System.out.println(rowKey + "数据已删除!");
    }

    /**
     * 查询指定表中的所有数据
     *
     * @param tName
     * @return
     */
    public static List<Student> getAllData(String tName) throws IOException {
        Table table = null;
        List<Student> list = new ArrayList<Student>();
        table = initHabase().getTable(TableName.valueOf(tName));
        ResultScanner results = table.getScanner(new Scan());
        for (Result result : results) {
            String id = new String(result.getRow());
            System.out.println("用户名:" + new String(result.getRow()));
            Student stu = new Student();
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                String row = Bytes.toString(CellUtil.cloneRow(cell));
                String Family = Bytes.toString(CellUtil.cloneFamily(cell));
                String colName = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                stu.setId(row);
                if (colName.equals("name")) {
                    stu.setName(value);
                }
                if (colName.equals("age")) {
                    stu.setAge(value);
                }
                if (colName.equals("gender")) {
                    stu.setGender(value);
                }
                if (colName.equals("phone")) {
                    stu.setPhone(value);
                }
                if (colName.equals("email")) {
                    stu.setEmail(value);
                }
            }
            list.add(stu);
        }

        return list;
    }

    /**
     * 查询指定单个cell内容
     *
     * @param tName
     * @param rowKey
     * @param family
     * @param col
     * @return
     */
    public static String getCellData(String tName, String rowKey, String family, String col) throws IOException {
        Table table = initHabase().getTable(TableName.valueOf(tName));
        String result = null;
        Get get = new Get(rowKey.getBytes());
        if (!get.isCheckExistenceOnly()) {
            get.addColumn(Bytes.toBytes(family), Bytes.toBytes(col));
            Result res = table.get(get);
            byte[] resByte = res.getValue(Bytes.toBytes(family), Bytes.toBytes(col));
            return result = Bytes.toString(resByte);
        } else {
            return result = "查询的结果不存在!";

        }

    }

    /**
     * 根据rowkey进行查询
     *
     * @param tName
     * @param rowKey
     * @return
     */
    public static Student getDataByRowKey(String tName, String rowKey) throws IOException {
        Table table = initHabase().getTable(TableName.valueOf(tName));
        Get get = new Get(rowKey.getBytes());
        Student student = new Student();
        student.setId(rowKey);
        //先判断是否有这个数据
        if (!get.isCheckExistenceOnly()) {
            Result result = table.get(get);
            for (Cell cell : result.rawCells()) {
                String colName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                if (colName.equals("name")) {
                    student.setName(value);
                }
                if (colName.equals("age")) {
                    student.setAge(value);
                }
                if (colName.equals("gender")) {
                    student.setGender(value);
                }
                if (colName.equals("phone")) {
                    student.setPhone(value);
                }
                if (colName.equals("email")) {
                    student.setEmail(value);
                }
            }
        }
        return student;
    }
}

运行结果:

创建表-----
log4j:WARN No appenders could be found for logger (org.apache.hadoop.security.Groups).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
表tableGPS创建成功!
插入数据!---------------
001数据插入成功!
002数据插入成功!
003数据插入成功!
获取表中的所有数据-------------------
用户名:stu-001
用户名:stu-002
用户名:stu-003
Student{id=‘stu-001’, name=‘gps1’, age=‘221’, gender=‘M’, phone=‘110’, email=‘1231@.com’}
Student{id=‘stu-002’, name=‘gps2’, age=‘222’, gender=‘M’, phone=‘114’, email=‘1232@.com’}
Student{id=‘stu-003’, name=‘gps3’, age=‘223’, gender=‘M’, phone=‘120’, email=‘1233@.com’}
获取表中的原始数据--------------------
scan: keyvalues={stu-001/contact:email/1639031362640/Put/vlen=9/seqid=0, stu-001/contact:phone/1639031362640/Put/vlen=3/seqid=0, stu-001/information:age/1639031362640/Put/vlen=3/seqid=0, stu-001/information:gender/1639031362640/Put/vlen=1/seqid=0, stu-001/information:name/1639031362640/Put/vlen=4/seqid=0}
scan: keyvalues={stu-002/contact:email/1639031362982/Put/vlen=9/seqid=0, stu-002/contact:phone/1639031362982/Put/vlen=3/seqid=0, stu-002/information:age/1639031362982/Put/vlen=3/seqid=0, stu-002/information:gender/1639031362982/Put/vlen=1/seqid=0, stu-002/information:name/1639031362982/Put/vlen=4/seqid=0}
scan: keyvalues={stu-003/contact:email/1639031363159/Put/vlen=9/seqid=0, stu-003/contact:phone/1639031363159/Put/vlen=3/seqid=0, stu-003/information:age/1639031363159/Put/vlen=3/seqid=0, stu-003/information:gender/1639031363159/Put/vlen=1/seqid=0, stu-003/information:name/1639031363159/Put/vlen=4/seqid=0}
根据rowkey查询某一条数据
Student{id=‘stu-003’, name=‘gps3’, age=‘223’, gender=‘M’, phone=‘120’, email=‘1233@.com’}
获取指定单个数据的某个字段-----------------
110
插入一条测试数据----------------
test010数据插入成功!
获取插入test010后的所有数据
用户名:stu-001
用户名:stu-002
用户名:stu-003
用户名:stu-test010
Student{id=‘stu-001’, name=‘gps1’, age=‘221’, gender=‘M’, phone=‘110’, email=‘1231@.com’}
Student{id=‘stu-002’, name=‘gps2’, age=‘222’, gender=‘M’, phone=‘114’, email=‘1232@.com’}
Student{id=‘stu-003’, name=‘gps3’, age=‘223’, gender=‘M’, phone=‘120’, email=‘1233@.com’}
Student{id=‘stu-test010’, name=‘gps10’, age=‘223’, gender=‘M’, phone=‘112320’, email=‘121231233@.com’}
删除测试数据-----------------
stu-test010数据已删除!
获取删除est010后的所有数据
用户名:stu-001
用户名:stu-002
用户名:stu-003
Student{id=‘stu-001’, name=‘gps1’, age=‘221’, gender=‘M’, phone=‘110’, email=‘1231@.com’}
Student{id=‘stu-002’, name=‘gps2’, age=‘222’, gender=‘M’, phone=‘114’, email=‘1232@.com’}
Student{id=‘stu-003’, name=‘gps3’, age=‘223’, gender=‘M’, phone=‘120’, email=‘1233@.com’}

Process finished with exit code 0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小高求学之路

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

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

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

打赏作者

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

抵扣说明:

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

余额充值