用mapreduce读取hdfs数据到hbase上
hdfs数据到hbase过程
将HDFS上的文件中的数据导入到hbase中
实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具
hbase先创建好表 create 'TB','info'
下面是实现代码:
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import java.io.IOException;
/**
* 用于HDFS的数据读取,写入到hbase中,
* hbase里预先创建好表:create 'NNTB','info'
* */
public class HdfsToHBase {
public static void main(String[] args) throws Exception{
System.setProperty("hadoop.home.dir", "D:\\hadoop-2.7.6");//这行我是本地运行所需指定的hadoop home
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "202.168.27.196:2181");//ip乱写的,端口默认2181
conf.set(TableOutputFormat.OUTPUT_TABLE, "NNTB");
Job job = Job.getInstance(conf, HdfsToHBase.class.getSimpleName());
TableMapReduceUtil.addDependencyJars(job);
job.setJarByClass(HdfsToHBase.class); job.setMapperClass(HdfsToHBaseMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setReducerClass(HdfsToHBaseReducer.class); FileInputFormat.addInputPath(job, new Path("hdfs://202.168.27.196:9000/user/hadoop/gznt/gznt_bmda/*"));
job.setOutputFormatClass(TableOutputFormat.class);
job.waitForCompletion(true);
} public static class HdfsToHBaseMapper extends Mapper<LongWritable, Text, Text, Text> {
private Text outKey = new Text();
private Text outValue = new Text();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] splits = value.toString().split("\t");
outKey.set(splits[0]);
outValue.set(splits[1]+"\t"+splits[2]+"\t"+splits[3]+"\t"+splits[4]);
context.write(outKey, outValue);
}
}
//::: create 'NNTB','info'
public static class HdfsToHBaseReducer extends TableReducer<Text, Text, NullWritable> {
@Override
protected void reduce(Text k2, Iterable<Text> v2s, Context context) throws IOException, InterruptedException {
Put put = new Put(k2.getBytes());
for (Text v2 : v2s) {
String[] splis = v2.toString().split("\t");
//info,对应hbase列族名
if(splis[0]!=null && !"NULL".equals(splis[0])){
put.addColumn("info".getBytes(), "NodeCode".getBytes(),splis[0].getBytes());
}
if(splis[1]!=null && !"NULL".equals(splis[1])){
put.addColumn("info".getBytes(), "NodeType".getBytes(),splis[1].getBytes());
}
if(splis[2]!=null && !"NULL".equals(splis[2])){
put.addColumn("info".getBytes(), "NodeName".getBytes(),splis[2].getBytes());
}
if(splis[3]!=null && !"NULL".equals(splis[3])){
put.addColumn("info".getBytes(), "IsWarehouse".getBytes(),splis[3].getBytes());
}
}
context.write(NullWritable.get(),put);
}
}
}
用mapreduce读取hdfs数据到hbase上的更多相关文章
- bulk-load 装载HDFS数据到HBase
bulk-load的作用是用mapreduce的方式将hdfs上的文件装载到hbase中,对于海量数据装载入hbase非常有用,参考http://hbase.apache.org/docs/r0.89 ...
- hdfs数据到hbase过程
需求:将HDFS上的文件中的数据导入到hbase中 实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具 一.hdfs中的数据是这样的 hbase创建好表 cre ...
- 使用MapReduce将HDFS数据导入Mysql
使用MapReduce将Mysql数据导入HDFS代码链接 将HDFS数据导入Mysql,代码示例 package com.zhen.mysqlToHDFS; import java.io.DataI ...
- MapReduce读取hdfs上文件,建立词频的倒排索引到Hbase
Hdfs上的数据文件为T0,T1,T2(无后缀): T0: What has come into being in him was life, and the life was the light o ...
- 使用MapReduce将HDFS数据导入到HBase(二)
package com.bank.service; import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf. ...
- 使用MapReduce将HDFS数据导入到HBase(一)
package com.bank.service; import java.io.IOException; import org.apache.hadoop.conf.Configuration;im ...
- 使用MapReduce将HDFS数据导入到HBase(三)
使用MapReduce生成HFile文件,通过BulkLoader方式(跳过WAL验证)批量加载到HBase表中 package com.mengyao.bigdata.hbase; import j ...
- spark读取hdfs数据本地性异常
在分布式计算中,为了提高计算速度,数据本地性是其中重要的一环. 不过有时候它同样也会带来一些问题. 一.问题描述 在分布式计算中,大多数情况下要做到移动计算而非移动数据,所以数据本地性尤其重要,因此我 ...
- spark读取hdfs数据本地性异常【转】
在分布式计算中,为了提高计算速度,数据本地性是其中重要的一环. 不过有时候它同样也会带来一些问题. 一.问题描述 在分布式计算中,大多数情况下要做到移动计算而非移动数据,所以数据本地性尤其重要,因此我 ...
随机推荐
- 用trie树实现输入提示功能,输入php函数名,提示php函数
参照刘汝佳的trie树 结构体 #include "stdio.h" #include "stdlib.h" #include "string.h&q ...
- 用unoreder_map实现词频统计
博客写在CSDN了,google了一下移植真的巨麻烦.... 这里贴个网址算了.... https://blog.csdn.net/z1991998920/article/details/796891 ...
- [转]查找问题的利器 - Git Bisect
转自:http://gitbook.liuhui998.com/5_4.html 假设你在项目的'2.6.18'版上面工作, 但是你当前的代码(master)崩溃(crash)了. 有时解决这种问题的 ...
- python:类与对象命名空间、面对对象的组合用法
1,类里可以定义两种属性: #静态属性 #静态属性就是直接在类中定义的变量 #动态属性 #动态属性就是定义在类中的方法 class Course: language = ['Chinese']#静态属 ...
- 商城管理系统项目(前台+后台+管理员+用户+html+jsp)
管理员后台 用户前台 如果下载项目报错,加包即可(包已经打包放在下载地址) 数据库:mysql drop database shoppingmall; create database shopping ...
- 【洛谷P3807】(模板)卢卡斯定理
卢卡斯定理 把n写成p进制a[n]a[n-1][n-2]…a[0],把m写成p进制b[n]b[n-1][n-2]…b[0],则C(n,m)与C(a[n],b[n])*C(a[n-1],b[n-1])* ...
- 【转载】Atom 是一款各方面体验都很像 Sublime Text 的编辑器
转载:http://www.appinn.com/atom-editor/ Atom 是一款各方面体验都很像 Sublime Text 的编辑器,它由 Github 出品,目前免费. Atom 功能的 ...
- nodejs如果npm install express -g远程安装费时
npm install -g cnpm --registry= https://registry.npm.taobao.org
- vue进阶语法及生命周期函数
1.calss和style绑定 操作元素的 class 列表和内联样式style是数据绑定的一个常见需求,它两都是属性,所以可以通过v-bind来绑定 1.1绑定HTML class 可以给v-bin ...
- JavaScript自己整理的基础-01
1.JavaScript 简介: JavaScript是互联网上最流行的脚本语言,所有现代的HTML都使用JavaScript.既然是脚本语言,那么它的特点就有一下三种: (1)弱类型: (2)解释型 ...