自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HBase實(shí)戰(zhàn)(1):數(shù)據(jù)導(dǎo)入方式

數(shù)據(jù)庫
作為Hadoop生態(tài)系統(tǒng)中重要的一員, HBase作為分布式列式存儲(chǔ), 在線實(shí)時(shí)處理的特性, 備受矚目, 將來能在很多應(yīng)用場(chǎng)景, 取代傳統(tǒng)關(guān)系型數(shù)據(jù)庫的江湖地位. 本篇博文重點(diǎn)講解HBase的數(shù)據(jù)導(dǎo)入, 描述三種方式, Client API, Bulkload, 以及Hive Over HBase。

 *). Client API實(shí)現(xiàn)

借助HBase的Client API來導(dǎo)入, 是最簡易學(xué)的方式.

  1. Configuration config = HBaseConfiguration.create(); 
  2. // 配置hbase.zookeeper.quorum: 后接zookeeper集群的機(jī)器列表 
  3. config.set("hbase.zookeeper.quorum""tw-node109,tw-node110,tw-node111"); 
  4. // 配置hbase.zookeeper.property.clientPort: zookeeper集群的服務(wù)端口 
  5. config.set("hbase.zookeeper.property.clientPort""2181"); 
  6.   
  7. HTable htable = null
  8. try { 
  9.   // 配置hbase的具體表名 
  10.   htable = new HTable(config, "hbase_table"); 
  11.   // 設(shè)置rowkey的值 
  12.   Put put = new Put(Bytes.toBytes("rowkey:1001")); 
  13.   // 設(shè)置family:qualifier:value 
  14.   put.add(Bytes.toBytes("family"), Bytes.toBytes("qualifier"), Bytes.toBytes("value")); 
  15.   // 使用put類, 寫入hbase對(duì)應(yīng)的表中 
  16.   htable.put(put); 
  17. } catch (Exception e) { 
  18.   e.printStackTrace(); 
  19. } finally { 
  20.   if (htable != null) { 
  21.     try { 
  22.       htable.close(); 
  23.     } catch (IOException e) { 
  24.       e.printStackTrace(); 
  25.     } 
  26.   } 

評(píng): HBase的client api編程, 相對(duì)還是簡單的. 唯一需要注意的是, 若在本地編寫測(cè)試用列, 需要在本地配置hbase集群相關(guān)的域名, 使得域名和ip地址能對(duì)應(yīng)上, 切記.
至于hbase client的讀寫優(yōu)化, 我們放到下面的博文進(jìn)行講解.

*). 批量導(dǎo)入Bulkload
HBase的bulkload數(shù)據(jù)導(dǎo)入, 分兩個(gè)階段:
#). 階段一: 借助使用HFileOutputFormat的MapReduce, 直接生成HBase的內(nèi)部數(shù)據(jù)存儲(chǔ)格式HFile. 
其原理: HFileOutputFormat借助configureIncrementalLoad函數(shù), 基于當(dāng)前table的各個(gè)region邊界自動(dòng)匹配MapReduce的分區(qū)類TotalOrderPartitioner, 這樣生成的HFile都對(duì)應(yīng)一個(gè)具體的region, 此時(shí)效率最高效.
#). 階段二: 借助completebulkload工具, 將生成的HFile文件熱載入hbase集群.

1. importtsv數(shù)據(jù)導(dǎo)入演示
hbase自帶了importtsv工具, 其對(duì)tsv格式的數(shù)據(jù)文件提供了默認(rèn)的支持.
數(shù)據(jù)文件data.tsv(以'\t'分割數(shù)據(jù)文件)

1
2
3
4
1001    lilei   17  13800001111
1002    lily    16  13800001112
1003    lucy    16  13800001113
1004    meimei  16  13800001114

上傳至hdfs目錄 /test/hbase/tsv/input

  1. sudo -u hdfs hdfs dfs -mkdir -p /test/hbase/tsv/input 
  2. sudo -u hdfs hdfs dfs -put data.tsv /test/hbase/tsv/input/ 

嘗試構(gòu)建的HBase表student

  1. hbase shell 
  2. hbase> create 'student', {NAME => 'info'} 

執(zhí)行importtsv

  1. sudo -u hdfs hadoop jar /usr/lib/hbase/hbase-<version>.jar importtsv -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默認(rèn)行為是才有client api的put來導(dǎo)入數(shù)據(jù)于hbase, 指定-Dimporttsv.bulk.output, 則需要下一步

  1. sudo -u hdfs hadoop jar /usr/lib/hbase/hbase-<version>.jar completebulkload /test/hbase/tsv/output/ student 

數(shù)據(jù)驗(yàn)證:
scan 'student', {LIMIT => 10}

2. 自定義bulkload數(shù)據(jù)導(dǎo)入演示
數(shù)據(jù)文件準(zhǔn)備, 以之前data.tsv文件為準(zhǔn)
構(gòu)建HBase表student_new

  1. hbase> create 'student_new', {NAME => 'info'} 

編寫MapReduce代碼, 如下所示:

  1. public class MyBulkload { 
  2.   
  3.     public static class MyBulkMapper extends 
  4.             Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue> { 
  5.   
  6.         @Override 
  7.         protected void setup(Context context) throws IOException, 
  8.                 InterruptedException { 
  9.             super.setup(context); 
  10.         } 
  11.   
  12.         @Override 
  13.         protected void map(LongWritable key, Text value, Context context) 
  14.                 throws IOException, InterruptedException { 
  15.             // 數(shù)據(jù)按\t切分組織, 也可以自定義的方式來解析, 比如復(fù)雜的json/xml文本行 
  16.             String line = value.toString(); 
  17.             String[] terms = line.split("\t"); 
  18.             if ( terms.length == 4 ) { 
  19.                 byte[] rowkey = terms[0].getBytes(); 
  20.                 ImmutableBytesWritable imrowkey = new ImmutableBytesWritable(rowkey); 
  21.                 // 寫入context中, rowkey => keyvalue, 列族:列名  info:name, info:age, info:phone 
  22.                 context.write(imrowkey, new KeyValue(rowkey, Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes(terms[1]))); 
  23.                 context.write(imrowkey, new KeyValue(rowkey, Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes(terms[2]))); 
  24.                 context.write(imrowkey, new KeyValue(rowkey, Bytes.toBytes("info"), Bytes.toBytes("phone"), Bytes.toBytes(terms[3]))); 
  25.             } 
  26.         } 
  27.     } 
  28.   
  29.     public static void main(String[] args) throws Exception { 
  30.   
  31.         if ( args.length != 3 ) { 
  32.             System.err.println("Usage: MyBulkload <table_name> <data_input_path> <hfile_output_path>"); 
  33.             System.exit(2); 
  34.         } 
  35.         String tableName = args[0]; 
  36.         String inputPath = args[1]; 
  37.         String outputPathargs[2]; 
  38.   
  39.         // 創(chuàng)建的HTable實(shí)例用于, 用于獲取導(dǎo)入表的元信息, 包括region的key范圍劃分 
  40.         Configuration conf = HBaseConfiguration.create(); 
  41.         HTable table = new HTable(conf, tableName); 
  42.   
  43.         Job job = Job.getInstance(conf, "MyBulkload"); 
  44.           
  45.         job.setMapperClass(MyBulkMapper.class); 
  46.   
  47.         job.setJarByClass(MyBulkload.class); 
  48.         job.setInputFormatClass(TextInputFormat.class); 
  49.   
  50.         // 最重要的配置代碼, 需要重點(diǎn)分析 
  51.         HFileOutputFormat.configureIncrementalLoad(job, table); 
  52.   
  53.         FileInputFormat.addInputPath(job, new Path(inputPath)); 
  54.         FileOutputFormat.setOutputPath(job, new Path(outputPath)); 
  55.   
  56.         System.exit(job.waitForCompletion(true) ? 0 : 1); 
  57.           
  58.     } 
  59.   

注: 借助maven的assembly插件, 生成胖jar包(就是把依賴的zookeeper和hbase jar包都打到該MapReduce包中), 否則的話, 就需要用戶靜態(tài)配置, 在Hadoop的class中添加zookeeper和hbase的配置文件和相關(guān)jar包.

最終的jar包為 mybulk.jar, 主類名為com.m8zmyp.mmxf.MyBulkload, 生成HFile, 增量熱載入hbase

  1. sudo -u hdfs hadoop jar <xxoo>.jar <MainClass> <table_name> <data_input_path> <hfile_output_path> 
  2. hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles <hfile_output_path> <table_name> 

 

  1. sudo -u hdfs hadoop jar mybulk.jar com.m8zmyp.mmxf.MyBulkload student_new /test/hbase/tsv/input /test/hbase/tsv/new_output 
  2. hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles /test/hbase/tsv/new_output student_new 

數(shù)據(jù)驗(yàn)證:

  1. scan 'student_new', {LIMIT => 10} 

*). 借助Hive Over Hbase

構(gòu)建Hbase表hbase_student

  1. hbase> create 'hbase_student', 'info' 

構(gòu)建hive外表hive_student, 并對(duì)應(yīng)hbase_student表

  1. CREATE EXTERNAL TABLE hive_student (rowkey string, name string, age int, phone string) 
  2. STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' 
  3. WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:name,info:age,info:phone"
  4. TBLPROPERTIES("hbase.table.name" = "hbase_student"); 

數(shù)據(jù)導(dǎo)入驗(yàn)證:
1. 創(chuàng)建數(shù)據(jù)外表

  1. CREATE EXTERNAL TABLE data_student (rowkey string, name string, age int, phone string) 
  2. ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'  
  3. LOCATION '/test/hbase/tsv/input/'; 

2. 數(shù)據(jù)通過hive_student導(dǎo)入到hbase_student表中

  1. SET hive.hbase.bulk=true
  2. INSERT OVERWRITE TABLE hive_student SELECT rowkey, name, age, phone FROM data_student; 


備注: 若遇到j(luò)ava.lang.IllegalArgumentException: Property value must not be null異常, 需要hive-0.13.0及以上版本支持
詳見: https://issues.apache.org/jira/browse/HIVE-5515

原文鏈接:http://www.cnblogs.com/mumuxinfei/p/3823367.html

責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2019-09-16 12:55:27

HBaseKafka數(shù)據(jù)

2010-05-27 18:30:56

MySQL 命令行導(dǎo)入

2020-12-02 08:43:00

Flink SQLHBase場(chǎng)景

2024-04-09 13:20:00

Excel數(shù)據(jù)庫數(shù)據(jù)

2018-09-04 12:03:31

HBase大數(shù)據(jù)存儲(chǔ)

2021-06-25 17:41:35

騰訊NTA

2019-05-05 09:03:06

HBase大數(shù)據(jù)存儲(chǔ)數(shù)據(jù)存儲(chǔ)

2010-05-19 15:01:14

MySQL數(shù)據(jù)導(dǎo)入

2013-05-16 10:07:42

固態(tài)硬盤RAID 0三星840 Pro

2012-09-27 10:21:00

2019-08-08 15:05:26

HBase數(shù)據(jù)遷移命令

2009-11-16 11:31:54

Oracle數(shù)據(jù)導(dǎo)入

2023-11-09 08:38:25

交叉表組件大數(shù)據(jù)

2016-12-27 09:08:34

HBase數(shù)據(jù)流程

2018-10-29 13:07:15

HBase存儲(chǔ)遷移

2020-07-08 13:46:25

Python數(shù)據(jù)分析預(yù)處理

2010-04-14 09:24:29

在Oracle數(shù)據(jù)庫

2023-03-28 07:17:25

場(chǎng)景數(shù)據(jù)業(yè)務(wù)

2015-05-13 15:15:16

HadoopHBaseMapReduce

2023-06-01 08:00:00

圖像分割機(jī)器學(xué)習(xí)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)