大数据应用之HBase数据插入性能优化之多线程并行插入测试案例
一、引言:
上篇文章提起关于HBase插入性能优化设计到的五个参数,从参数配置的角度给大家提供了一个性能测试环境的实验代码。根据网友的反馈,基于单线程的模式实现的数据插入毕竟有限。通过个人实测,在我的虚拟机环境下,单线程插入数据的值约为4w/s。集群指标是:CPU双核1.83,虚拟机512M内存,集群部署单点模式。本文给出了基于多线程并发模式的,测试代码案例和实测结果,希望能给大家一些启示:
二、源程序:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Put; public class HBaseImportEx {
static Configuration hbaseConfig = null;
public static HTablePool pool = null;
public static String tableName = "T_TEST_1";
static{
//conf = HBaseConfiguration.create();
Configuration HBASE_CONFIG = new Configuration();
HBASE_CONFIG.set("hbase.master", "192.168.230.133:60000");
HBASE_CONFIG.set("hbase.zookeeper.quorum", "192.168.230.133");
HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
hbaseConfig = HBaseConfiguration.create(HBASE_CONFIG); pool = new HTablePool(hbaseConfig, 1000);
}
/*
* Insert Test single thread
* */
public static void SingleThreadInsert()throws IOException
{
System.out.println("---------开始SingleThreadInsert测试----------");
long start = System.currentTimeMillis();
//HTableInterface table = null;
HTable table = null;
table = (HTable)pool.getTable(tableName);
table.setAutoFlush(false);
table.setWriteBufferSize(24*1024*1024);
//构造测试数据
List<Put> list = new ArrayList<Put>();
int count = 10000;
byte[] buffer = new byte[350];
Random rand = new Random();
for(int i=0;i<count;i++)
{
Put put = new Put(String.format("row %d",i).getBytes());
rand.nextBytes(buffer);
put.add("f1".getBytes(), null, buffer);
//wal=false
put.setWriteToWAL(false);
list.add(put);
if(i%10000 == 0)
{
table.put(list);
list.clear();
table.flushCommits();
}
}
long stop = System.currentTimeMillis();
//System.out.println("WAL="+wal+",autoFlush="+autoFlush+",buffer="+writeBuffer+",count="+count); System.out.println("插入数据:"+count+"共耗时:"+ (stop - start)*1.0/1000+"s"); System.out.println("---------结束SingleThreadInsert测试----------");
}
/*
* 多线程环境下线程插入函数
*
* */
public static void InsertProcess()throws IOException
{
long start = System.currentTimeMillis();
//HTableInterface table = null;
HTable table = null;
table = (HTable)pool.getTable(tableName);
table.setAutoFlush(false);
table.setWriteBufferSize(24*1024*1024);
//构造测试数据
List<Put> list = new ArrayList<Put>();
int count = 10000;
byte[] buffer = new byte[256];
Random rand = new Random();
for(int i=0;i<count;i++)
{
Put put = new Put(String.format("row %d",i).getBytes());
rand.nextBytes(buffer);
put.add("f1".getBytes(), null, buffer);
//wal=false
put.setWriteToWAL(false);
list.add(put);
if(i%10000 == 0)
{
table.put(list);
list.clear();
table.flushCommits();
}
}
long stop = System.currentTimeMillis();
//System.out.println("WAL="+wal+",autoFlush="+autoFlush+",buffer="+writeBuffer+",count="+count); System.out.println("线程:"+Thread.currentThread().getId()+"插入数据:"+count+"共耗时:"+ (stop - start)*1.0/1000+"s");
} /*
* Mutil thread insert test
* */
public static void MultThreadInsert() throws InterruptedException
{
System.out.println("---------开始MultThreadInsert测试----------");
long start = System.currentTimeMillis();
int threadNumber = 10;
Thread[] threads=new Thread[threadNumber];
for(int i=0;i<threads.length;i++)
{
threads[i]= new ImportThread();
threads[i].start();
}
for(int j=0;j< threads.length;j++)
{
(threads[j]).join();
}
long stop = System.currentTimeMillis(); System.out.println("MultThreadInsert:"+threadNumber*10000+"共耗时:"+ (stop - start)*1.0/1000+"s");
System.out.println("---------结束MultThreadInsert测试----------");
} /**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
//SingleThreadInsert();
MultThreadInsert(); } public static class ImportThread extends Thread{
public void HandleThread()
{
//this.TableName = "T_TEST_1"; }
//
public void run(){
try{
InsertProcess();
}
catch(IOException e){
e.printStackTrace();
}finally{
System.gc();
}
}
} }
三、说明
1.线程数设置需要根据本集群硬件参数,实际测试得出。否则线程过多的情况下,总耗时反而是下降的。
2.单笔提交数对性能的影响非常明显,需要在自己的环境下,找到最理想的数值,这个需要与单条记录的字节数相关。
四、测试结果
---------开始MultThreadInsert测试----------
线程:8插入数据:10000共耗时:1.328s
线程:16插入数据:10000共耗时:1.562s
线程:11插入数据:10000共耗时:1.562s
线程:10插入数据:10000共耗时:1.812s
线程:13插入数据:10000共耗时:2.0s
线程:17插入数据:10000共耗时:2.14s
线程:14插入数据:10000共耗时:2.265s
线程:9插入数据:10000共耗时:2.468s
线程:15插入数据:10000共耗时:2.562s
线程:12插入数据:10000共耗时:2.671s
MultThreadInsert:100000共耗时:2.703s
---------结束MultThreadInsert测试----------
备注:该技术专题讨论正在群Hadoop高级交流群:293503507同步直播中,敬请关注。
大数据应用之HBase数据插入性能优化之多线程并行插入测试案例的更多相关文章
- 大数据应用之HBase数据插入性能优化实测教程
引言: 大家在使用HBase的过程中,总是面临性能优化的问题,本文从HBase客户端参数设置的角度,研究HBase客户端数据批量插入性能优化的问题.事实胜于雄辩,数据比理论更有说服力,基于此,作者设计 ...
- TODOList 多线程交互、RCP、事物控制、数据倾斜、HBase数据同步性
TODOList 多线程交互.RCP.事物控制.数据倾斜.HBase数据同步性 TODO List thread.join()如何互相之间通知? 线程池何时最后运行完成? MemCache性能要优于R ...
- 【转载】HBase 数据库检索性能优化策略
转自:http://www.ibm.com/developerworks/cn/java/j-lo-HBase/index.html 高性能 HBase 数据库 本文首先介绍了 HBase 数据库基本 ...
- HBase 数据库检索性能优化策略--转
https://www.ibm.com/developerworks/cn/java/j-lo-HBase/index.html HBase 数据表介绍 HBase 数据库是一个基于分布式的.面向列的 ...
- HBase 数据库检索性能优化策略
HBase 数据表介绍 HBase 数据库是一个基于分布式的.面向列的.主要用于非结构化数据存储用途的开源数据库.其设计思路来源于 Google 的非开源数据库"BigTable" ...
- MySQL插入性能优化
目录 MySQL插入性能优化 代码优化 values 多个 一个事务 插入字段尽量少,尽量用默认值 关闭 unique_checks bulk_insert_buffer_size 配置优化 inno ...
- 《Spark大数据处理:技术、应用与性能优化 》
基本信息 作者: 高彦杰 丛书名:大数据技术丛书 出版社:机械工业出版社 ISBN:9787111483861 上架时间:2014-11-5 出版日期:2014 年11月 开本:16开 页码:255 ...
- 《Spark大数据处理:技术、应用与性能优化》【PDF】 下载
内容简介 <Spark大数据处理:技术.应用与性能优化>根据最新技术版本,系统.全面.详细讲解Spark的各项功能使用.原理机制.技术细节.应用方法.性能优化,以及BDAS生态系统的相关技 ...
- 《Spark大数据处理:技术、应用与性能优化》【PDF】
内容简介 <Spark大数据处理:技术.应用与性能优化>根据最新技术版本,系统.全面.详细讲解Spark的各项功能使用.原理机制.技术细节.应用方法.性能优化,以及BDAS生态系统的相关技 ...
随机推荐
- Java——File类成员方法
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...
- Crystal Report Error: Either the Crystal Reports registy key permission are insufficient or the Crystal Reports runtime is not installed correctly
在64位 Windows 7中水晶报表的错误: Crystal Report Error: Either the Crystal Reports registy key permission are ...
- 使用API接口在zabbix系统中登陆、创建、删除agent
一.API的介绍 API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力 ...
- isKindOfClass in cocos2d-x
在最新版2.*的cocos2d-x中isKindOfClass可以用如下代码代替. 未验证,不过看了引擎代码是这样写的 原代码 [s1 isKindOfClass:[DestHole class] ...
- JSP--TOMCAT-MYSQL web页面查询
queryStudent.jsp代码如下 <%@ page language="java" contentType="text/html; charset=gb23 ...
- ionic 编写自定义控件
创建组件 在项目所在目录下执行: ionic g component <ComponentName> 在src/components中会出现: --components |--Compon ...
- WKWebView中HTML5获取位置失败
WKWebView中HTML5获取位置失败,在info.plist文件中添加以下代码打开网页时就会询问是否允许获取位置信息了. <key>NSLocationAlwaysUsageDesc ...
- 第十七篇 make的路径搜索综合实践
本节我们编写路径搜索相关的makefile,具体需求如下: 1.工程项目中不希望源码文件夹在编译时被改动(只读文件夹). 2.在编译时自动创建文件夹(build)用于存放编译结果. 3.编译过 ...
- HTTPS网站的内幕
什么是HTTPS网站? HTTPS可以理解为HTTP+TLS,HTTP是互联网中使用最为广泛的协议,目前大部分的WEB应用和网站都是使用HTTP协议传输. 那网站为什么要实现HTTPS? 一言概之,为 ...
- poj-1239(递推关系)好难
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...