当前位置: 首页 > 科技观察

HBase实战(一):数据导入方法

时间:2023-03-16 13:09:17 科技观察

*)。ClientAPI实现使用HBaseClientAPI导入,是最简单易学的方式。Configurationconfig=HBaseConfiguration.create();//配置hbase.zookeeper.quorum:后面是zookeeper集群的机器列表config.set("hbase.zookeeper.quorum","tw-node109,tw-node110,tw-node111");//配置hbase.zookeeper.property.clientPort:zookeeper集群的服务端口config.set("hbase.zookeeper.property.clientPort","2181");HTablehtable=null;try{  //配置hbase的具体表名  htable=newHTable(config,"hbase_table");  //设置rowkey的值  Putput=newPut(Bytes.toBytes("rowkey:1001"));  //设置family:qualifier:value  put.add(Bytes.toBytes("family"),Bytes.toBytes("qualifier"),Bytes.toBytes("value"));  //使用put类写入hbase对应的表  htable.put(put);}catch(Exceptione){  e.printStackTrace();}最后{  if(htable!=null){    try{      htable.close();  }catch(IOExceptione){      e.printStackTrace();    }  }}点评:HBase客户端api编程比较简单。唯一需要注意的是,如果在本地写测试列,需要在本地配置hbase集群相关的域名,让域名和ip地址对应起来。记住。至于hbase客户端的读写优化,我们会在后面的博文中讲解。*)。BulkloadHBase批量加载数据导入分为两个阶段:#)。Stage1:借助MapReduce使用HFileOutputFormat,直接生成内部HBase的数据存储格式为HFile。其原理:HFileOutputFormat利用configureIncrementalLoad函数,根据当前表的各个region的边界,自动匹配MapReduce分区类TotalOrderPartitioner,使得生成的HFile对应到一个特定的region,此时效率最高。#)。Phase2:借助completebulkload工具,将生成的HFile文件热加载到hbase集群中。1、importtsv数据导入演示hbase自带importtsv工具,默认支持tsv格式的数据文件。数据文件data.tsv(以'\t'分割数据数据文件的形式)12341001Lilei1713800001111111111112LILY16138000011121003-lucy16138000011131004Meimei1004meimei161380000001114/test/test/test/test/hbase/测试/hbase/tsv/inputsudo-uhdfshdfsdfs-putdata。tsv/test/hbase/tsv/input/HBase表studenthbase试图构建shellhbase>create'student',{NAME=>'info'}执行importtsvsudo-uhdfshadoopjar/usr/lib/hbase/hbase-.jarimporttsv-Dimporttsv.columns=HBASE_ROW_KEY,info:name,info:age,info:phone-Dimporttsv.bulk.output=/test/hbase/tsv/output/student/test/hbase/tsv/input未指定-Dimporttsv.bulk.output,importtsv的默认行为是有客户端api的put将数据导入到hbase中,如果指定了-Dimporttsv.bulk.output,下一步就是sudo-uhdfshadoopjar/usr/lib/hbase/hbase-.jarcompletebulkload/test/hbase/tsv/output/student数据校验:scan'student',{LIMIT=>10}2.自定义bulkload数据导入demo数据文件准备,建HBase表student_newhbase>create'student_new',{NAME=>'info'}并在前面的基础上编写MapReduce代码data.tsv文件,如下图:nterruptedException{super.setup(context);}@Overrideprotectedvoidmap(LongWritablekey,Textupthrowtevalue,Contextrcontext)dException{//数据按\t划分组织,也可以自定义解析,比如复杂的json/xml文本行Stringline=value.toString();String[]terms=line.split("\t");if(terms.length==4){byte[]rowkey=terms[0].getBytes();ImmutableBytesWritableimrowkey=newImmutableBytesWritable(rowkey);//写入context,rowkey=>keyvalue,列族:列名info:name,info:age,info:phonecontext.write(imrowkey,newKeyValue(rowkey,Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes(terms[1])));context.write(imrowkey,newKeyValue(rowkey,Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes(terms[2])));context.write(imrowkey,newKeyValue(rowkey,Bytes.toBytes("信息"),Bytes.toBytes("电话"),Bytes.toBytes(terms[3])));}}}publicstaticvoidmain(String[]args)throwsException{if(args.length!=3){System.err.println("Usage:MyBulkload");System.exit(2);}StringtableName=args[0];StringinputPath=args[1];StringooutputPath=args[2];//创建的HTable实例用于获取导入表的元信息,包括region的key范围划分Configurationconf=HBaseConfiguration.create();HTabletable=newHTable(conf,tableName);Jobjob=Job.getInstance(conf,"MyBulkload");job.setMapperClass(MyBulkMapper.class);job.setJarByClass(MyBulkload.class);job.setInputFormatClass(TextInputFormat.class);//最重要的配置代码需要重点关于分析HFileOutputFormat.configureIncrementalLoad(job,table);FileInputFormat.addInputPath(job,newPath(inputPath));FileOutputFormat.setOutputPath(job,newPath(outputPath));System.exit(job.waitForCompletion(true)?0:1);}}注意:借助maven的assembly插件,生成fatjar包(即将依赖的zookeeper和hbasejar包打入MapReduce包),否则用户需要静态配置,添加zookeeper和hbase到Hadoop类的配置文件和相关的jar包。最终jar包为mybulk.jar,主类名为com.m8zmyp.mmxf.MyBulkload,生成HFile,增量热加载到hbasesudo-uhdfshadoopjar.jarhbaseorg.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles<table_name>sudo-uhdfshadoopjarmybulk.jarcom.m8zmyp.mmxf.MyBulkloadstudent_new/test/hbase/tsv/input/test/hbase/tsv/new_outputstudent_newdata验证:扫描'student_new',{LIMIT=>10}*)。使用HiveOverHbase建Hbase表hbase_studenthbase>create'hbase_student','info'建hive表hive_student,对应的hbase_student表CREATEEXTERNALTABLEhive_student(rowkeystring,namestring,ageint,phonestring)STOREDBY'org.apache.hadoop.hive.hbase.HBaseStorageHandler'WITHSERDEPROPERTIES("hbase.columns.mapping"=":key,info:name,info:age,info:phone")TBLPROPERTIES("hbase.table.name"="hbase_student");数据导入验证:1.创建数据表CREATEEXTERNALTABLEdata_student(rowkeystring,namestring,ageint,phonestring)ROWFORMATDELIMITEDFIELDSTERMINATEDBY'\t'LOCATION'/test/hbase/tsv/input/';2.数据通过hive_student导入hbase_student表SEThive.hbase.bulk=true;INSERTOVERWRITETABLEhive_studentSELECTrowkey,name,年龄,phoneFROMdata_student;备注:如果遇到java.lang.IllegalArgumentException:Propertyvaluemustnotbenull,需要hive-0.13.0及以上。详情参见:https://issues.apache.org/jira/browse/HIVE-5515原文链接:http://www.cnblogs.com/mumuxinfei/p/3823367.html