使用MapReduce将HDFS数据导入到HBase(二)
package com.bank.service;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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.hbase.util.Bytes;
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 org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
/**
* 使用MapReduce批量导入Hbase
* 通过TableOutputFormat,该类内部传给指定的Put实例并调用table.put()方法。作业结束前会主动调用flushCommits()方法保存仍在写缓冲区的数据
*
* @author mengyao
*
*/
public class CnyBatch extends Configured implements Tool {
static class CnyBatchMapper extends Mapper<LongWritable, Text, LongWritable, Text> {
protected void map(LongWritable key, Text value, Context context)
throws java.io.IOException, InterruptedException {
context.write(key, value);
}
}
static class CnyBatchReduce extends TableReducer<LongWritable, Text, NullWritable> {
private final static String familyName = "info";
private final static String[] qualifiers = {"gzh", "currency", "version", "valuta", "qfTime", "flag", "machineID"};
@Override
protected void reduce(LongWritable key,
java.lang.Iterable<Text> value, Context context)
throws java.io.IOException, InterruptedException {
final String[] values = value.toString().split("\t");
if (values.length == 7 && values.length == qualifiers.length) {
final String row = values[0]+"_"+values[1]+"_"+values[2]+"_"+values[3];
long timestamp = System.currentTimeMillis();
Put put = new Put(Bytes.toBytes(row));
for (int i = 0; i < values.length; i++) {
String qualifier = qualifiers[i];
String val = values[i];
put.add(Bytes.toBytes(familyName), Bytes.toBytes(qualifier), timestamp, Bytes.toBytes(val));
}
context.write(NullWritable.get(), put);
} else {
System.err.println(" ERROR: value length must equale qualifier length ");
}
};
}
@Override
public int run(String[] arg0) throws Exception {
Job job = Job.getInstance(getConf(), CnyBatch.class.getSimpleName());
TableMapReduceUtil.addDependencyJars(job);
job.setJarByClass(CnyBatch.class);
FileInputFormat.setInputPaths(job, arg0[0]);
job.setMapperClass(CnyBatchMapper.class);
job.setMapOutputKeyClass(LongWritable.class);
job.setMapOutputValueClass(Text.class);
job.setReducerClass(CnyBatchReduce.class);
job.setOutputFormatClass(TableOutputFormat.class);
return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "h5:2181,h6:2181,h7:2181");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("dfs.socket.timeout", "100000");
String[] otherArgs = new GenericOptionsParser(args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println(" ERROR: <dataInputDir> <tableName>");
System.exit(2);
}
conf.set(TableOutputFormat.OUTPUT_TABLE, args[1]);
int status = ToolRunner.run(conf, new CnyBatch(), args);
System.exit(status);
}
}
使用MapReduce将HDFS数据导入到HBase(二)的更多相关文章
- 使用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 ...
- 使用MapReduce将HDFS数据导入Mysql
使用MapReduce将Mysql数据导入HDFS代码链接 将HDFS数据导入Mysql,代码示例 package com.zhen.mysqlToHDFS; import java.io.DataI ...
- 使用MapReduce将mysql数据导入HDFS
package com.zhen.mysqlToHDFS; import java.io.DataInput; import java.io.DataOutput; import java.io.IO ...
- 用mapreduce读取hdfs数据到hbase上
hdfs数据到hbase过程 将HDFS上的文件中的数据导入到hbase中 实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具 hbase先创建好表 cre ...
- HBase(三): Azure HDInsigt HBase表数据导入本地HBase
目录: hdfs 命令操作本地 hbase Azure HDInsight HBase表数据导入本地 hbase hdfs命令操作本地hbase: 参见 HDP2.4安装(五):集群及组件安装 , ...
- 将Excel中数据导入数据库(二)
在上篇文章中介绍到将Excel中数据导入到数据库中,但上篇文章例子只出现了nvachar类型,且数据量很小.今天碰到将Excel中数据导入数据库中的Excel有6419行,其中每行均有48个字段,有i ...
- HBase结合MapReduce批量导入(HDFS中的数据导入到HBase)
HBase结合MapReduce批量导入 package hbase; import java.text.SimpleDateFormat; import java.util.Date; import ...
- 把hdfs数据写入到hbase表
功能:把hdfs上的数据写入到hbase表. hadoop的mapreduce输出要导入到hbase表,最好先输出HFile格式,再导入hbase,因为HFile是hbase的内部存储格式,所以导入效 ...
随机推荐
- Linux/UNIX套接字连接
套接字连接 套接字是一种通信机子.凭借这样的机制.客户/server系统的开发工作既能够在本地单机上进行.也能够夸网络进行. 套接字的创建和使用与管道是有差别的.由于套接字明白地将客户和server区 ...
- VSS Get Latest Version 没有提示recursive的对话框解决
今天按照VSS使用时,当“Get Latest version”时,不小心勾选了 “Only show this dialog when the Shift key is down”,因此当我再“Ge ...
- STL之Pairs
什么是Pair 关于类Pair的介绍,下面是引自<C++ Standard Library>的一段话: The class pair is provided to treat two va ...
- Apache和Nginx平滑重启
之前修改了服务器配置都是简单粗暴的用restart重启apache/nginx,据说这样不好.需要平滑重启服务器,避免重启时打断用户行为.然后就根据官方文档了解了一下平滑重启的命令.本文根据Apach ...
- GitHub的css/js文件给墙了的解决方法
今天早上一打开github发现css等都加载失败. 后来发现是给墙了. 本人用的是windows. 解决方法:改hosts 一般情况是在这里:C:\Windows\System32\drivers\e ...
- 【※索引】mysql索引常用命令
索引分单列索引和组合索引.单列索引,即一个索引只包含单个列,一个表可以有多个单列索引,但这不是组合索引.组合索引,即一个索包含多个列. MySQL索引类型包括: (1)普通索引 这是最基本的索引,它没 ...
- String分割类
StringTokenizer类 用的时候要 import java.util.StringTokenizer; StringTokenizer b=new StringTokenizer( Stri ...
- switch语句中的选择因子
switch语句能否用作用在byte上,能否作用在long上,能否作用在String上? switch选择语句的格式为: switch(intergral-selector){ case integ ...
- mysql服务启动
1.C:\Program Files (x86)\MySQL\mysql-5.5.33-win32\bin>net stop mysqlMySQL 服务正在停止.MySQL 服务已成功停止. 2 ...
- 使用github创建博客
本文主要介绍以下几个内容: 1.使用githbu创建自己的博客 2.将博客域名映射到自己的域名 3.如果写博客 一.使用github创建自己的博客 具体可参考https://pages.githu ...