HBase(扫描HBase表的数据转化为对应的类Java代码)

Msg实体类

package com.xxxx.test01;

public class Msg {
    private String name;
    private String bumen;
    private String address;
    private String diyu;
    private String wangzhi;
    private String email;
    private String daibiao;
    private String bianma;
    private String lianxi;
    private String phone;
    private String shuxing;

    public String getName() {
        return name;
    }

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

    public String getBumen() {
        return bumen;
    }

    public void setBumen(String bumen) {
        this.bumen = bumen;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getDiyu() {
        return diyu;
    }

    public void setDiyu(String diyu) {
        this.diyu = diyu;
    }

    public String getWangzhi() {
        return wangzhi;
    }

    public void setWangzhi(String wangzhi) {
        this.wangzhi = wangzhi;
    }

    public String getEmail() {
        return email;
    }

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

    public String getDaibiao() {
        return daibiao;
    }

    public void setDaibiao(String daibiao) {
        this.daibiao = daibiao;
    }

    public String getBianma() {
        return bianma;
    }

    public void setBianma(String bianma) {
        this.bianma = bianma;
    }

    public String getLianxi() {
        return lianxi;
    }

    public void setLianxi(String lianxi) {
        this.lianxi = lianxi;
    }

    public String getPhone() {
        return phone;
    }

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

    public String getShuxing() {
        return shuxing;
    }

    public void setShuxing(String shuxing) {
        this.shuxing = shuxing;
    }

    public Msg(String name, String bumen, String address, String diyu, String wangzhi, String email, String daibiao, String bianma, String lianxi, String phone, String shuxing) {
        this.name = name;
        this.bumen = bumen;
        this.address = address;
        this.diyu = diyu;
        this.wangzhi = wangzhi;
        this.email = email;
        this.daibiao = daibiao;
        this.bianma = bianma;
        this.lianxi = lianxi;
        this.phone = phone;
        this.shuxing = shuxing;
    }

    public Msg() {
    }
}
View Code

HBaseConnection连接类

根据自己的信息进行连接,我是加载了自己虚拟机的hbase-site.xml作为配置文件的

package com.xxxx.test01;

import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

/**
 * HBase多线程连接
 */
public class HBaseConnection {

    //声明静态属性
    public static Connection connection=null;
    static {

//        //1.创建配置对象
//        Configuration configuration = new Configuration();
//
//        //2.设置配置参数
//        configuration.set("hbase.zookeeper.quorum","node1,node2,node3");

        //直接使用读取本地文件的形式添加参数
        //3.建立hbase连接
        try {
            connection = ConnectionFactory.createConnection();
        } catch (IOException e) {
//            throw new RuntimeException(e);
            System.out.println("连接失败");
            e.printStackTrace();
        }
    }


    public static void closeConnection() throws IOException {
        //判断连接是否为空
        if(connection!=null){
            connection.close();
        }
    }


    public static void main(String[] args) throws IOException {

        //不再main线程中单独创建连接,而是直接使用
        System.out.println(HBaseConnection.connection);

        //用完记得关闭连接
        HBaseConnection.closeConnection();;

    }

}
View Code

我的实现代码:

package com.xxxx.test01;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

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

public class HBaseDML {

    //静态对象
    public static Connection connection = HBaseConnection.connection;

    /**
     * 获取表中数据
     * @return 返回Msg的list对象
     */
    public static List<Msg> scanAll() throws IOException {

        //list
        List<Msg> list = new ArrayList<>();


        //1.Table连接
        Table table = connection.getTable(TableName.valueOf("STUDENT"));

        //scan方法
        Scan scan = new Scan();

        //添加限制:无

        try {

            //获取数据
            ResultScanner scanner = null;

            scanner = table.getScanner(scan);

            //转成Msg类对象,放入list集合中
            for (Result result : scanner) {

                Cell[] cells = result.rawCells();

                Msg msg = new Msg();
                for (Cell cell : cells) {

                    //获取列名
                    String columnname = new String(CellUtil.cloneQualifier(cell));
                    //获取对应的值
                    String value = new String(CellUtil.cloneValue(cell));
                    //看columnname对应的哪一个数据
                    switch (columnname) {
                        case "bumen":
                            msg.setBumen(value);
                            break;
                        case "address":
                            msg.setAddress(value);
                            break;
                        case "diyu":
                            msg.setDiyu(value);
                            break;
                        case "wangzhi":
                            msg.setWangzhi(value);
                            break;
                        case "email":
                            msg.setEmail(value);
                            break;
                        case "daibiao":
                            msg.setDaibiao(value);
                            break;
                        case "bianma":
                            msg.setBianma(value);
                            break;
                        case "lianxi":
                            msg.setLianxi(value);
                            break;
                        case "phone":
                            msg.setPhone(value);
                            break;
                        default:
                            msg.setShuxing(value);
                            break;
                    }
                }
                //表名设置为了rowKey,所以一个cell数组中,表名(RowKey)是相同的
                msg.setName(new String(CellUtil.cloneRow(cells[0])));

                //把一个msg对象放入list集合
                list.add(msg);

            }


        } catch (IOException e) {
//            throw new RuntimeException(e);
            e.printStackTrace();
        }


        //关闭table
        table.close();

        return list;

    }



    //测试代码
    public static void main(String[] args) throws IOException {
        //测试
        List<Msg> lists =  scanAll();

        for (Msg list : lists) {


            System.out.println(list.getName()+"--"+list.getBumen()+"--"+list.getAddress()+"--"
                    +list.getDiyu()+"--"+list.getWangzhi()+"--"+ list.getEmail()+"--" +list.getDaibiao()
                    +"--"+list.getBianma()+"--"+list.getLianxi()+"--"+list.getPhone()+"--"+list.getShuxing());

        }

        HBaseConnection.closeConnection();


    }
}

 

成功打印出想要的结果:

 

posted on 2023-09-21 16:19  201812  阅读(71)  评论(0编辑  收藏  举报