把hdfs数据写入到hbase表
功能:把hdfs上的数据写入到hbase表。
hadoop的mapreduce输出要导入到hbase表,最好先输出HFile格式,再导入hbase,因为HFile是hbase的内部存储格式,所以导入效率很高,下面我们来看一下具体怎么做。
1、我们在hdfs上有一个文本文件:

2、在hbase表里我们创建一个t1表
创建语句:create 't1','cf'
3、写MR作业
package cn.tendency.wenzhouhbase.hadoop; import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Mutation;
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.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; public class Hadoop2Hbase { @SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.1.124,192.168.1.125,192.168.1.126");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.master.port", "60000");
conf.set("hbase.rootdir", "hdfs://192.168.1.122:9000/hbase");
conf.set(TableOutputFormat.OUTPUT_TABLE, "t1"); Job job = new Job(conf, Hadoop2Hbase.class.getSimpleName());
TableMapReduceUtil.addDependencyJars(job);
job.setJarByClass(Hadoop2Hbase.class); job.setMapperClass(HbaseMapper.class);
job.setReducerClass(HbaseReducer.class); job.setMapOutputKeyClass(LongWritable.class);
job.setMapOutputValueClass(Text.class); job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TableOutputFormat.class); FileInputFormat.setInputPaths(job, "hdfs://192.168.1.123:9000/mytest/*");
job.waitForCompletion(true);
} static class HbaseMapper extends
Mapper<LongWritable, Text, LongWritable, Text> {
@Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, LongWritable, Text>.Context context)
throws IOException, InterruptedException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String[] split = value.toString().split("\t");
context.write(
key,
new Text(split[0]+sdf.format(Calendar.getInstance().getTime())
+ "\t" + value.toString()));
}
} static class HbaseReducer extends
TableReducer<LongWritable, Text, NullWritable> {
@Override
protected void reduce(
LongWritable key,
Iterable<Text> values,
Reducer<LongWritable, Text, NullWritable, Mutation>.Context context)
throws IOException, InterruptedException {
for (Text text : values) {
String[] split = text.toString().split("\t");
Put put = new Put(split[0].getBytes());
put.addColumn("cf".getBytes(), "oneColumn".getBytes(), text
.toString().getBytes());
put.addColumn("cf".getBytes(), "id".getBytes(),
split[1].getBytes());
put.addColumn("cf".getBytes(), "name".getBytes(),
split[2].getBytes());
put.addColumn("cf".getBytes(), "age".getBytes(),
split[3].getBytes());
// put.addColumn("cf".getBytes(), "addr".getBytes(),
// split[4].getBytes());
context.write(NullWritable.get(), put);
}
}
}
}
把hdfs数据写入到hbase表的更多相关文章
- hbase使用MapReduce操作4(实现将 HDFS 中的数据写入到 HBase 表中)
实现将 HDFS 中的数据写入到 HBase 表中 Runner类 package com.yjsj.hbase_mr2; import com.yjsj.hbase_mr2.ReadFruitFro ...
- Flink 使用(一)——从kafka中读取数据写入到HBASE中
1.前言 本文是在<如何计算实时热门商品>[1]一文上做的扩展,仅在功能上验证了利用Flink消费Kafka数据,把处理后的数据写入到HBase的流程,其具体性能未做调优.此外,文中并未就 ...
- 使用spark将内存中的数据写入到hive表中
使用spark将内存中的数据写入到hive表中 hive-site.xml <?xml version="1.0" encoding="UTF-8" st ...
- 将从数据库中获取的数据写入到Excel表中
pom.xml文件写入代码,maven自动加载poi-3.1-beta2.jar <!-- https://mvnrepository.com/artifact/poi/poi --> & ...
- 使用MapReduce将HDFS数据导入到HBase(三)
使用MapReduce生成HFile文件,通过BulkLoader方式(跳过WAL验证)批量加载到HBase表中 package com.mengyao.bigdata.hbase; import j ...
- Mapreduce读取Hbase表,写数据到一个Hbase表中
public class LabelJob { public static void main(String[] args) throws Exception { Job job = Job.getI ...
- 使用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 ...
- Mysql把一个表的数据写入另一个表中
一.表结构一样 insert into 表1 select * from 表2 二. 表结构不一样或者取部分列 insert into 表1 (列名1,列名2,列名3) select 列1,列2,列3 ...
随机推荐
- SpringBoot中使用@Scheduled创建定时任务
SpringBoot中使用@Scheduled创建定时任务 定时任务一般会在很多项目中都会用到,我们往往会间隔性的的去完成某些特定任务来减少服务器和数据库的压力.比较常见的就是金融服务系统推送回调,一 ...
- STL源码剖析——iterators与trait编程#3 iterator_category
最后一个迭代器的相应类型就是iterator_category,就是迭代器本身的类型,根据移动特性与实行的操作,迭代器被分为了五类: Input Iterator:这种迭代器所指的对象,不允许外界改变 ...
- Python学习-55 小游戏- 猜大小
#游戏开始,首先玩家选择大小,选择完成后开始摇骰子(11<=总值<=18为大,3<=总值<=10为小) import random def roll_dice(numbers= ...
- Kafka 的这些原理你知道吗
如果只是为了开发 Kafka 应用程序,或者只是在生产环境使用 Kafka,那么了解 Kafka 的内部工作原理不是必须的.不过,了解 Kafka 的内部工作原理有助于理解 Kafka 的行为,也利用 ...
- 怎样修改vim的缩进
默认vim的tab缩进是八个空格, 太长了, 需要改短一点. 第一步: 找到vimrc文件所在位置 # find / -name vimrc 第二步: 找到以后用vim打开vimrc文件并增加下面两行 ...
- Python之算法评估-4
一.评估算法的方式分两种,一种是分类算法的评估,一种是回归算法的评估.为什么要分两种呢,因为分类算法中可以通过准确率.精准率.召回率.混淆矩阵.AUC来评估算法的准确度.但是在预测值的时候是没有办法去 ...
- Markdown笔记(git提交带有emoji的commit描述)
用法:> git commit -m ' :tada: initial ' emoji来源:> ![gitmoji](https://gitmoji.carloscuesta.me/sta ...
- Authentication failed for "http://xxxxxx"
1.配置用户信息 git config --global user.name [username] git config --global user.email [email] 2.查询用户信息 ...
- 数据备份 及 Python 操作 Mysql
一 MySQL数据备份 #1. 物理备份: 直接复制数据库文件,适用于大型数据库环境.但不能恢复到异构系统中如Windows. #2. 逻辑备份: 备份的是建表.建库.插入等操作所执行SQL语句,适用 ...
- Linux查找文件之Find命令
Linux系统文件中常用属性包括以下内容:名称,大小,权限,属主,属组,修改时间,访问时间等.在庞大的Linux系统中查询文件,需要借助查找工具来实现,依此可以查询相同或指定属性的文件,本文所讲的查询 ...