【hadoop】Hbase java api 案例

代码实现:

HBaseConnection.java

复制代码
package com.peizheng.bigdata;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

public class HBaseConnection {
    public static Connection connection = null;
    static {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "master,slave1,slave2");
        conf.set("hbase.zookeeper.property.clientPort","2181");

        try {
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void closeConnection(){
        try {
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

HBaseOperation.java

复制代码
package com.peizheng.bigdata;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

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

public class HBaseOperation {

    public static void createNameSpace(String namespace) throws IOException {

        // 1 获取admin对象   另有Table对象
        Admin admin = HBaseConnection.connection.getAdmin();

        // 1.1 Builder类
        NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(namespace);

        // 1.2 添加需求,这里是添加了自定义的描述信息
        //builder.addConfiguration("user","peizheng");

        // 2 调用方法,创建命名空间
        admin.createNamespace(builder.build());

        // 3 关闭admin
        admin.close();
    }

    public static void createTable(String name, String[] cols) throws IOException {
        Admin admin = HBaseConnection.connection.getAdmin();

        HTableDescriptor hTableDescriptor = new HTableDescriptor(name);

        for (String col : cols) {
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);
            hColumnDescriptor.setMaxVersions(5);

            hTableDescriptor.addFamily(hColumnDescriptor);
        }


        admin.createTable(hTableDescriptor);

        admin.close();
    }

    public static void putCell(String tableName, String rowKey, String columnFamily, String columnName, String value) throws IOException {
        Table table = HBaseConnection.connection.getTable(TableName.valueOf(tableName));

        Put put = new Put(Bytes.toBytes(rowKey));

        put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName),Bytes.toBytes(value));

        table.put(put);

        table.close();
    }

    // 查询
    // 单行读取
    public static void getRow(String tableName, String rowKey) throws IOException {
        Table table = HBaseConnection.connection.getTable(TableName.valueOf(tableName));

        Get get = new Get(Bytes.toBytes(rowKey));



        // Result -> Cell[]
        Result result = table.get(get);

        // cell存储非常底层
        Cell[] cells = result.rawCells();

        for (Cell cell : cells) {
            String value = new String(CellUtil.cloneValue(cell));
            String family = new String(CellUtil.cloneFamily(cell));
            String colunm = new String(CellUtil.cloneQualifier(cell));


            System.out.println(family + ":" + colunm + "," + value);
        }

        table.close();
    }

    public static void getCell(String tableName, String rowKey, String familyName, String columnName) throws IOException {
        Table table = HBaseConnection.connection.getTable(TableName.valueOf(tableName));

        Get get = new Get(Bytes.toBytes(rowKey));

        get.addColumn(Bytes.toBytes(familyName),Bytes.toBytes(columnName));

        // Result -> Cell[]
        Result result = table.get(get);

        // cell存储非常底层
        Cell[] cells = result.rawCells();

        for (Cell cell : cells) {
            String value = new String(CellUtil.cloneValue(cell));
            String family = new String(CellUtil.cloneFamily(cell));
            String colunm = new String(CellUtil.cloneQualifier(cell));


            System.out.println(family + ":" + colunm + "," + value);
        }

        table.close();
    }

    public static void scanRows(String tableName, String startRowKey, String endRowKey) throws IOException {
        Table table = HBaseConnection.connection.getTable(TableName.valueOf(tableName));

        Scan scan = new Scan();

        // 指定起始的行 (包含)
        scan.setStartRow(Bytes.toBytes(startRowKey));

        // 指定结束的行 (默认不包含)
        scan.setStopRow(Bytes.toBytes(endRowKey));

        ResultScanner scanner = table.getScanner(scan);

        // Result记录一行数据,Cell数组
        // ResultScanner记录多行数据,Result数组
        for (Result result : scanner) {
            Cell[] cells = result.rawCells();

            for (Cell cell : cells) {
                String value = new String(CellUtil.cloneValue(cell));
                String family = new String(CellUtil.cloneFamily(cell));
                String colunm = new String(CellUtil.cloneQualifier(cell));


                System.out.print(family + ":" + colunm + "," + value + "\t");
            }
            System.out.println();
        }

        table.close();
    }

    public static void filterScan(String tableName, String startRowKey, String endRowKey, String familyName, String columnName, String val) throws IOException {
        Table table = HBaseConnection.connection.getTable(TableName.valueOf(tableName));

        Scan scan = new Scan();

        // 指定起始的行 (包含)
        scan.setStartRow(Bytes.toBytes(startRowKey));

        // 指定结束的行 (默认不包含)
        scan.setStopRow(Bytes.toBytes(endRowKey));

        FilterList filterList = new FilterList();

        //设置过滤器
        //
        SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
            Bytes.toBytes(familyName),
            Bytes.toBytes(columnName),
                CompareFilter.CompareOp.LESS_OR_EQUAL,
                Bytes.toBytes(val)
        );

        //添加过滤器
        filterList.addFilter(singleColumnValueFilter);
        scan.setFilter(filterList);

        ResultScanner scanner = table.getScanner(scan);

        // Result记录一行数据,Cell数组
        // ResultScanner记录多行数据,Result数组
        for (Result result : scanner) {
            Cell[] cells = result.rawCells();

            for (Cell cell : cells) {
                String value = new String(CellUtil.cloneValue(cell));
                String family = new String(CellUtil.cloneFamily(cell));
                String colunm = new String(CellUtil.cloneQualifier(cell));


                System.out.print(family + ":" + colunm + "," + value + "\t");
            }
            System.out.println();

            table.close();
        }
    }



    public static void main(String[] args) throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "master,slave1,slave2");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        HBaseConnection.connection = ConnectionFactory.createConnection(conf);

        String tableName = "temperature";
        String[] cols = {"cf"};
        if (!HBaseConnection.connection.getAdmin().tableExists(TableName.valueOf(tableName))) {
            createTable(tableName, cols);
        }

        BufferedReader bufferedReader = new BufferedReader(new FileReader("F:/temperature.log"));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            String[] splits = line.split(",");
            String id = splits[0].trim();
            String year = splits[1].trim();
            String temperature = splits[2].trim();
            String rowKey = id + ":" + year;
            putCell(tableName, rowKey, "cf", "id", id);
            putCell(tableName, rowKey, "cf", "year", year);
            putCell(tableName, rowKey, "cf", "temperature", temperature);
        }
        bufferedReader.close();
        HBaseConnection.closeConnection();
    }
}

相关运行结果:

java程序运行结果:

hbase客户端运行结果:

scan 'temperature'

报错解决

一直运行中可能是设置连接的是ip,不是master,slave1,slave2,这种,可能报错Caused by: org.apache.hadoop.hbase.MasterNotRunningException: java.net.UnknownHostExce。在网上找了半天的原因也没有找到的话参考下面文章修改 windows的ssh配置文件:

ip,主机名供参考:

【hadoop】创建 SSH 别名来连接远程 linux-CSDN博客

相关推荐
后台模板学习42 分钟前
大模型 Prompt 工程在电商订单系统中的踩坑记录
java·python·prompt
w_zero_one42 分钟前
算法--构造
java·数据结构·算法
SL_staff1 小时前
制造业订单交付崩盘?用四层协同框架重建交付可信度(技术实践)
java·产品·全栈
SL_staff2 小时前
打破风控迭代死循环:用决策编排重构业务与技术协作链路
java·产品·全栈
Mikko72 小时前
Redis 连接数满了服务端有错误信息吗?连接数不增加却命令超时?6.2 实测连接池、超时与大 key 怎么判定
java·spring boot·redis·后端
.Bryce.2 小时前
从零到一开发技术名词大全:从需求分析到 AI Agent 工程
java·网络·人工智能·agent·需求分析·agent评测
SL_staff3 小时前
JVS-APS的7个刚性约束设计:从无限产能幻觉到可执行排产的工程实践
java·spring cloud·全栈
代码山河4 小时前
基本类型转换:自动类型转换与强制类型转换
java·学习·架构·教程·面向对象·项目
Escalating_xu5 小时前
【Linux 网络编程】五种 I/O 模型与非阻塞 I/O:从“等数据”到正确处理 EAGAIN
java·linux·网络
程序猿乐锅6 小时前
万字详解Single-flight(单飞模式)
java·开发语言·分布式·后端·学习·single-flight