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

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 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调试功能,不断的解决报错问题(是个考验耐心和细心的过程,中间遇到的问题有些可能是需要不断搜索外文网站和一些社区论坛才能找到正确的解释,没办法,开源技术没有现成的资料和使用说明,就得靠自己摸索和不断网上搜索)。

相关实践学习
云数据库HBase版使用教程
  相关的阿里云产品:云数据库 HBase 版 面向大数据领域的一站式NoSQL服务,100%兼容开源HBase并深度扩展,支持海量数据下的实时存储、高并发吞吐、轻SQL分析、全文检索、时序时空查询等能力,是风控、推荐、广告、物联网、车联网、Feeds流、数据大屏等场景首选数据库,是为淘宝、支付宝、菜鸟等众多阿里核心业务提供关键支撑的数据库。 了解产品详情: https://cn.aliyun.com/product/hbase   ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
19天前
|
SQL 存储 关系型数据库
数据库开发之图形化工具以及表操作的详细解析
数据库开发之图形化工具以及表操作的详细解析
31 0
|
2月前
|
SQL 数据可视化 Java
DBeaver数据库可视化工具
DBeaver数据库可视化工具
40 3
|
2月前
|
Oracle 关系型数据库 Java
程序员必备推荐一款与Swagger媲美的数据库文档生成工具
程序员必备推荐一款与Swagger媲美的数据库文档生成工具
23 0
|
11天前
|
监控 关系型数据库 MySQL
初体验:数据库监控、管理和可观测性工具(PMM)
Percona Monitoring and Management (PMM) 是一个开源工具,用于监控MySQL、PostgreSQL和MongoDB的性能。它提供实时监控、数据可视化、故障排除和管理功能,支持本地和云端数据库。要安装PMM,首先需安装Docker,然后通过提供的脚本部署PMM服务器和客户端。在MySQL服务器上创建PMM用户后,使用`pmm-admin`命令添加数据库。访问PMM的HTTPS网址(默认用户名和密码为admin)进行配置。本文还包含了安装Docker和PMM的命令行步骤。
初体验:数据库监控、管理和可观测性工具(PMM)
|
2天前
|
SQL 存储 关系型数据库
性能诊断工具DBdoctor如何快速纳管数据库PolarDB-X
DBdoctor是一款基于eBPF技术的数据库性能诊断工具,已通过阿里云PolarDB分布式版(V2.3)认证。PolarDB-X是阿里云的高性能云原生分布式数据库,采用Shared-nothing和存储计算分离架构,支持高可用、水平扩展和低成本存储。PolarDB-X V2.3.0在读写混合场景下对比开源MySQL有30-40%的性能提升。DBdoctor能按MySQL方式纳管PolarDB-X的DN节点,提供性能洞察和诊断。用户可通过指定步骤安装PolarDB-X和DBdoctor,实现数据库的管理和性能监控。
|
2天前
|
Cloud Native 关系型数据库 分布式数据库
数据库性能诊断工具DBdoctor通过阿里云PolarDB产品生态集成认证
DBdoctor(V3.1.0)成功通过阿里云PolarDB分布式版(V2.3)集成认证,展现优秀兼容性和稳定性。此工具是聚好看科技的内核级数据库性能诊断产品,运用eBPF技术诊断SQL执行,提供智能巡检、根因分析和优化建议。最新版V3.1.1增加了对PolarDB-X和OceanBase的支持,以及基于cost的索引诊断功能。PolarDB-X是阿里巴巴的高性能云原生分布式数据库,兼容MySQL生态。用户可通过提供的下载地址、在线试用链接和部署指南体验DBdoctor。
|
12天前
|
SQL NoSQL 关系型数据库
【好用】Star超36.8k,一个的免费通用数据库管理工具
关于数据库管理工具,大家可能都在用SQLyog、Navicat、MySQL-Front、SQL Studio、MySQL Workbench等等,这些管理工具不是不好用,就是要变魔术才可以用,今天 V 哥给大家推荐一个即好用,又免费的可视化通用数据库管理工具,让你再也不用偷偷摸摸的了,光明正大放心用,真心好用。
|
17天前
|
SQL 关系型数据库 MySQL
stream-query多数据库进行CI测试
stream-query多数据库进行CI测试
15 0
|
17天前
|
存储 分布式计算 Hadoop
基于Hadoop分布式数据库HBase1.0部署及使用
基于Hadoop分布式数据库HBase1.0部署及使用
|
25天前
|
SQL JavaScript Java
闲不住,手写了数据库生成工具
`shigen`是一个擅长多种编程语言的博主,分享技术成长和认知。最近,他制作了一个数据库表结构导出工具,支持生成MD和DOCX文档。程序通过SQL(如`show tables`和`desc`)获取信息,展示包括字段注释在内的表详情。代码已分享,可用于学习交流。一起学习,每天进步!
16 0
闲不住,手写了数据库生成工具

热门文章

最新文章