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

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 由于在网上找不到Jmeter连接Hbase的源文件或是插件,所以本文只是通过Jmeter的BeanShell来调用和调试Hbase的远程连接操作,具体性能测试时,需要怎么应用(比如通过Java Request等方式),等具体开展测试时再进行灵活扩展和调整。
版权声明:本文为博主原创文章,未经博主允许不得转载。欢迎访问我的博客 https://blog.csdn.net/smooth00/article/details/74279856

由于在网上找不到Jmeter连接Hbase的源文件或是插件,所以本文只是通过Jmeter的BeanShell来调用和调试Hbase的远程连接操作,具体性能测试时,需要怎么应用(比如通过Java Request等方式),等具体开展测试时再进行灵活扩展和调整。

关键的是要引用正确的Hbase jar包(还要保证版本的兼容,Hbase1.0开始就要求JDK1.7及以上,而且Jmeter引用的Hbase Jar包最好是与服务端的Hbase一致,否则也会出现兼容性问题)。以下的测试代码都是在Jmeter3.1+JDK1.8环境下进行的。

1、下载Hbase Jar包,可以到官网下载整个包,获取其中的lib jar包:http://archive.apache.org/dist/hbase/,也可以下载我为本次测试所整理好的jar包(性能测试工具所引用的hbase依赖包

2、在Jmeter的测试计划里把所有Jar包添加到classpath中(我不习惯把Jar包直接扔到Jmeter目录的lib\ext下,不方便区分管理)


3、将hadoop-common-2.2.0-bin-master文件目录放到Jmeter的bin目录下,主要是Windows下调用Hadoop组件,如果没有winutils.exe会报错(可以到网上下载这个包,上面提供的性能测试工具所引用的hbase依赖包里也有)。

4、在Jmeter中建立线程组,和建一个BeanShell Sampler,直接将以下代码复制上去进行调试(注意要配好zookeeper连接地址和端口)

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 java.io.File;

public class HbaseTest
{ 
     String tableName="student";
     Configuration configuration=null;	
     public void SetConfiguration() {    
   		configuration = HBaseConfiguration.create();       
   		//config.set("hbase.zookeeper.quorum", "hellotest");//单机            
   		configuration.set("hbase.zookeeper.quorum", "agent01.org.cn,agent02.org.cn,master.org.cn");
   		//包括三个节点的zookeeper地址(也可以是Host IP),第一个是主节点
   		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); 
     }

     /**
  * 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 ......");
    		log.info(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");
   		log.info("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!");
    		log.info(tableName+"delete success!");
   		}else{
    		System.out.println(tableName+"Table does not exist!");
    		log.info(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!");
    		log.info("add success!");
   		}else{
    		System.out.println(tableName+"Table does not exist!");
    		log.info(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!");
    		log.info("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))+" ");
    
    		log.info("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
    		log.info("Timetamp:"+cell.getTimestamp()+" ");
    		log.info("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
    		log.info("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
    		log.info("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.next(); result != null; result = results.next()) {
   		// print out the row we found and the columns we were looking for
    		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))+" ");

     			log.info("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
     			log.info("Timetamp:"+cell.getTimestamp()+" ");
     			log.info("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
     			log.info("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
     			log.info("value:"+new String(CellUtil.cloneValue(cell))+" ");
    		}
   		}
  	} catch (IOException e) {
   		// TODO Auto-generated catch block
   		e.printStackTrace();
  	}  
 }
 public boolean runActions(){
	 	File directory = new File(".");
	 	System.setProperty("hadoop.home.dir", directory.getCanonicalPath()+"\\hadoop-common-2.2.0-bin-master");
  		 SetConfiguration();
	      createTable(configuration, tableName);
	      addData(configuration, tableName);
	      getData(configuration, tableName);
	      getAllData(configuration, tableName);
	      deleteDate(configuration, tableName);
	      dropTable(configuration, tableName);
	      return true;
	}
}

boolean runFlag=new HbaseTest().runActions();
log.info("runFlag:"+runFlag);
以上代码中,加了log.info,用来调试代码以验证代码是否执行成功

5、在线程组中加上查看结果树、Summary Report、Debug Sampler,以全运行时查看成功情况。通过点击Jmeter右上方的三角!号(如果代码报错也能看能看到提示),可以查看代码运行日志(这是调试Java代码比较方便的工具),运行结果如下:

6、可以将以上的测试代码保存成java(或者可以打成jar包再用),直接通过jmerter进行引用和测试

7、相对来讲,Jmeter不适合调试Java代码,因为报一段长长的错误日志后,可能报错的最后一句话才能看出错误所在,所以以上代码也可以等在Java开发工具中调测通过后,再移植过来,然后再进一步调试和修改一些兼容性的错误。

相关实践学习
云数据库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
目录
相关文章
|
6天前
|
SQL 关系型数据库 MySQL
stream-query多数据库进行CI测试
stream-query多数据库进行CI测试
14 0
|
30天前
|
编解码 NoSQL 测试技术
性能工具之Jmeter HLS 插件(入门篇)
【2月更文挑战第28天】JMeter Redis 数据集 vs CSV 数据集性能对比
66 1
性能工具之Jmeter HLS 插件(入门篇)
|
30天前
|
JavaScript jenkins 测试技术
这10款性能测试工具,收藏起来,测试人的工具箱!
这10款性能测试工具,收藏起来,测试人的工具箱!
|
1月前
|
关系型数据库 Java 数据库连接
PostgreSQL从小白到高手教程 - 第47讲:JMETER工具使用
PostgreSQL从小白到高手教程 - 第47讲:JMETER工具使用
106 3
|
1月前
|
编解码 测试技术 索引
性能工具之 Jmeter 使用 HTTP 请求编写 HLS 脚本
在我们简要介绍了 HLS 协议的基础知识,接下来我们详细介绍一种使用 Jmeter 编写压测 HLS 协议脚本的方法。
70 1
性能工具之 Jmeter 使用 HTTP 请求编写 HLS 脚本
|
1月前
|
数据库
ABAP 泛型编程实战 - 分享一个数据库表内容的拷贝工具试读版
ABAP 泛型编程实战 - 分享一个数据库表内容的拷贝工具试读版
17 0
|
2月前
|
运维 安全 关系型数据库
参加数据库管理工具DAS训练营,赢取国潮保温杯和阿里云定制双肩包!
本训练营带您简单了解数据库自治与云安全服务,数据库自治服务提供云上RDS、PolarDB、NoSQL、ADB等数据库7*24小时异常检测、SQL自优化、安全合规审计、弹性伸缩、数据自治、锁分析等亮点功能。一站式自动化、数字化DAS集成平台,助力您畅享DBA运维智能化。
|
28天前
|
关系型数据库 MySQL 数据库
rds安装数据库客户端工具
安装阿里云RDS的数据库客户端涉及在本地安装对应类型(如MySQL、PostgreSQL)的客户端工具。对于MySQL,可选择MySQL Command-Line Client或图形化工具如Navicat,安装后输入RDS实例的连接参数进行连接。对于PostgreSQL,可以使用`psql`命令行工具或图形化客户端如PgAdmin。首先从阿里云控制台获取连接信息,然后按照官方文档安装客户端,最后配置客户端连接以确保遵循安全指引。
81 1
|
11天前
|
JSON 测试技术 数据格式
性能工具之Jmeter关联入门
【4月更文挑战第4天】关联是每个性能测试人员必须掌握的技能,是解决性能脚本中的"金钥匙"。
22 2
性能工具之Jmeter关联入门
|
20天前
|
测试技术 数据库连接 数据库
测试环境的数据库连不了,打包报错怎么办
测试环境的数据库连不了,打包报错怎么办
13 0