HBase MapReduce

1. HBase to HBase

Mapper 繼承 TableMapper,輸入為Rowkey和Result.

public abstract class TableMapper<KEYOUT, VALUEOUT> extends Mapper<ImmutableBytesWritable, Result, KEYOUT, VALUEOUT> {
public TableMapper() {
}
}
package com.scb.jason.mapper;import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;/*** Created by Administrator on 2017/8/16.*/
public class User2BasicMapper extends TableMapper<ImmutableBytesWritable, Put> {private ImmutableBytesWritable mapOutputkey = new ImmutableBytesWritable();@Overridepublic void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {//Get rowKey
        mapOutputkey.set(key.get());Put put = new Put(key.get());for(Cell cell:value.rawCells()){if("info".equals(Bytes.toString(CellUtil.cloneFamily(cell)))){if("name".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))){put.add(cell);}if("age".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))){put.add(cell);}}}context.write(mapOutputkey,put);}}

Reducer 繼承 TableReducer

public abstract class TableReducer<KEYIN, VALUEIN, KEYOUT> extends Reducer<KEYIN, VALUEIN, KEYOUT, Mutation> {
public TableReducer() {
}
}
package com.scb.jason.reducer;import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;/*** Created by Administrator on 2017/8/16.*/
public class User2BasicReducer extends TableReducer<ImmutableBytesWritable, Put, ImmutableBytesWritable>{@Overrideprotected void reduce(ImmutableBytesWritable key, Iterable<Put> values, Context context) throws IOException, InterruptedException {for(Put put:values){context.write(null,put);}}
}

Driver

package com.scb.jason.driver;import com.scb.jason.mapper.User2BasicMapper;
import com.scb.jason.reducer.User2BasicReducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;import java.io.IOException;/*** Created by Administrator on 2017/8/16.*/
public class User2BasicDriver extends Configured implements Tool{public int run(String[] strings) throws Exception {Job job = Job.getInstance(this.getConf(),this.getClass().getSimpleName());job.setJarByClass(this.getClass());Scan scan = new Scan();scan.setCaching(500);        // 1 is the default in Scan, which will be bad for MapReduce jobsscan.setCacheBlocks(false);  // don't set to true for MR jobs// set other scan attrs
TableMapReduceUtil.initTableMapperJob("user",      // input tablescan,             // Scan instance to control CF and attribute selectionUser2BasicMapper.class,   // mapper classImmutableBytesWritable.class,             // mapper output keyPut.class,             // mapper output value
                job);TableMapReduceUtil.initTableReducerJob("basic",      // output tableUser2BasicReducer.class,             // reducer class
                job);job.setNumReduceTasks(1);boolean isSuccess = job.waitForCompletion(true);return isSuccess?1:0;}public static void main(String[] args) throws Exception {Configuration configuration = HBaseConfiguration.create();int status = ToolRunner.run(configuration,new User2BasicDriver(),args);System.exit(status);}}

?2. HBase to File

Mapper

package com.scb.jason.mapper;import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.Text;import java.io.IOException;/*** Created by Administrator on 2017/8/16.*/
public class User2FileMapper extends TableMapper<Text, Text> {private Text rowKeyText = new Text();private Text valueText = new Text();@Overridepublic void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {//Get rowKey
        rowKeyText.set(key.get());Put put = new Put(key.get());byte[] inforName = null;byte[] inforAge = null;for(Cell cell:value.rawCells()){if("info".equals(Bytes.toString(CellUtil.cloneFamily(cell)))){if("name".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))){inforName = CellUtil.cloneValue(cell);}if("age".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))){inforAge = CellUtil.cloneValue(cell);}}}valueText.set(new String(inforName)+"\t"+new String(inforAge));context.write(rowKeyText,valueText);}}

No Reducer Reducer

Driver

package com.scb.jason.driver;import com.scb.jason.mapper.User2BasicMapper;
import com.scb.jason.mapper.User2FileMapper;
import com.scb.jason.reducer.User2BasicReducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;/*** Created by Administrator on 2017/8/16.*/
public class User2FileDriver extends Configured implements Tool{public int run(String[] args) throws Exception {Job job = Job.getInstance(this.getConf(),this.getClass().getSimpleName());job.setJarByClass(this.getClass());Scan scan = new Scan();scan.setCaching(500);        // 1 is the default in Scan, which will be bad for MapReduce jobsscan.setCacheBlocks(false);  // don't set to true for MR jobs// set other scan attrs
TableMapReduceUtil.initTableMapperJob("user",      // input tablescan,             // Scan instance to control CF and attribute selectionUser2FileMapper.class,   // mapper classText.class,             // mapper output keyText.class,             // mapper output value
                job);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(Text.class);job.setOutputFormatClass(TextOutputFormat.class);FileOutputFormat.setOutputPath(job,new Path(args[0]));job.setNumReduceTasks(1);boolean isSuccess = job.waitForCompletion(true);return isSuccess?1:0;}public static void main(String[] args) throws Exception {Configuration configuration = HBaseConfiguration.create();int status = ToolRunner.run(configuration,new User2FileDriver(),args);System.exit(status);}}

3. File to HBase

Driver

package com.scb.jason.driver;import com.scb.jason.mapper.File2HbaseMapper;
import com.scb.jason.mapper.User2BasicMapper;
import com.scb.jason.reducer.File2HBaseReducer;
import com.scb.jason.reducer.User2BasicReducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;/*** Created by Administrator on 2017/8/16.*/
public class File2BasicDriver extends Configured implements Tool{public int run(String[] strings) throws Exception {Job job = Job.getInstance(this.getConf(),this.getClass().getSimpleName());job.setJarByClass(this.getClass());job.setMapperClass(File2HbaseMapper.class);FileInputFormat.addInputPath(job,new Path("F:\\Workspace\\File"));job.setMapOutputKeyClass(ImmutableBytesWritable.class);job.setMapOutputValueClass(Put.class);TableMapReduceUtil.initTableReducerJob("basic",      // output tableFile2HBaseReducer.class,             // reducer class
                job);job.setNumReduceTasks(1);boolean isSuccess = job.waitForCompletion(true);return isSuccess?1:0;}public static void main(String[] args) throws Exception {Configuration configuration = HBaseConfiguration.create();int status = ToolRunner.run(configuration,new File2BasicDriver(),args);System.exit(status);}}

Mapper

package com.scb.jason.mapper;import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;
import java.util.StringTokenizer;/*** Created by Administrator on 2017/8/17.*/
public class File2HbaseMapper extends Mapper<LongWritable,Text,ImmutableBytesWritable,Put> {private ImmutableBytesWritable mapOutputkey = new ImmutableBytesWritable();@Overridepublic void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {String lineValue = value.toString();StringTokenizer stringTokenizer = new StringTokenizer(lineValue);String rowkey = stringTokenizer.nextToken();String name = stringTokenizer.nextToken();String age = stringTokenizer.nextToken();Put put = new Put(Bytes.toBytes(rowkey));put.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes(name));put.add(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes(age));mapOutputkey.set(Bytes.toBytes(key.get()));context.write(mapOutputkey,put);}}

Reducer

package com.scb.jason.reducer;import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;import java.io.IOException;/*** Created by Administrator on 2017/8/25.*/
public class File2HBaseReducer extends TableReducer<ImmutableBytesWritable, Put, ImmutableBytesWritable> {@Overrideprotected void reduce(ImmutableBytesWritable key, Iterable<Put> values, Context context) throws IOException, InterruptedException {for(Put put:values){context.write(null,put);}}}

?4. HBase to RDBMS

public static class MyRdbmsReducer extends Reducer<Text, IntWritable, Text, IntWritable>  {private Connection c = null;public void setup(Context context) {// create DB connection...
  }public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {// do summarization// in this example the keys are Text, but this is just an example
  }public void cleanup(Context context) {// close db connection
  }}

5. File -> HFile -> ?HBase 批量導入

http://www.cnblogs.com/shitouer/archive/2013/02/20/hbase-hfile-bulk-load.html

?

轉載于:https://www.cnblogs.com/xdlaoliu/p/7406789.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/371395.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/371395.shtml
英文地址,請注明出處:http://en.pswp.cn/news/371395.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

第六期的知識點

1.volatile詳解 在應用程序中&#xff0c;volatile主要是被設計用來修飾被不同線程訪問和修改的變量 .volatile的本意是“易變的” 因為訪問寄存器要比訪問內存單元快的多,所以編譯器一般都會作減少存取內存的優化&#xff0c;但有可能會讀臟數據。當要求使用volatile聲明變量值…

在DelayQueue中更改延遲,從而更改順序

因此&#xff0c;我正在研究構建一個簡單的對象緩存&#xff0c;該緩存在給定時間后會使對象過期。 顯而易見的機制是使用Java并發包中的DelayedQueue類。 但我想知道是否有可能在將對象添加到隊列后更新延遲。 看一下Delayed接口&#xff0c;似乎沒有充分的理由不在文檔中&…

vi編輯器服務器維護,vi編輯器有哪幾種工作模式及如何轉換_網站服務器運行維護,vi編輯器,工作模式...

整理分享一些 Linux思維導圖(值得收藏)_網站服務器運行維護本篇文章整理分享了一些 Linux思維導圖(值得收藏)。有一定的參考價值&#xff0c;有需要的朋友可以參考一下&#xff0c;希望對大家有所幫助。vi編輯器有三種基本的工作模式&#xff0c;分別是&#xff1a;指令行模式、…

(八)cmockery中的calculator和run_tests函數的注釋代碼

所分析的calculator.c和calculator_test.c文件位于 工程中的 cmockery/src/example/ 目錄下&#xff0c;是一個相對而言比較全面的樣例程序&#xff0c;用到了cmockery項目中的大多數單元測試方法。基本上涵蓋了之前所有的樣例程序中的用法&#xff0c;還有兩組測試是database操…

家用雙wan口路由器推薦_請推薦雙WAN口的有線千兆硬路由器?

利益相關&#xff1a;TP-LINK一線銷售人員(來看看會不會有推薦我司產品的2333 )路由器&#xff1a;TL-ER3220G&#xff0c;帶機量300終端&#xff0c;可管理50個AP&#xff0c;最大支持四條寬帶接入POE交換機&#xff1a;TL-SF1005P(5口百兆) TL-SG1005P(5口千兆) TL-SF1009PH(…

第一章魔獸窗口

開始顯示第一個窗體 用戶直接點登陸的話就會提示用戶名不能為空密碼不能為空 沒有賬號的話只能先注冊&#xff0c;點擊藍色摁鈕進入下一個窗體 這里有判斷是否為空&#xff0c;注冊成功后利用窗體傳值&#xff0c;并且打開第一個窗口 把注冊的用戶名和密碼寫上去就可以的登陸到…

Apache Digester示例–輕松配置

解決問題–硬編碼&#xff0c;需要為您的應用程序創建自定義配置&#xff0c;例如struts配置文件&#xff0c;以僅通過更改文件來改變應用程序行為。 Apache Digester可以輕松為您完成此任務。 使用Apache Digester相當容易將XML文檔轉換為相應的Java bean對象層次結構。 請參閱…

騰訊云搭svn服務器,騰訊云使用筆記二: 安裝svn服務器及web同步

A01&#xff1a;安裝subversionsudo apt-get install subversionA02:創建倉庫很多目錄可以放subversion文件倉庫&#xff0c;最常見的是/usr/local/svn和/home/svnsudo mkdir -p /home/svn/youshengyousesudo svnadmin create /home/svn/youshengyouse//說明&#xff1a;先創建…

python將圖像轉換為8位單通道_使用Python將圖片轉換為單通道黑白圖片

本文介紹如何使用python將圖片轉換為純黑白的單通道圖片。文中用到的腳本支持彩色、灰度、帶alpha通道的輸入圖片以及SVG矢量圖&#xff0c;支持調整輸出圖片大小以及設置灰度閾值。最后介紹如何輸出SSD1306 OLED顯示屏可用的XBM文件&#xff0c;并利用輸出的XBM數據在0.96寸的…

Java FlameGraph 火焰圖

上周一個偶然的機會聽同事提到了Java FlameGraph&#xff0c;剛實驗了一下&#xff0c;效果非常好。 一、什么是FlameGraph 直接看圖說話。FlameGraph 是 SVG格式&#xff0c;矢量圖&#xff0c;可以隨意擴大縮小&#xff0c;看不清的信息可以放大看。圖中&#xff0c;各種紅橙…

ADB 常用命令

獲取Android設備號 adb shell getprop ro.serialno 獲取系統版本 adb shell getprop ro.build.version.release>4.2.2 獲取系統api版本 adb shell getprop ro.build.version.sdk>17 獲取設備分辨率&#xff08;SDK4.3&#xff09; adb shell wm size獲取設備屏幕密度&am…

哪個Java線程消耗了我的CPU?

當您的Java應用程序占用100&#xff05;的CPU時&#xff0c;您該怎么辦&#xff1f; 事實證明&#xff0c;您可以使用內置的UNIX和JDK工具輕松找到有問題的線程。 不需要探查器或代理。 為了進行測試&#xff0c;我們將使用以下簡單程序&#xff1a; public class Main {publi…

煙草局計算機筆試,2020年廣西南寧煙草局什么時候筆試?

最近廣西煙草局各地市社招通知頻發&#xff0c;南寧煙草局報名截止至今都無任何消息&#xff0c;根據往年的考情&#xff0c;通知近期很大可能會發布&#xff0c;將于6月底完成筆面!你備考好了嗎&#xff1f;今天廣西中公國企小編來給大家說一下南寧煙草局社招的筆試內容及備考…

JAVA Swing 組件演示***

下面是Swing組件的演示&#xff1a; package a_swing;import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Cursor; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.…

Spring 3.1緩存和@CacheEvict

我的上一個博客演示了Spring 3.1的Cacheable批注的應用&#xff0c; Cacheable批注用于標記返回值將存儲在緩存中的方法。 但是&#xff0c; Cacheable只是Spring的Guy為緩存而設計的一對注釋??中的一個&#xff0c;另一個是CacheEvict 。 像Cacheable一樣&#xff0c; Cache…

centos 獲取硬件序列號_如何在 Linux 上查找硬件規格

在 Linux 系統上有許多工具可用于查找硬件規格。-- Sk&#xff08;作者&#xff09;在 Linux 系統上有許多工具可用于查找硬件規格。在這里&#xff0c;我列出了四種最常用的工具&#xff0c;可以獲取 Linux 系統的幾乎所有硬件&#xff08;和軟件&#xff09;細節。好在是這些…

位置服務器管理器,查看 DIMM 位置

鍵入&#xff1a;-> show /System/Memory/DIMMs -t locationTarget | Property | Value-----------------------------------------------------------------------/System/Memory/DIMMs/ | location | CMIOU0/CM/CMP/BOB00/CH0/DIMM (CPU MemoryDIMM_0 | | IO Unit 0 Memor…

Spring –持久層–編寫實體并配置Hibernate

歡迎來到本教程的第二部分。 當您看到本文有多長時間時&#xff0c;請不要驚慌–我向您保證&#xff0c;這主要是簡單的POJO和一些生成的代碼。 在開始之前&#xff0c;我們需要更新我們的Maven依賴項&#xff0c;因為我們現在將使用Hibernate和Spring。 將以下依賴項添加到pom…

無線服務器主機名是,wifi默認服務器主機名

wifi默認服務器主機名 內容精選換一換以CentOS 7操作系統的彈性云服務器為例&#xff1a;登錄Linux彈性云服務器&#xff0c;查看“cloud-init”的配置文件。檢查“/etc/cloud/cloud.cfg”文件中“update_hostname”是否被注釋或者刪除。如果沒有被注釋或者刪除&#xff0c;則需…

pygame里面物體閃爍運動_利用自閃爍發光二極管探究小車在傾斜軌道上的運動規律...

2020年11月23日&#xff0c;周一&#xff0c;24小時安全值班。利用當班中午的時間&#xff0c;微主在創客空間測試了自閃爍發光二極管在勻加速運動中的效果&#xff0c;結果還比較滿意。將小車放置在傾斜的軌道上&#xff0c;將自閃爍發光二極管和紐扣電池構成頻閃光源&#xf…