一、引言:

  上篇文章提起关于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数据插入性能优化之多线程并行插入测试案例的更多相关文章

  1. 大数据应用之HBase数据插入性能优化实测教程

    引言: 大家在使用HBase的过程中,总是面临性能优化的问题,本文从HBase客户端参数设置的角度,研究HBase客户端数据批量插入性能优化的问题.事实胜于雄辩,数据比理论更有说服力,基于此,作者设计 ...

  2. TODOList 多线程交互、RCP、事物控制、数据倾斜、HBase数据同步性

    TODOList 多线程交互.RCP.事物控制.数据倾斜.HBase数据同步性 TODO List thread.join()如何互相之间通知? 线程池何时最后运行完成? MemCache性能要优于R ...

  3. 【转载】HBase 数据库检索性能优化策略

    转自:http://www.ibm.com/developerworks/cn/java/j-lo-HBase/index.html 高性能 HBase 数据库 本文首先介绍了 HBase 数据库基本 ...

  4. HBase 数据库检索性能优化策略--转

    https://www.ibm.com/developerworks/cn/java/j-lo-HBase/index.html HBase 数据表介绍 HBase 数据库是一个基于分布式的.面向列的 ...

  5. HBase 数据库检索性能优化策略

    HBase 数据表介绍 HBase 数据库是一个基于分布式的.面向列的.主要用于非结构化数据存储用途的开源数据库.其设计思路来源于 Google 的非开源数据库"BigTable" ...

  6. MySQL插入性能优化

    目录 MySQL插入性能优化 代码优化 values 多个 一个事务 插入字段尽量少,尽量用默认值 关闭 unique_checks bulk_insert_buffer_size 配置优化 inno ...

  7. 《Spark大数据处理:技术、应用与性能优化 》

    基本信息 作者: 高彦杰 丛书名:大数据技术丛书 出版社:机械工业出版社 ISBN:9787111483861 上架时间:2014-11-5 出版日期:2014 年11月 开本:16开 页码:255 ...

  8. 《Spark大数据处理:技术、应用与性能优化》【PDF】 下载

    内容简介 <Spark大数据处理:技术.应用与性能优化>根据最新技术版本,系统.全面.详细讲解Spark的各项功能使用.原理机制.技术细节.应用方法.性能优化,以及BDAS生态系统的相关技 ...

  9. 《Spark大数据处理:技术、应用与性能优化》【PDF】

    内容简介 <Spark大数据处理:技术.应用与性能优化>根据最新技术版本,系统.全面.详细讲解Spark的各项功能使用.原理机制.技术细节.应用方法.性能优化,以及BDAS生态系统的相关技 ...

随机推荐

  1. learning shell built-in variables (1)

    Shell built-in variables [Purpose]        Learning shell built-in variables, example $0,$1,$2,$3,$#, ...

  2. RM报表,点击保存,为何每次都显示 另存为的对话框?

    function TRMDesignerForm.FileSave: Boolean; var lSaved: Boolean; lFileName: string; begin Result := ...

  3. 日期控件:My97DatePicker

    My97DatePicker是一款非常灵活好用的日期控件.使用非常简单. 1.下载My97DatePicker组件包 下载地址:http://download.csdn.net/detail/emov ...

  4. 玩转X-CTR100 l 平台-4WD智能小车

    我造轮子,你造车,创客一起造起来!更多塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 本文介绍使用X-CTR100控制器搭建4WD智能 ...

  5. 201621123010《Java程序设计》第14周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 2. 使用数据库技术改造你的系统 2.1 简述如何使用数据库技术改造你的系统.要建立什么表?截图你的表设计. 答 ...

  6. L236

    The Norwegian Authority for Investigation of Economic and Environmental Crime (Okokrim) said the mov ...

  7. REST easy with kbmMW #15 – Handling HTTP POST

    我被问到有关如何通过基于kbmMW智能服务(Smart Service)的REST处理POST的问题. 这篇博客文章解释了典型的POST各种形式的访问,以及如何在kbmMW中处理它们. POST变种W ...

  8. UITableView简述

    原帖:http://blog.csdn.net/totogo2010/article/details/7642908 Table View简单描述: 在iPhone和其他iOS的很多程序中都会看到Ta ...

  9. 解决:People下面选择分享可见联系人,选择多个联系人后通过短信分享,短信中只显示一个联系人

    问题描述: [操作步骤]:People下导入导出中选择分享可见联系人,选择多个联系人后通过短信分享 [测试结果]:短信中只能显示一个联系人 [预期结果]:可以显示多个联系人 经过代码分析,从compo ...

  10. SQL之join

    QL join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据. 有时为了得到完整的结果,我们需要从两个或更多的表中获取结果.我们就需要执行 join. 数据库中的表可通过键将彼此联系起来. ...