转自:http://blog.csdn.net/stark_summer/article/details/44174381

未实验

最近一个群友的boss让研究hbase,让hbase的入库速度达到5w+/s,这可愁死了,4台个人电脑组成的集群,多线程入库调了好久,速度也才1w左右,都没有达到理想的那种速度,然后就想到了这种方式,但是网上多是用mapreduce来实现入库,而现在的需求是实时入库,不生成文件了,所以就只能自己用代码实现了,但是网上查了很多资料都没有查到,最后在一个网友的指引下,看了源码,最后找到了生成Hfile的方式,实现了之后,发现单线程入库速度才达到1w4左右,和之前的多线程的全速差不多了,百思不得其解之时,调整了一下代码把列的Byte.toBytes(cols)这个方法调整出来只做一次,速度立马就到3w了,提升非常明显,这是我的电脑上的速度,估计在它的集群上能更快一点吧,下面把代码和大家分享一下。

    String tableName = "taglog";
byte[] family = Bytes.toBytes("logs");
//配置文件设置
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.master", "192.168.1.133:60000");
conf.set("hbase.zookeeper.quorum", "192.168.1.135");
//conf.set("zookeeper.znode.parent", "/hbase");
conf.set("hbase.metrics.showTableName", "false");
//conf.set("io.compression.codecs", "org.apache.hadoop.io.compress.SnappyCodec"); String outputdir = "hdfs://hadoop.Master:8020/user/SEA/hfiles/";
Path dir = new Path(outputdir);
Path familydir = new Path(outputdir, Bytes.toString(family));
FileSystem fs = familydir.getFileSystem(conf);
BloomType bloomType = BloomType.NONE;
final HFileDataBlockEncoder encoder = NoOpDataBlockEncoder.INSTANCE;
int blockSize = 64000;
Configuration tempConf = new Configuration(conf);
tempConf.set("hbase.metrics.showTableName", "false");
tempConf.setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 1.0f);
//实例化HFile的Writer,StoreFile实际上只是HFile的轻量级的封装
StoreFile.Writer writer = new StoreFile.WriterBuilder(conf, new CacheConfig(tempConf),
fs, blockSize)
.withOutputDir(familydir)
.withCompression(Compression.Algorithm.NONE)
.withBloomType(bloomType).withComparator(KeyValue.COMPARATOR)
.withDataBlockEncoder(encoder).build();
long start = System.currentTimeMillis(); DecimalFormat df = new DecimalFormat("0000000"); KeyValue kv1 = null;
KeyValue kv2 = null;
KeyValue kv3 = null;
KeyValue kv4 = null;
KeyValue kv5 = null;
KeyValue kv6 = null;
KeyValue kv7 = null;
KeyValue kv8 = null; //这个是耗时操作,只进行一次
byte[] cn = Bytes.toBytes("cn");
byte[] dt = Bytes.toBytes("dt");
byte[] ic = Bytes.toBytes("ic");
byte[] ifs = Bytes.toBytes("if");
byte[] ip = Bytes.toBytes("ip");
byte[] le = Bytes.toBytes("le");
byte[] mn = Bytes.toBytes("mn");
byte[] pi = Bytes.toBytes("pi"); int maxLength = 3000000;
for(int i=0;i<maxLength;i++){
String currentTime = ""+System.currentTimeMillis() + df.format(i);
long current = System.currentTimeMillis();
//rowkey和列都要按照字典序的方式顺序写入,否则会报错的
kv1 = new KeyValue(Bytes.toBytes(currentTime),
family, cn,current,KeyValue.Type.Put,Bytes.toBytes("3")); kv2 = new KeyValue(Bytes.toBytes(currentTime),
family, dt,current,KeyValue.Type.Put,Bytes.toBytes("6")); kv3 = new KeyValue(Bytes.toBytes(currentTime),
family, ic,current,KeyValue.Type.Put,Bytes.toBytes("8")); kv4 = new KeyValue(Bytes.toBytes(currentTime),
family, ifs,current,KeyValue.Type.Put,Bytes.toBytes("7")); kv5 = new KeyValue(Bytes.toBytes(currentTime),
family, ip,current,KeyValue.Type.Put,Bytes.toBytes("4")); kv6 = new KeyValue(Bytes.toBytes(currentTime),
family, le,current,KeyValue.Type.Put,Bytes.toBytes("2")); kv7 = new KeyValue(Bytes.toBytes(currentTime),
family, mn,current,KeyValue.Type.Put,Bytes.toBytes("5")); kv8 = new KeyValue(Bytes.toBytes(currentTime),
family,pi,current,KeyValue.Type.Put,Bytes.toBytes("1")); writer.append(kv1);
writer.append(kv2);
writer.append(kv3);
writer.append(kv4);
writer.append(kv5);
writer.append(kv6);
writer.append(kv7);
writer.append(kv8);
} writer.close(); //把生成的HFile导入到hbase当中
HTable table = new HTable(conf,tableName);
LoadIncrementalHFiles loader = new LoadIncrementalHFiles(conf);
loader.doBulkLoad(dir, table);      最后再附上查看hfile的方式,查询正确的hfile和自己生成的hfile,方便查找问题。
  hbase org.apache.hadoop.hbase.io.hfile.HFile -p -f hdfs://hadoop.Master:8020/user/SEA/hfiles/logs/51aa97b2a25446f89d5c870af92c9fc1

非mapreduce生成Hfile,然后导入hbase当中的更多相关文章

  1. hbase 学习(十二)非mapreduce生成Hfile,然后导入hbase当中

    最近一个群友的boss让研究hbase,让hbase的入库速度达到5w+/s,这可愁死了,4台个人电脑组成的集群,多线程入库调了好久,速度也才1w左右,都没有达到理想的那种速度,然后就想到了这种方式, ...

  2. MapReduce生成HFile入库到HBase

    转自:http://www.cnblogs.com/shitouer/archive/2013/02/20/hbase-hfile-bulk-load.html 一.这种方式有很多的优点: 1. 如果 ...

  3. MapReduce生成HFile入库到HBase及源码分析

    http://blog.pureisle.net/archives/1950.html

  4. 使用MapReduce将HDFS数据导入到HBase(三)

    使用MapReduce生成HFile文件,通过BulkLoader方式(跳过WAL验证)批量加载到HBase表中 package com.mengyao.bigdata.hbase; import j ...

  5. MapReduce将HDFS文本数据导入HBase中

    HBase本身提供了很多种数据导入的方式,通常有两种常用方式: 使用HBase提供的TableOutputFormat,原理是通过一个Mapreduce作业将数据导入HBase 另一种方式就是使用HB ...

  6. 通过生成HFile导入HBase

    要实现DataFrame通过HFile导入HBase有两个关键步骤 第一个是要生成Hfile第二个是HFile导入HBase 测试DataFrame数据来自mysql,如果对读取mysql作为Data ...

  7. Spark:DataFrame批量导入Hbase的两种方式(HFile、Hive)

    Spark处理后的结果数据resultDataFrame可以有多种存储介质,比较常见是存储为文件.关系型数据库,非关系行数据库. 各种方式有各自的特点,对于海量数据而言,如果想要达到实时查询的目的,使 ...

  8. Hbase 学习(十一)使用hive往hbase当中导入数据

    我们可以有很多方式可以把数据导入到hbase当中,比如说用map-reduce,使用TableOutputFormat这个类,但是这种方式不是最优的方式. Bulk的方式直接生成HFiles,写入到文 ...

  9. 使用MapReduce将HDFS数据导入到HBase(二)

    package com.bank.service; import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf. ...

随机推荐

  1. Top PG Clustering HA Solutions for PostgreSQL

    转自:https://severalnines.com/blog/top-pg-clustering-ha-solutions-postgresql If your system relies on  ...

  2. IP地址转换函数

    只适用于IPV4 inet_addr函数将用点分十进制字符串表示的IPv4地址转化为用网络字节序整数表示的IPv4地址. 失败时返回INADDR_NONE. inet_aton函数完成和inet_ad ...

  3. 我的虚拟机静态IP配置

  4. ML(1)——机器学习简述

    简述 机器学习是人工智能的一种实现方式:深度学习是一种实现机器学习的技术,或者说是一种特殊的机器学习方法,可以说广义上的机器学习也包括了深度学习,三者的关系如下图所示: 从判别垃圾邮件到无人驾驶技术, ...

  5. 【转】C#获取当前日期时间(转)

    我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间(2008-09-04 12 ...

  6. MySQL之 从复制延迟问题排查

    一.从库复制延迟问题 1.可能的原因如下(1)主从服务器处于不同的网络之中,由于网络延迟导致:(2)主从服务器的硬件配置不同,从服务器的硬件配置(包括内存,CPU,网卡等)远低于主服务器:(3)主库上 ...

  7. 高级OPENGL, 利用uniform块接口

    1.找到需要的uniform块的索引, 将程序对象的该uniform块索引绑定uniform 缓冲对象的绑定点 2.建立uniform缓冲对象,对象绑定GL_UNIFORM_BUFFER缓冲目标,为缓 ...

  8. elasticsearch 基础 语法总结

    1. es 使用 restful 风格的 api 备注: es 的 api  格式  基本是这个样     请求方式    /索引名/文档名/id?参数   ,但是 还有 很多不是这样的 请求,比如 ...

  9. htmlcleaner使用及xpath语法初探

    一.HtmlCleaner使用: 1.HtmlCleaner HtmlCleaner是一个开源的Java语言的Html文档解析器.HtmlCleaner能够重新整理HTML文档的每个元素并生成结构良好 ...

  10. 源代码安装Apache、Mysql、PHP

    源代码软件的优点:     获得最新版,能及时修复bug:     能自行修改和定制: 源代码打包形式:     .tar.gz和.tar.bz2格式居多: 完整性校验:     md5sum校验工具 ...