Microsoft PowerPoint - hbase_program(0201).ppt

Similar documents
使用MapReduce读取XML文件

雲端 Cloud Computing 技術指南 運算 應用 平台與架構 10/04/15 11:55:46 INFO 10/04/15 11:55:53 INFO 10/04/15 11:55:56 INFO 10/04/15 11:56:05 INFO 10/04/15 11:56:07 INFO

Java ¿ª·¢ 2.0: Óà Hadoop MapReduce ½øÐдóÊý¾Ý·ÖÎö

JavaIO.PDF

chp6.ppt

三种方法实现Hadoop(MapReduce)全局排序(1)

1.JasperReport ireport JasperReport ireport JDK JDK JDK JDK ant ant...6

PowerPoint 演示文稿

MapReduce

EJB-Programming-4-cn.doc

操作指南产品文档

三种方法实现Hadoop(MapReduce)全局排序(2)

《大话设计模式》第一章

1: public class MyOutputStream implements AutoCloseable { 3: public void close() throws IOException { 4: throw new IOException(); 5: } 6:

untitled

Hadoop 集 群 ( 第 6 期 ) WordCount 运 行 详 解 1 MapReduce 理 论 简 介 1.1 MapReduce 编 程 模 型 MapReduce 采 用 分 而 治 之 的 思 想, 把 对 大 规 模 数 据 集 的 操 作, 分 发 给 一 个 主 节 点 管

(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

EJB-Programming-3.PDF

FileMaker 16 ODBC 和 JDBC 指南

Java java.lang.math Java Java.util.Random : ArithmeticException int zero = 0; try { int i= 72 / zero ; }catch (ArithmeticException e ) { // } 0,

关于天云趋势 天云趋势由宽带资本和趋势科技共同投资成立于 2010 年 3 月 趋势科技是 Hadoop 的重度使用者 : 2006 年开始使用, 用于处理网页和邮件服务器评级 五个数据中心, 近 1000 个节点, 最大集群约 500 台服务器 日均处理 3.6T 日志数据 亚洲最早, 也是最大的

3.1 num = 3 ch = 'C' 2

Chapter 9: Objects and Classes


untitled

Learning Java

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

Hive:用Java代码通过JDBC连接Hiveserver

使 用 Java 语 言 模 拟 保 险 箱 容 量 门 板 厚 度 箱 体 厚 度 属 性 锁 具 类 型 开 保 险 箱 关 保 险 箱 动 作 存 取 款

内 容 简 介 本 书 是 一 本 关 于 语 言 程 序 设 计 的 教 材, 涵 盖 了 语 言 的 基 本 语 法 和 编 程 技 术, 其 中 包 含 了 作 者 对 语 言 多 年 开 发 经 验 的 总 结, 目 的 是 让 初 学 的 读 者 感 受 到 语 言 的 魅 力, 并 掌

FileMaker 15 ODBC 和 JDBC 指南

Microsoft Word - Learn Objective-C.doc

主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new

詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入

Java Access 5-1 Server Client Client Server Server Client 5-2 DataInputStream Class java.io.datainptstream (extends) FilterInputStream InputStream Obj

untitled

Microsoft Word - 01.DOC

6-1 Table Column Data Type Row Record 1. DBMS 2. DBMS MySQL Microsoft Access SQL Server Oracle 3. ODBC SQL 1. Structured Query Language 2. IBM

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

1-1 database columnrow record field 不 DBMS Access Paradox SQL Server Linux MySQL Oracle IBM Informix IBM DB2 Sybase 1-2

开发指南

7521,WARD,SALESMAN,7698,22-2 月 -81,1250,500, ,JONES,MANAGER,7839,02-4 月 -81,2975,, ,MARTIN,SALESMAN,7698,28-9 月 -81,1250,1400, ,BLAK

Spark读取Hbase中的数据

投影片 1

ABOUT ME AGENDA 唐建法 / TJ MongoDB 高级方案架构师 MongoDB 中文社区联合发起人 Spark 介绍 Spark 和 MongoDB 案例演示

内 容 提 要 将 JAVA 开 发 环 境 迁 移 到 Linux 系 统 上 是 现 在 很 多 公 司 的 现 实 想 法, 而 在 Linux 上 配 置 JAVA 开 发 环 境 是 步 入 Linux 下 JAVA 程 序 开 发 的 第 一 步, 本 文 图 文 并 茂 地 全 程 指

( Version 0.4 ) 1

全国计算机技术与软件专业技术资格(水平)考试

1. 访 问 最 新 发 行 公 告 信 息 jconnect for JDBC 访 问 最 新 发 行 公 告 信 息 最 新 版 本 的 发 行 公 告 可 以 从 网 上 获 得 若 要 查 找 在 本 产 品 发 布 后 增 加 的 重 要 产 品 或 文 档 信 息, 请 访

untitled

The golden pins of the PCI card can be oxidized after months or years

untitled

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

Reducing Client Incidents through Big Data Predictive Analytics

附录J:Eclipse教程

學 科 100% ( 為 單 複 選 題, 每 題 2.5 分, 共 100 分 ) 1. 請 參 閱 附 圖 作 答 : (A) 選 項 A (B) 選 項 B (C) 選 項 C (D) 選 項 D Ans:D 2. 下 列 對 於 資 料 庫 正 規 化 (Normalization) 的 敘

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

untitled

SDK 概要 使用 Maven 的用户可以从 Maven 库中搜索 "odps-sdk" 获取不同版本的 Java SDK: 包名 odps-sdk-core odps-sdk-commons odps-sdk-udf odps-sdk-mapred odps-sdk-graph 描述 ODPS 基

目錄

untitled

untitled

<4D F736F F D F6F70B4F3CAFDBEDDBCB0BAA3C1BFCAFDBEDDCDDABEF2D3A6D3C3B9A4B3CCCAA6C5E0D1B5B0E056312E332E646F63>

javaexample-02.pdf

Partition Key: 字 符 串 类 型, 表 示 当 前 Entity 的 分 区 信 息 这 个 Property 对 于 Table Service 自 动 纵 向 和 横 向 扩 展 至 关 重 要 Row Key: 字 符 串 类 型, 在 给 定 Partition Key 的

输入 project name 选择完成

Chapter 9: Objects and Classes

RUN_PC連載_12_.doc

ebook39-6

Java

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

RUN_PC連載_8_.doc

untitled

Microsoft PowerPoint - Lecture7II.ppt

4.1 AMI MQSeries API AMI MQI AMI / / AMI JavaC C++ AMI / AMI AMI - / /

中 文 摘 要 智 慧 型 手 機 由 於 有 強 大 的 功 能, 以 及 優 渥 的 便 利 性, 還 能 與 網 路 保 持 隨 時 的 鏈 結 與 同 步 更 新, 因 此 深 受 廣 大 消 費 者 喜 愛, 當 然, 手 機 遊 戲 也 成 為 現 代 人 不 可 或 缺 的 娛 樂 之

业 务 与 运 营 Business & Operation (Transform) 加 载 (Load) 至 目 的 端 的 过 程, 该 部 分 在 数 据 挖 掘 和 分 析 过 程 中 为 最 基 础 的 一 部 分 一 个 良 好 的 ETL 系 统 应 该 有 以 下 几 个 功 能 1

任務二 : 產生 20 個有炸彈的磚塊, 放在隨機的位置編輯 Block 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class

27 :OPC 45 [4] (Automation Interface Standard), (Costom Interface Standard), OPC 2,,, VB Delphi OPC, OPC C++, OPC OPC OPC, [1] 1 OPC 1.1 OPC OPC(OLE f

白 皮 书 英 特 尔 IT 部 门 实 施 Apache Hadoop* 英 特 尔 分 发 版 软 件 的 最 佳 实 践 目 录 要 点 概 述...1 业 务 挑 战...2 Hadoop* 分 发 版 注 意 事 项...3 Hadoop* 基 础 架 构 注 意 事 项

1 1 大概思路 创建 WebAPI 创建 CrossMainController 并编写 Nuget 安装 microsoft.aspnet.webapi.cors 跨域设置路由 编写 Jquery EasyUI 界面 运行效果 2 创建 WebAPI 创建 WebAPI, 新建 -> 项目 ->

基于ECO的UML模型驱动的数据库应用开发1.doc

untitled

epub83-1

IoC容器和Dependency Injection模式.doc

x MapReduce A Italic URL Constant width Constant width bold Constant width italic 這個圖示代表提示或建議 這個圖示代表一般註解

目录 1 本期内容 MapReduce 理论简介 MapReduce 编程模型 MapReduce 处理过程 运行 WordCount 程序 准备工作 运行例子

Microsoft Word - 11.doc

Chapter 9: Objects and Classes

「人名權威檔」資料庫欄位建置表

單步除錯 (1/10) 打開 Android Studio, 點選 Start a new Android Studio project 建立專案 Application name 輸入 BMI 點下 Next 2 P a g e

CC213

D C 93 2

untitled

Swing-02.pdf

Guava学习之Resources

概述

Transcription:

TSMC 教育訓練課程 HBase Programming < V 0.20 > 王耀聰陳威宇 Jazz@nchc.org.tw waue@nchc.org.tw

Outline HBase 程式編譯方法 HBase 程式設計 常用的 HBase API 說明實做 I/O 操作搭配 Map Reduce 運算 其他用法補充 其他專案 2

HBase 程式編譯方法 此篇介紹兩種編譯與執行 HBase 程式的方法 : Method 1 使用 Java JDK 1.6 Method 2 使用 Eclipse 套件

1. Java 之編譯與執行 1. 將 hbase_home 目錄內的.jar 檔全部拷貝至 hadoop_home/lib/ 資料夾內 2. 編譯 javac Δ -classpath Δ hadoop-*-core.jar:hbase-*.jar Δ -d Δ MyJava Δ MyCode.java 3. 封裝 jar Δ -cvf Δ MyJar.jar Δ -C Δ MyJava Δ. 4. 執行 bin/hadoop Δ jar Δ MyJar.jar Δ MyCode Δ {Input/ Δ Output/ } 所在的執行目錄為 Hadoop_Home./MyJava = 編譯後程式碼目錄 Myjar.jar = 封裝後的編譯檔 先放些文件檔到 HDFS 上的 input 目錄./input;./ouput 不一定為 hdfs 的輸入 輸出目錄 4 4

2. Eclipse 之編譯與執行 5

程式設計 此篇介紹如何撰寫 HBase 程式常用的 HBase API 說明實做 I/O 操作搭配 Map Reduce 運算

HBase 程式設計 常用的 HBase API 說明

Table, Family Column, Qualifier Row, TimeStamp, Cell, Lock HTable 成員 8

HBase 常用函式 HBaseAdmin HBaseConfiguration HTable HTableDescriptor Put Get Scanner Database Table Family Column Qualifier 9

HBaseConfiguration Adds HBase configuration files to a Configuration = new HBaseConfiguration ( ) = new HBaseConfiguration (Configuration c) 繼承自 org.apache.hadoop.conf.configuration <property> <name> name </name> <value> value </value> </property> 回傳值 void void String String void void 函數 addresource clear get getboolean set setboolean 參數 (Path file) () (String name) (String name, boolean defaultvalue ) (String name, String value) (String name, boolean value) 10

HBaseAdmin HBase 的管理介面 = new HBaseAdmin( HBaseConfiguration conf ) Ex: HBaseAdmin admin = new HBaseAdmin(config); admin.disabletable ( tablename ); 回傳值 函數 參數 addcolumn (String tablename, HColumnDescriptor column) checkhbaseavailable (HBaseConfiguration conf) createtable (HTableDescriptor desc) void deletetable (byte[] tablename) deletecolumn (String tablename, String columnname) enabletable (byte[] tablename) disabletable (String tablename) HTableDescriptor[] listtables () void modifytable (byte[] tablename, HTableDescriptor htd) boolean tableexists (String tablename) 11

HTableDescriptor HTableDescriptor contains the name of an HTable, and its column families. = new HTableDescriptor() = new HTableDescriptor(String name) Constant-values Ex: org.apache.hadoop.hbase.htabledescriptor.table_descriptor_version HTableDescriptor htd = new HTableDescriptor(tablename); htd.addfamily ( new HColumnDescriptor ( Family )); 回傳值 void HColumnDescriptor byte[] byte[] void 函數 addfamily removefamily getname getvalue setvalue 參數 (HColumnDescriptor family) (byte[] column) ( ) = Table name (byte[] key) = 對應 key 的 value (String key, String value) 12

HColumnDescriptor An HColumnDescriptor contains information about a column family = new HColumnDescriptor(String familyname) Constant-values Ex: org.apache.hadoop.hbase.htabledescriptor.table_descriptor_version HTableDescriptor htd = new HTableDescriptor(tablename); HColumnDescriptor col = new HColumnDescriptor("content:"); htd.addfamily(col); 回傳值 byte[] byte[] void 函數 getname getvalue setvalue 參數 ( ) = Family name (byte[] key) = 對應 key 的 value (String key, String value) 13

HTable Used to communicate with a single HBase table. Ex: = new HTable(HBaseConfiguration conf, String tablename) HTable table = new HTable (conf, Bytes.toBytes ( tablename )); ResultScanner scanner = table.getscanner ( family ); 回傳值 void void boolean Result byte[][] ResultScanner HTableDescriptor byte[] static boolean void 函數 checkandput close exists get getendkeys getscanner gettabledescriptor gettablename istableenabled put 參數 (byte[] row, byte[] family, byte[] qualifier, byte[] value, Put put) () (Get get) (Get get) () (byte[] family) () () (HBaseConfiguration conf, String tablename) (Put put) 14

Put Used to perform Put operations for a single row. Ex: = new Put(byte[] row) = new Put(byte[] row, RowLock rowlock) HTable table = new HTable (conf, Bytes.toBytes ( tablename )); Put p = new Put ( brow ); p.add (family, qualifier, value); table.put ( p ); Put Put byte[] RowLock long boolean Put add add getrow getrowlock gettimestamp isempty settimestamp (byte[] family, byte[] qualifier, byte[] value) (byte[] column, long ts, byte[] value) () () () () (long timestamp) 15

Get Used to perform Get operations on a single row. Ex: = new Get (byte[] row) = new Get (byte[] row, RowLock rowlock) HTable table = new HTable(conf, Bytes.toBytes(tablename)); Get g = new Get(Bytes.toBytes(row)); Get Get Get Get TimeRange Get Get addcolumn addcolumn addcolumns addfamily gettimerange settimerange setfilter (byte[] column) (byte[] family, byte[] qualifier) (byte[][] columns) (byte[] family) () (long minstamp, long maxstamp) (Filter filter) 16

Scanner All operations are identical to Get Rather than specifying a single row, an optional startrow and stoprow may be defined. If rows are not specified, the Scanner will iterate over all rows. = new Scan () = new Scan (byte[] startrow, byte[] stoprow) = new Scan (byte[] startrow, Filter filter) Get Get Get Get TimeRange Get Get addcolumn addcolumn addcolumns addfamily gettimerange settimerange setfilter (byte[] column) (byte[] family, byte[] qualifier) (byte[][] columns) (byte[] family) () (long minstamp, long maxstamp) (Filter filter) 17

Result Single row result of a Get or Scan query. Ex: = new Result() HTable table = new HTable(conf, Bytes.toBytes(tablename)); Get g = new Get(Bytes.toBytes(row)); Result rowresult = table.get(g); Bytes[] ret = rowresult.getvalue( (family + ":"+ column ) ); boolean NavigableMap <byte[],byte[]> byte[] byte[] int containscolumn getfamilymap getvalue getvalue Size (byte[] family, byte[] qualifier) (byte[] family) (byte[] column) (byte[] family, byte[] qualifier) () 18

Interface ResultScanner Interface for client-side scanning. Go to HTable to obtain instances. Ex: HTable.getScanner (Bytes.toBytes(family)); ResultScanner scanner = table.getscanner (Bytes.toBytes(family)); for (Result rowresult : scanner) { Bytes[] str = rowresult.getvalue ( family, column ); } void Result close next () () 19

HBase Key/Value 的格式 org.apache.hadoop.hbase.keyvalue getrow(), getfamily(), getqualifier(), gettimestamp(), and getvalue(). The KeyValue blob format inside the byte array is: <keylength> <valuelength> <key> <value> Key 的格式 : < rowlength > < row> < columnfamilylength > < columnfamily > < columnqualifier > < timestamp > < keytype > Rowlength 最大值為 Short.MAX_SIZE, column family length 最大值為 Byte.MAX_SIZE, column qualifier + key length 必須小於 Integer.MAX_SIZE. 20

HBase 程式設計 實做 I/O 操作

範例一 : 新增 Table < 指令 > 22

範例一 : 新增 Table public static void createhbasetable ( String tablename ) throws IOException { HTableDescriptor htd = new HTableDescriptor(tablename); HColumnDescriptor col = new HColumnDescriptor("content:"); htd.addfamily(col); HBaseConfiguration config = new HBaseConfiguration(); HBaseAdmin admin = new HBaseAdmin(config); if(admin.tableexists(tablename)) { admin.disabletable(tablename); admin.deletetable(tablename); } admin.createtable(htd); } < 程式碼 > 23

範例二 :Put: 資料進 Column < 指令 > 24

範例二 : Put 資料進 Column static public void putdata(string tablename, String row, String family, String column, String value) throws IOException { HBaseConfiguration config = new HBaseConfiguration(); HTable table = new HTable(config, tablename); byte[] brow = Bytes.toBytes(row); byte[] bfamily = Bytes.toBytes(family); byte[] bcolumn = Bytes.toBytes(column); byte[] bvalue = Bytes.toBytes(value); Put p = new Put(brow); p.add(bfamily, bcolumn, bvalue); table.put(p); table.close(); } < 程式碼 > 25

範例三 : Get Column Value < 指令 > 26

範例三 : Get Column Value < 程式碼 > static String getcolumn ( String tablename, String row, String family, String column ) { HBaseConfiguration conf = new HBaseConfiguration(); String ret = ""; HTable table; try { table = new HTable(conf, Bytes.toBytes(tablename)); Get g = new Get(Bytes.toBytes(row)); Result rowresult = table.get(g); ret = Bytes.toString(rowResult.getValue(Bytes.toBytes(family + : + column))); table.close(); } catch (IOException e) { e.printstacktrace(); } return ret; } 27

範例四 : Scan all Column < 指令 > 28

範例四 :Scan: all Column static void ScanColumn(String tablename, String family, String column) { HBaseConfiguration conf = new HBaseConfiguration(); HTable table; try { table = new HTable(conf, Bytes.toBytes(tablename)); ResultScanner scanner = table.getscanner(bytes.tobytes(family)); int i = 1; for (Result rowresult : scanner) { byte[] by = rowresult.getvalue( Bytes.toBytes(family), Bytes.toBytes(column) ); String str = Bytes.toString ( by ); System.out.println("row " + i + " is \"" + str +"\""); i++; } } catch (IOException e) { e.printstacktrace(); } } < 程式碼 > 29

範例五 : 刪除資料表 < 指令 > 30

範例五 : 刪除資料表 static void drop ( String tablename ) { HBaseConfiguration conf = new HBaseConfiguration(); try { HBaseAdmin admin = new HBaseAdmin(conf); if (admin.tableexists(tablename)) { admin.disabletable(tablename); admin.deletetable(tablename); System.out.println("Droped the table [" + tablename+ "]"); }else{ System.out.println("Table [" + tablename+ "] was not found!"); } } catch (IOException e) { e.printstacktrace(); } } < 程式碼 > 31

HBase 程式設計 MapReduce 與 HBase 的搭配

範例六 :WordCountHBase: 程式說明 33

範例六 :WordCountHBase: public class WordCountHBase { public static class Map extends Mapper<LongWritable,Text,Text, IntWritable> { private IntWritable i = new IntWritable(1); public void map(longwritable key,text value,context context) throws IOException, InterruptedException { String s[] = value.tostring().trim().split(" "); for( String m : s) { context.write(new Text(m), i); } } } public static class Reduce extends TableReducer<Text, IntWritable, NullWritable> { public void reduce(text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for(intwritable i : values) { sum += i.get(); } Put put = new Put(Bytes.toBytes(key.toString())); put.add(bytes.tobytes("content"), Bytes.toBytes("count"), Bytes.toBytes(String.valueOf(sum))); context.write(nullwritable.get(), put); } } <1> 34

範例六 :WordCountHBase: public static void createhbasetable(string tablename)throws IOException { HTableDescriptor htd = new HTableDescriptor(tablename); HColumnDescriptor col = new HColumnDescriptor("content:"); htd.addfamily(col); HBaseConfiguration config = new HBaseConfiguration(); HBaseAdmin admin = new HBaseAdmin(config); if(admin.tableexists(tablename)) { admin.disabletable(tablename); admin.deletetable(tablename); } System.out.println("create new table: " + tablename); admin.createtable(htd); } } { } public static void main(string args[]) throws Exception String tablename = "wordcount"; Configuration conf = new Configuration(); conf.set(tableoutputformat.output_table, tablename); createhbasetable(tablename); String input = args[0]; Job job = new Job(conf, "WordCount table with " + input); job.setjarbyclass(wordcounthbase.class); job.setnumreducetasks(3); job.setmapperclass(map.class); job.setreducerclass(reduce.class); job.setmapoutputkeyclass(text.class); job.setmapoutputvalueclass(intwritable.class); job.setinputformatclass(textinputformat.class); job.setoutputformatclass(tableoutputformat.class); FileInputFormat.addInputPath(job, new Path(input)); System.exit(job.waitForCompletion(true)?0:1); <2> 35

範例六 : 執行結果 36

範例七 :LoadHBaseMapper: 說明 : 此程式碼將 HBase 的資料取出來, 再將結果塞回 hdfs 上運算方法 : 將此程式運作在 hadoop 0.20 平台上, 用 ( 參考 2) 的方法加入 hbase 參數後, 將此程式碼打包成 XX.jar 執行 : --------------------------- hadoop jar XX.jar LoadHBaseMapper <hdfs_output> --------------------------- 結果 : $ hadoop fs -cat <hdfs_output>/part-r-00000 --------------------------- 54 30 31 GunLong 54 30 32 Esing 54 30 33 SunDon 54 30 34 StarBucks --------------------------- 注意 : 1. 請注意 hbase 上必須要有 table, 並且已經有資料 2. 運算完後, 程式將執行結果放在你指定 hdfs 的 <hdfs_output> 內請注意沒有 <hdfs_output> 資料夾 37

範例七 :LoadHBaseMapper: public class LoadHBaseMapper { public static class HtMap extends TableMapper<Text, Text> { public void map(immutablebyteswritable key, Result value, Context context) throws IOException, InterruptedException { String res = Bytes.toString(value.getValue( Bytes.toBytes("Detail"), }} Bytes.toBytes("Name"))); context.write(new Text(key.toString()), new Text(res)); public static class HtReduce extends Reducer<Text, Text, Text, Text> { public void reduce(text key, Iterable<Text> values, Context context) }} throws IOException, InterruptedException { String str = new String(""); Text final_key = new Text(key); Text final_value = new Text(); for (Text tmp : values) { str += tmp.tostring(); } final_value.set(str); context.write(final_key, final_value); <1> 38

範例七 : LoadHBaseMapper public static void main(string args[]) throws Exception { String input = args[0]; String tablename = "tsmc"; Configuration conf = new Configuration(); Job job = new Job(conf, tablename + " hbase data to hdfs"); job.setjarbyclass(loadhbasemapper.class); TableMapReduceUtil.initTableMapperJob (tablename, myscan, HtMap.class,Text.class, Text.class, job); job.setmapperclass(htmap.class); job.setreducerclass(htreduce.class); job.setmapoutputkeyclass(text.class); job.setmapoutputvalueclass(text.class); job.setinputformatclass(tableinputformat.clas s); job.setoutputformatclass(textoutputformat.c lass); job.setoutputkeyclass(text.class); job.setoutputvalueclass(text.class); FileOutputFormat.setOutputPath(job, new Path(input)); System.exit(job.waitForCompletion(true)? 0 : 1); } } <2> 39

範例七 : 執行結果 40

其他用法補充 HBase 內 contrib 的項目, 如 Trancational Thrift

1. Transactional HBase Indexed Table = Secondary Index = Transactional HBase 內容與原本 table 相似的另一張 table, 但 key 不同, 利於排列內容 Primary Table Indexed Table name price description name price description 1 apple 10 xx 2 orig 5 ooo 2 3 orig banana 5 15 ooo vvvv 4 1 tomato apple 8 10 uu xx 4 tomato 8 uu 3 banana 15 vvvv 42

1. 環境設定 需在 $HBASE_INSTALL_DIR/conf/hbase-site.xml 檔內增加兩項內容 <property> <name>hbase.regionserver.class</name> <value>org.apache.hadoop.hbase.ipc.indexedregioninterface</value> </property> <property> <name>hbase.regionserver.impl</name> <value> org.apache.hadoop.hbase.regionserver.tableindexed.indexedregionserver </value> </property> 43

1.a Ex : 從一個原有的 Table 增加 IndexedTable public void addsecondaryindextoexistingtable(string TableName, String IndexID, String IndexColumn) throws IOException { HBaseConfiguration conf = new HBaseConfiguration(); conf.addresource(new Path("/opt/hbase/conf/hbasesite.xml")); IndexedTableAdmin admin = null; admin = new IndexedTableAdmin(conf); admin.addindex(bytes.tobytes(tablename), new IndexSpecification( IndexID, Bytes.toBytes(IndexColumn))); }} 44

1.b Ex : 建立一個新的 Table 附帶 IndexedTable public void createtablewithsecondaryindexes(string TableName, String IndexColumn) throws IOException { HBaseConfiguration conf = new HBaseConfiguration(); conf.addresource(new Path("/opt/hbase/conf/hbase-site.xml")); HTableDescriptor desc = new HTableDescriptor(TableName); desc.addfamily(new HColumnDescriptor( Family1")); IndexedTableDescriptor Idxdesc = new IndexedTableDescriptor(desc); Idxdesc.addIndex(new IndexSpecification(IndexColumn, Bytes.toBytes(" Family1 :" + IndexColumn))); IndexedTableAdmin admin = new IndexedTableAdmin(conf); admin.createindexedtable(idxdesc); } 45

2. Thrift 46

其他專案 王耀聰陳威宇 Jazz@nchc.org.tw waue@nchc.org.tw

PIG 48

Hive 49

a Conclusions

Questions and Thanks