Java客户端访问HBase集群解决方案(优化)

本文涉及的产品
注册配置 MSE Nacos/ZooKeeper,118元/月
云原生网关 MSE Higress,422元/月
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 测试环境:Idea+Windows10准备工作:   、打开本地 C:\Windows\System32\drivers\etc(系统默认)下名为hosts的系统文件,如果提示当前用户没有权限打开文件;第一种方法是...

测试环境:Idea+Windows10

准备工作:

   <1>、打开本地 C:\Windows\System32\drivers\etc(系统默认)下名为hosts的系统文件,如果提示当前用户没有权限打开文件;第一种方法是将hosts文件拖到桌面进行配置后再拖回原处;第二种一劳永逸的方法是修改当前用户对该文件的权限为完全控制;

   <2>、打开后hosts文件后,添加HBase集群服务器的用户名及IP地址如下:

hosts文件参考格式

   <3>、由于是windows系统下远程连接HBase,而HBase底层依赖Hadoop,所以需要下载hadoop二进制包存放到本地目录将来会在程序中引用该目录,否则会报错。你也可以理解为windows下需要模拟linux环境才能正常连接HBasehadoop;(注:windows下的版本需要和linux下一致,这里我仅仅提供的2.6.0hadoop版本解析包)

程序代码:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>spring_hbase</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring_hbase</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!--HBase依赖-->
		<dependency>
			<groupId>org.apache.hbase</groupId>
			<artifactId>hbase-client</artifactId>
			<version>1.2.0</version>
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-hadoop</artifactId>
			<version>2.5.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-hdfs</artifactId>
			<version>2.5.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-hadoop-core</artifactId>
			<version>2.4.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hbase</groupId>
			<artifactId>hbase</artifactId>
			<version>1.2.1</version>
			<type>pom</type>
		</dependency>
		<!--HBase依赖-->
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

HBaseUtils.class:

package com.example.spring_hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.springframework.data.hadoop.hbase.HbaseTemplate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * HBase工具类
 * Author JiaPeng_lv
 */
public class HBaseUtils {
    private static Connection connection;
    private static Configuration configuration;
    private static HBaseUtils hBaseUtils;
    private static Properties properties;

    /**
     * 创建连接池并初始化环境配置
     */
    public void init(){
        properties = System.getProperties();
        //实例化HBase配置类
        if (configuration==null){
            configuration = HBaseConfiguration.create();
        }
        try {
            //加载本地hadoop二进制包
            properties.setProperty("hadoop.home.dir", "D:\\hadoop-common-2.6.0-bin-master");
            //zookeeper集群的URL配置信息
            configuration.set("hbase.zookeeper.quorum","k1,k2,k3,k4,k5");
            //HBase的Master
            configuration.set("hbase.master","hba:60000");
            //客户端连接zookeeper端口
            configuration.set("hbase.zookeeper.property.clientPort","2181");
            //HBase RPC请求超时时间,默认60s(60000)
            configuration.setInt("hbase.rpc.timeout",20000);
            //客户端重试最大次数,默认35
            configuration.setInt("hbase.client.retries.number",10);
            //客户端发起一次操作数据请求直至得到响应之间的总超时时间,可能包含多个RPC请求,默认为2min
            configuration.setInt("hbase.client.operation.timeout",30000);
            //客户端发起一次scan操作的rpc调用至得到响应之间的总超时时间
            configuration.setInt("hbase.client.scanner.timeout.period",200000);
            //获取hbase连接对象
            if (connection==null||connection.isClosed()){
                connection = ConnectionFactory.createConnection(configuration);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接池
     */
    public static void close(){
        try {
            if (connection!=null)connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 私有无参构造方法
     */
    private HBaseUtils(){}

    /**
     * 唯一实例,线程安全,保证连接池唯一
     * @return
     */
    public static HBaseUtils getInstance(){
        if (hBaseUtils == null){
            synchronized (HBaseUtils.class){
                if (hBaseUtils == null){
                    hBaseUtils = new HBaseUtils();
                    hBaseUtils.init();
                }
            }
        }
        return hBaseUtils;
    }

    /**
     * 获取单条数据
     * @param tablename
     * @param row
     * @return
     * @throws IOException
     */
    public static Result getRow(String tablename, byte[] row) throws IOException{
        Table table = null;
        Result result = null;
        try {
            table = connection.getTable(TableName.valueOf(tablename));
            Get get = new Get(row);
            result = table.get(get);
        }finally {
            table.close();
        }
        return result;
    }

    /**
     * 查询多行信息
     * @param tablename
     * @param rows
     * @return
     * @throws IOException
     */
    public static Result[] getRows(String tablename,List<byte[]> rows) throws  IOException{
        Table table = null;
        List<Get> gets = null;
        Result[] results = null;
        try {
            table = connection.getTable(TableName.valueOf(tablename));
            gets = new ArrayList<Get>();
            for (byte[] row : rows){
                if(row!=null){
                    gets.add(new Get(row));
                }
            }
            if (gets.size() > 0) {
                results = table.get(gets);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            table.close();
        }
        return results;
    }

    /**
     * 获取整表数据
     * @param tablename
     * @return
     */
    public static ResultScanner get(String tablename) throws IOException{
        Table table = null;
        ResultScanner results = null;
        try {
            table = connection.getTable(TableName.valueOf(tablename));
            Scan scan = new Scan();
            scan.setCaching(1000);
            results = table.getScanner(scan);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            table.close();
        }
        return results;
    }

    /**
     * 单行插入数据
     * @param tablename
     * @param rowkey
     * @param family
     * @param cloumns
     * @throws IOException
     */
    public static void put(String tablename, String rowkey, String family, Map<String,String> cloumns) throws IOException{
        Table table = null;
        try {
            table = connection.getTable(TableName.valueOf(tablename));
            Put put = new Put(rowkey.getBytes());
            for (Map.Entry<String,String> entry : cloumns.entrySet()){
                put.addColumn(family.getBytes(),entry.getKey().getBytes(),entry.getValue().getBytes());
            }
            table.put(put);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            table.close();
            close();
        }
    }
}

①、保证该工具类唯一实例

②、全局共享重量级类Connection,该类为线程安全,使用完毕后关闭连接池

③、每次执行内部CRUD方法会创建唯一对象Table,该类为非线程安全,使用完毕后关闭

由于时间原因,内部功能方法及测试较少,有其他需求的可以自行百度添加更多方法,这里主要以类结构及配置为主。

Test.class:

package com.example.spring_hbase;

import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringHbaseApplicationTests {
	@Test
	public void contextLoads() {
	}

	@Test
	public void test01(){
		HBaseUtils.getInstance();
		try {
			Long time = System.currentTimeMillis();
			Result result = HBaseUtils.getRow("GPS_MAP", Bytes.toBytes(1));
			System.out.println("本次查询耗时:"+(System.currentTimeMillis()-time)*1.0/1000+"s");
			NavigableMap<byte[],NavigableMap<byte[],NavigableMap<Long,byte[]>>> navigableMap = result.getMap();
			for (byte[] family:navigableMap.keySet()){
				System.out.println("columnFamily:"+ new String(family));
				for (byte[] column : navigableMap.get(family).keySet()){
					System.out.println("column:"+new String(column));
					for (Long t : navigableMap.get(family).get(column).keySet()){
						System.out.println("value:"+new String(navigableMap.get(family).get(column).get(t)));
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			HBaseUtils.close();
		}
	}

	@Test
	public void test02(){
		HBaseUtils.getInstance();
		ResultScanner results = null;
		try {
			Long time = System.currentTimeMillis();
			results = HBaseUtils.get("GPS_MAP");
			System.out.println("本次查询耗时:"+(System.currentTimeMillis()-time)*1.0/1000+"s");
			for (Result result : results){
				NavigableMap<byte[],NavigableMap<byte[],NavigableMap<Long,byte[]>>> navigableMap = result.getMap();
				for (byte[] family:navigableMap.keySet()){
					System.out.println("columnFamily:"+ new String(family));
					for (byte[] column : navigableMap.get(family).keySet()){
						System.out.println("column:"+new String(column));
						for (Long t : navigableMap.get(family).get(column).keySet()){
							System.out.println("value:"+new String(navigableMap.get(family).get(column).get(t)));
						}
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			results.close();
			HBaseUtils.close();
		}
	}

	@Test
	public void test03(){
		HBaseUtils.getInstance();
		Result[] results = null;
		List<byte[]> list = null;
		try {
			list = new ArrayList<byte[]>();
			list.add(Bytes.toBytes(1));
			list.add(Bytes.toBytes(2));
			Long time = System.currentTimeMillis();
			results = HBaseUtils.getRows("GPS_MAP",list);
			System.out.println("本次查询耗时:"+(System.currentTimeMillis()-time)*1.0/1000+"s");
			for (Result result : results){
				NavigableMap<byte[],NavigableMap<byte[],NavigableMap<Long,byte[]>>> navigableMap = result.getMap();
				for (byte[] family:navigableMap.keySet()){
					System.out.println("columnFamily:"+ new String(family));
					for (byte[] column : navigableMap.get(family).keySet()){
						System.out.println("column:"+new String(column));
						for (Long t : navigableMap.get(family).get(column).keySet()){
							System.out.println("value:"+new String(navigableMap.get(family).get(column).get(t)));
						}
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			HBaseUtils.close();
		}
	}

	@Test
	public void test04(){
		HBaseUtils.getInstance();
		try {
			Map<String,String> cloumns = new HashMap<String, String>();
			cloumns.put("test01","test01");
			cloumns.put("test02","test02");
			Long time = System.currentTimeMillis();
			HBaseUtils.put("GPS_MAP","3","TEST",cloumns);
			System.out.println("本次插入耗时:"+(System.currentTimeMillis()-time)*1.0/1000+"s");
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			HBaseUtils.close();
		}
	}
}

测试后发现查询和插入效率相对于没有优化过的类耗时大大缩减;

 

相关实践学习
lindorm多模间数据无缝流转
展现了Lindorm多模融合能力——用kafka API写入,无缝流转在各引擎内进行数据存储和计算的实验。
云数据库HBase版使用教程
&nbsp; 相关的阿里云产品:云数据库 HBase 版 面向大数据领域的一站式NoSQL服务,100%兼容开源HBase并深度扩展,支持海量数据下的实时存储、高并发吞吐、轻SQL分析、全文检索、时序时空查询等能力,是风控、推荐、广告、物联网、车联网、Feeds流、数据大屏等场景首选数据库,是为淘宝、支付宝、菜鸟等众多阿里核心业务提供关键支撑的数据库。 了解产品详情:&nbsp;https://cn.aliyun.com/product/hbase &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
9天前
|
监控 IDE Java
【Java性能调优新工具】JDK 22性能分析器:深度剖析,优化无死角!
【9月更文挑战第9天】JDK 22中的性能分析器为Java应用的性能调优提供了强大的支持。通过深度集成、全面监控、精细化分析和灵活报告生成等核心优势,性能分析器帮助开发者实现了对应用性能的全面掌控和深度优化。在未来的Java开发过程中,我们期待性能分析器能够继续发挥重要作用,为Java应用的性能提升贡献更多力量。
|
14天前
|
存储 Java 程序员
优化Java多线程应用:是创建Thread对象直接调用start()方法?还是用个变量调用?
这篇文章探讨了Java中两种创建和启动线程的方法,并分析了它们的区别。作者建议直接调用 `Thread` 对象的 `start()` 方法,而非保持强引用,以避免内存泄漏、简化线程生命周期管理,并减少不必要的线程控制。文章详细解释了这种方法在使用 `ThreadLocal` 时的优势,并提供了代码示例。作者洛小豆,文章来源于稀土掘金。
|
19天前
|
算法 Java 数据库
Java 性能优化秘籍:在数字化浪潮中,让你的应用如火箭般飞驰!
【8月更文挑战第30天】Java 作为一种广泛使用的编程语言,其性能优化是开发者关注的重点。优化需基于对 Java 内存模型、垃圾回收及线程并发模型的理解。合理的垃圾回收算法与线程安全措施、锁机制的应用至关重要。实践中,避免不必要的对象创建可减轻内存压力;优化数据库操作,如合理使用索引和查询语句,同样重要。JVM 参数调优,如调整堆大小和垃圾回收器选择,也能显著提升性能。综合运用这些策略并通过持续测试与调整,可以使 Java 应用在高并发和大数据量场景下保持高效运行,提供流畅的用户体验。
36 3
|
17天前
|
存储 开发者 C#
WPF与邮件发送:教你如何在Windows Presentation Foundation应用中无缝集成电子邮件功能——从界面设计到代码实现,全面解析邮件发送的每一个细节密武器!
【8月更文挑战第31天】本文探讨了如何在Windows Presentation Foundation(WPF)应用中集成电子邮件发送功能,详细介绍了从创建WPF项目到设计用户界面的全过程,并通过具体示例代码展示了如何使用`System.Net.Mail`命名空间中的`SmtpClient`和`MailMessage`类来实现邮件发送逻辑。文章还强调了安全性和错误处理的重要性,提供了实用的异常捕获代码片段,旨在帮助WPF开发者更好地掌握邮件发送技术,提升应用程序的功能性与用户体验。
20 0
|
4月前
|
Java Shell 分布式数据库
【大数据技术Hadoop+Spark】HBase数据模型、Shell操作、Java API示例程序讲解(附源码 超详细)
【大数据技术Hadoop+Spark】HBase数据模型、Shell操作、Java API示例程序讲解(附源码 超详细)
134 0
|
SQL 分布式计算 Hadoop
Hadoop集群hbase的安装
Hadoop集群hbase的安装
190 0
|
3月前
|
存储 分布式计算 Hadoop
Hadoop节点文件存储HBase设计目的
【6月更文挑战第2天】
48 6
|
3月前
|
存储 分布式计算 Hadoop
Hadoop节点文件存储Hbase高可靠性
【6月更文挑战第2天】
66 2
|
3月前
|
存储 分布式计算 Hadoop
Hadoop节点文件存储Hbase面向列
【6月更文挑战第2天】
33 2
|
4月前
|
分布式计算 安全 Hadoop
HBase Shell-org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet 已解决
在HBase Shell遇到错误时,检查Hadoop非安全模式:`hdfs dfsadmin -safemode get`。问题解决在于`hbase-site.xml`中添加配置:Zookeeper客户端端口设为2181和预写日志提供者设为filesystem。