性能测试工具操作数据库(十二)-Loadrunner与Hbase

本文涉及的产品
任务调度 XXL-JOB 版免费试用,400 元额度,开发版规格
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
MSE Nacos/ZooKeeper 企业版试用,1600元额度,限量50份
简介: Hbase的测试关键是要引用正确的Hbase jar包(还要保证版本的兼容,Hbase1.0开始就要求JDK1.7及以上,而Loadrunner11不支持JDK1.7,所以本文举例用的是Loadrunner12,另外要保证引用的Hbase Jar包也是与服务端的Hbase版本一致,否则也会出现兼容性...
版权声明:本文为博主原创文章,未经博主允许不得转载。欢迎访问我的博客 https://blog.csdn.net/smooth00/article/details/74295829

Hbase的测试关键是要引用正确的Hbase jar包(还要保证版本的兼容,Hbase1.0开始就要求JDK1.7及以上,而Loadrunner11不支持JDK1.7,所以本文举例用的是Loadrunner12,另外要保证引用的Hbase Jar包也是与服务端的Hbase版本一致,否则也会出现兼容性问题)。

1、在loadrunner中新建脚本(本文以LoadRunner12.02为例),要求选择协议类型为Java Vuser
2、在Replay-->Runtime Settings设置Java VM路径,由于LoadRunner12对jdk1.8的支持不好,本次测试是拷贝了一份绿色免安装版(32位)的jdk1.7.0_67,所以路径选择固定路径模式(Use the Specified JDK),并设置好JDK1.7的路径。
3、使用Java操作Hbase需要相关的Jar包(可以去官网下载),也可以下载我上传的包(性能测试工具所引用的hbase依赖包),放到include目录或其他目录下,并在Run-time Settings中配置Classpath


4、在Java Vuser脚本中编写Hbase的测试代码(举例如下,具体需要配置好):

/*
 * LoadRunner Java script. (Build: _build_number_)
 * 
 * Script Description: 
 *                     
 */
import java.io.IOException;               
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;
import java.io.File;

import lrapi.lr;

public class Actions
{ 
	String tableName="student";
	Configuration configuration=null;	
	public int init() throws Throwable {
		//加载日志输出方式
	    File directory = new File(".");
	    DOMConfigurator.configure(directory.getCanonicalPath()+"\\log4j.xml");//加载log4j.xml文件
	    //PropertyConfigurator.configure("E:/study/log4j/log4j.properties");//加载.properties文件
	    Logger log=Logger.getLogger("org.zblog.test");
	    System.setProperty("hadoop.home.dir", directory.getCanonicalPath()+"\\hadoop-common-2.2.0-bin-master");
	    
	    configuration = HBaseConfiguration.create();       
	    //config.set("hbase.zookeeper.quorum", "hellotest");//单机            
	    configuration.set("hbase.zookeeper.quorum", "agent01.org.cn,agent02.org.cn,master.org.cn");
	    // zookeeper地址(包括一个主节点,两个子节点)  
	    configuration.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口  
	    configuration.set("hbase.regionserver.port", "16020");
        configuration.set("zookeeper.znode.parent", "/hbase-unsecure");
	    configuration.set("hbase.rootdir", "hdfs://master.org.cn:8020/apps/hbase/data");
	    //connection = ConnectionFactory.createConnection(config); 
		return 0;
	}//end of init


	public int action() throws Throwable {    
		createTable(configuration, tableName);
		addData(configuration, tableName);
		getData(configuration, tableName);
		getAllData(configuration, tableName);
		deleteDate(configuration, tableName);
		dropTable(configuration, tableName);  
		return 0;
	}//end of action


	public int end() throws Throwable {
		return 0;
	}//end of end

     /**
  * create a new Table
  * @param configuration Configuration
  * @param tableName String,the new Table's name
  * */
 public static void createTable(Configuration configuration,String tableName){
  HBaseAdmin admin;
  try {
		admin = new HBaseAdmin(configuration);
		if(admin.tableExists(tableName)){
    	admin.disableTable(tableName);
    	admin.deleteTable(tableName);
    	System.out.println(tableName+"is exist ,delete ......");
		}     
		HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf(tableName));
		tableDescriptor.addFamily(new HColumnDescriptor("info"));
		tableDescriptor.addFamily(new HColumnDescriptor("address"));
		admin.createTable(tableDescriptor);
		System.out.println("end create table");
  } catch (MasterNotRunningException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
  } catch (ZooKeeperConnectionException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
  } catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
  }
  
 }
 
 /**
  * Delete the existing table
  * @param configuration Configuration
  * @param tableName String,Table's name
  * */
 public static void dropTable(Configuration configuration,String tableName){
  HBaseAdmin admin;
  try {
		admin = new HBaseAdmin(configuration);
		if(admin.tableExists(tableName)){
    	admin.disableTable(tableName);
    	admin.deleteTable(tableName);
    	System.out.println(tableName+"delete success!");
		}else{
    	System.out.println(tableName+"Table does not exist!");
		}
  } catch (MasterNotRunningException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
  } catch (ZooKeeperConnectionException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
  } catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
  }
 }
 
 /**
  * insert a data
  * @param configuration Configuration
  * @param tableName String,Table's name
  * */
 public static void addData(Configuration configuration,String tableName){
  HBaseAdmin admin;
  try {
		admin = new HBaseAdmin(configuration);
		if(admin.tableExists(tableName)){
    	HTable table=new HTable(configuration, tableName);
    	Put put=new Put(Bytes.toBytes("zhangsan"));
    	put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("28"));
    	table.put(put);
    	System.out.println("add success!");
		}else{
    	System.out.println(tableName+"Table does not exist!");
		}
  } catch (MasterNotRunningException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
  } catch (ZooKeeperConnectionException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
  } catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
  }
 }
 
 /**
  * Delete a data
  * @param configuration Configuration
  * @param tableName String,Table's name
  * */
 public static void deleteDate(Configuration configuration,String tableName){
  HBaseAdmin admin;
  try {
		admin=new HBaseAdmin(configuration);
		if(admin.tableExists(tableName)){
    	HTable table=new HTable(configuration, tableName);
    	Delete delete=new Delete(Bytes.toBytes("zhangsan"));
    	table.delete(delete);
    	System.out.println("delete success!");
		}else{
    	System.out.println("Table does not exist!");
		}
  } catch (MasterNotRunningException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
  } catch (ZooKeeperConnectionException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
  } catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
  }
 }
 

 /**
  * get a data
  * @param configuration Configuration
  * @param tableName String,Table's name
  * */
 public static void getData(Configuration configuration,String tableName){
  HTable table;
  try {
		table = new HTable(configuration, tableName);
		Get get=new Get(Bytes.toBytes("zhangsan"));
		Result result=table.get(get);
 
		for(Cell cell:result.rawCells()){   
    	System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
    	System.out.println("Timetamp:"+cell.getTimestamp()+" ");
    	System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
    	System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
    	System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
		}
  } catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
  }
 }
 
 /**
  * insert all data
  * @param configuration Configuration
  * @param tableName String,Table's name
  * */
 public static void getAllData(Configuration configuration,String tableName){
  HTable table;
  try {
		table=new HTable(configuration, tableName);
		Scan scan=new Scan();
		ResultScanner results=table.getScanner(scan);
		for(Result result:results){
    	for(Cell cell:result.rawCells()){   
     		System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
     		System.out.println("Timetamp:"+cell.getTimestamp()+" ");
     		System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
     		System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
     		System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
			}
		}
  } catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
  }
    
 }
}

5、保存脚本,同时将Hadoop-common-2.2.0-bin-master文件目录放到脚本的根目录下(因为以上代码写的取文件路径就是脚本根目录),这么做的目的主要是Windows下调用hadoop组件,如果没有winutils.exe就会报错(可以到网上下载这个Hadoop-common-2.2.0-bin-master包,上面提供下载的性能测试工具所引用的hbase依赖包里也有)。

6、运行脚本,通过LoadRunner的Replay Log可以看到数据操作测试执行成功:


7、通过性能测试工具调用Hbase的最大挑战是某些Jar包的缺失和版本的不兼容,所以本文也只能是提供个参考,具体应用的时候需要根据服务端部署的Hadoop版本和开发应用的需要,进行配置和引用相关的Jar包,并通过Loadrunner的Replay调试功能,不断的解决报错问题(是个考验耐心和细心的过程,中间遇到的问题有些可能是需要不断搜索外文网站和一些社区论坛才能找到正确的解释,没办法,开源技术没有现成的资料和使用说明,就得靠自己摸索和不断网上搜索)。

目录
相关文章
|
5月前
|
数据可视化 BI API
无缝对接云数据库:自定义报表生成工具在混合云环境下的部署指南
自定义报表生成工具通过拖拽设计、多数据源整合及自动化输出,帮助业务人员零代码创建个性化报表,解决传统工具灵活性不足、技术门槛高的问题。文章对比其与传统报表差异,列举行业应用场景(如财务、零售),并给出选型建议与主流工具(如FineReport、Power BI、板栗看板)的优劣势分析。
223 0
|
4月前
|
前端开发 Java jenkins
Jmeter压力测试工具全面教程和使用技巧。
JMeter是一个能够模拟高并发请求以检查应用程序各方面性能的工具,包括但不限于前端页面、后端服务及数据库系统。熟练使用JMeter不仅能够帮助发现性能瓶颈,还能在软件开发早期就预测系统在面对真实用户压力时的表现,确保软件质量和用户体验。在上述介绍的基础上,建议读者结合官方文档和社区最佳实践,持续深入学习和应用。
903 10
|
3月前
|
人工智能 数据库 iOS开发
DBeaver Ultimate Edtion 25.2 发布 - 通用数据库工具
DBeaver Ultimate Edtion 25.2 Multilingual (macOS, Linux, Windows) - 通用数据库工具
437 0
|
6月前
|
Java 测试技术 容器
Jmeter工具使用:HTTP接口性能测试实战
希望这篇文章能够帮助你初步理解如何使用JMeter进行HTTP接口性能测试,有兴趣的话,你可以研究更多关于JMeter的内容。记住,只有理解并掌握了这些工具,你才能充分利用它们发挥其应有的价值。+
995 23
|
5月前
|
SQL 存储 数据库
SQL Server Management Studio (SSMS) 21 - 微软数据库管理工具
SQL Server Management Studio (SSMS) 21 - 微软数据库管理工具
954 0
|
9月前
|
自然语言处理 数据库 iOS开发
DBeaver Ultimate Edtion 25.0 Multilingual (macOS, Linux, Windows) - 通用数据库工具
DBeaver Ultimate Edtion 25.0 Multilingual (macOS, Linux, Windows) - 通用数据库工具
625 12
DBeaver Ultimate Edtion 25.0 Multilingual (macOS, Linux, Windows) - 通用数据库工具
|
8月前
|
SQL 存储 分布式数据库
分布式存储数据恢复—hbase和hive数据库数据恢复案例
分布式存储数据恢复环境: 16台某品牌R730xd服务器节点,每台服务器节点上有数台虚拟机。 虚拟机上部署Hbase和Hive数据库。 分布式存储故障: 数据库底层文件被误删除,数据库不能使用。要求恢复hbase和hive数据库。
281 12
|
9月前
|
SQL 分布式计算 数据库
【YashanDB 知识库】Hive 命令工具 insert 崖山数据库报错
【YashanDB 知识库】Hive 命令工具 insert 崖山数据库报错
|
9月前
|
SQL 关系型数据库 网络安全
Navicat Premium 17 最新版下载与配置:5分钟完成企业级数据库工具部署
Navicat Premium 17 是一款支持多种主流数据库(如 MySQL、Oracle、PostgreSQL 等)的多数据库管理工具,提供可视化数据建模、SQL 编辑和数据同步等功能。试用版提供 14 天全功能体验,商业版支持跨平台使用。安装环境要求 Windows 10/11 或 macOS 12.0+,最低配置为 4GB 内存。下载并解压安装包后,按步骤启动安装程序、接受许可协议、自定义安装路径并完成安装。首次运行时需激活许可证并配置数据库连接。常见问题包括无法写入注册表、试用期续费及连接数据库权限问题。高级功能涵盖 SSH 通道加速、自动化任务调度和性能调优建议。
2445 19
|
8月前
|
SQL Oracle 数据库
这款免费数据库工具,可能是YashanDB图形化管理的最佳选择
DBeaver for YashanDB 是一款专为国产自研数据库 YashanDB 定制的图形化管理工具,基于全球流行的开源数据库工具 DBeaver 二次开发而成。它深度适配 YashanDB 的各种架构,支持 HEAP/LSC 多形态表管理和 Oracle 生态兼容,提供高效的对象管理、智能 SQL 开发和工业级 PL/SQL 调试功能。通过可视化操作,开发者可轻松完成物化视图配置、存储过程管理等复杂任务,大幅提升效率。该工具完全免费,支持多平台,为企业级数据库管理提供了成熟解决方案。