最近一个群友的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

hbase 学习(十二)非mapreduce生成Hfile,然后导入hbase当中的更多相关文章

  1. MapReduce生成HFile入库到HBase

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

  2. 非mapreduce生成Hfile,然后导入hbase当中

    转自:http://blog.csdn.net/stark_summer/article/details/44174381 未实验 最近一个群友的boss让研究hbase,让hbase的入库速度达到5 ...

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

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

  4. Java进阶(五十二)利用LOG4J生成服务日志

    Java进阶(五十二)利用LOG4J生成服务日志 前言 由于论文写作需求,需要进行流程挖掘.前提是需要有真实的事件日志数据.真实的事件日志数据可以用来发现.监控和提升业务流程. 为了获得真实的事件日志 ...

  5. (转)SpringMVC学习(十二)——SpringMVC中的拦截器

    http://blog.csdn.net/yerenyuan_pku/article/details/72567761 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter, ...

  6. HBase学习(二) 基本命令 Java api

    一.Hbase shell 1.Region信息观察 创建表指定命名空间 在创建表的时候可以选择创建到bigdata17这个namespace中,如何实现呢? 使用这种格式即可:'命名空间名称:表名' ...

  7. 【Hbase学习之二】Hbase 搭建

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 hbase-2.1.3 一.单机模 ...

  8. Scala学习十二——高阶函数

    一.本章要点 在Scala中函数是”头等公民“(可以作为参数,返回值,赋值给其他); 可以创建匿名函数,通常还会交给其他函数; 函数参数可以给出需要稍后执行的行为; 许多集合方法都接受函数参数,将函数 ...

  9. hbase学习(二)hbase单机和高可用完全分布式安装部署

    hbase版本 2.0.4  与hadoop兼容表http://hbase.apache.org/book.html#hadoop  我的 hadoop版本是3.1   1.单机版hbase 1.1解 ...

随机推荐

  1. Python 爬虫实例(15) 爬取 汽车之家(汽车授权经销商)

    有人给我吹牛逼,说汽车之家反爬很厉害,我不服气,所以就爬取了一下这个网址. 本片博客的目的是重点的分析定向爬虫的过程,希望读者能学会爬虫的分析流程. 一:爬虫的目标: 打开汽车之家的链接:https: ...

  2. mysql 大数据提取

    今天要重五百多万的一个数据库表 提取 大约五十万条数据,刚开始的解决思路是: 先把数据查询出来,然后再导出来,然后再设计一个数据库表格,把这些数据导入,最后导出数据和导入数据花费了很多时间,最后向同事 ...

  3. Python selenium 滚动条 详解

    在我们使用Python + selenium 爬虫的时候,会遇到如下报错,原因是  当页面上的元素超过一屏后,想操作屏幕下方的元素,是不能直接定位到,会报元素不可见的. selenium.common ...

  4. Python 爬虫 大量数据清洗 ---- sql语句优化

    . 问题描述 在做爬虫的时候,数据量很大,大约有五百百万条数据,假设有个字段是conmany_name(拍卖公司名称),我们现在需要从五百万条数据里面查找出来五十家拍卖公司, 并且要求字段 time( ...

  5. MSSQL2005:“超时时间已到。在操作完成之前超时时间已过或服务器未响应”

    1.今天在整合项目中有这样一个需求,就是要改变以存在表字段的文本的大小,如把char(15)改成varchar(50). 2.此时以存在表已有1885742条数据,在直接下面进行调用 ALTER TA ...

  6. Four Node.js Gotchas that Operations Teams Should Know about

    There is no doubt that Node.js is one of the fastest growing platforms today. It can be found at sta ...

  7. JDK1.6新特性,WebService强化

    Web service是一个平台独立的,松耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布式的互操作的应用程序. Web ...

  8. [LeetCode] Meeting Rooms I & II

    Meeting Rooms Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s ...

  9. Android 架构师技能图谱(转载)

    架构与设计 设计模式 重构 技术选型 特性 可用性 性能 包大小 方法数 文档 技术支持 UI架构模式 MVC MVP MVVM 研发工具 集成开发环境 Android Studio Sublime ...

  10. PHP mysqli方式连接类

    分享一个PHP以mysqli方式连接类完整代码实例,有关mysqli用法实例. 一个在PHP中以mysqli方式连接数据库的一个数据库类实例,该数据库类是从一个PHP的CMS中整理出来的,可实现PHP ...