使用MapReduce将HDFS数据导入到HBase(一)
package com.bank.service;
import java.io.IOException;
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.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
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.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
/**
* 使用MapReduce批量导入Hbase(没有Reduce函数的MapReduce)
* @author mengyao
*
*/
public class DataImportToHbase extends Configured implements Tool {
static class DataImportToHbaseMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {
private static String familyName = "info";
private static String[] qualifiers = {"gzh", "currency", "version", "valuta", "qfTime", "flag", "machineID"};
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] values = line.split("\t");
if (values.length == 7 && values.length == qualifiers.length) {
String row = values[0]+"_"+values[1]+"_"+values[2]+"_"+values[3];
long timestamp = System.currentTimeMillis();
ImmutableBytesWritable immutable = new ImmutableBytesWritable(Bytes.toBytes(row));
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(immutable, 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(), DataImportToHbase.class.getSimpleName());
job.setJarByClass(DataImportToHbase.class);
job.setInputFormatClass(TextInputFormat.class);
FileInputFormat.setInputPaths(job, new Path(arg0[0]));
job.setMapperClass(DataImportToHbaseMapper.class);
job.setMapOutputKeyClass(ImmutableBytesWritable.class);
job.setMapOutputValueClass(Put.class);
TableMapReduceUtil.initTableReducerJob(arg0[1], null, job);
job.setNumReduceTasks(0);
TableMapReduceUtil.addDependencyJars(job);
return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "h5:2181,h6:2181,h7:2181");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("dfs.socket.timeout", "3600000");
String[] otherArgs = new GenericOptionsParser(args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println(" ERROR: <dataInputDir> <tableName>");
System.exit(2);
}
int status = ToolRunner.run(conf, new DataImportToHbase(), otherArgs);
System.exit(status);
}
}
使用MapReduce将HDFS数据导入到HBase(一)的更多相关文章
- 使用MapReduce将HDFS数据导入到HBase(二)
package com.bank.service; import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf. ...
- 使用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安装(五):集群及组件安装 , ...
- 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的内部存储格式,所以导入效 ...
- sqoop将关系型的数据库得数据导入到hbase中
1.sqoop将关系数据库导入到hbase的参数说明
随机推荐
- android api 中文 (73)—— AdapterView
前言 本章内容是android.widget.AdapterView,版本为Android 2.3 r1,翻译来自"cnmahj",欢迎大家访问他的博客:http://androi ...
- win7 无线网络无法启动
开始菜单-运行输入services.msc然后确定!找到WLAN Autoconfig这一项,启动此项服务,一切就OK了
- android studio 的部分设置
1.android studio 如何提示方法的用法 在 Eclipse中鼠标放上去就可以提示方法的用法,实际上Android Studio也可以设置的.如图 Preferences > Edi ...
- linux删除或隐藏命令历史记录history
1.环境变量添加HISTCONTROL = ignorespace 在命令前面插入空格,这条命令会被 shell 忽略,也就意味着它不会出现在历史记录中.但是这种方法有个前提,只有在你的环境变量 HI ...
- MM32Flash读写操作(兼容STM32)
MM32Flash读写操作(兼容STM32) Flash基础描述 思维导图 编程实现读写操作 主函数结构 #include "delay.h" #include "sys ...
- 更加详细的Log4net的配置
请转到周金桥的文章 http://blog.csdn.net/zhoufoxcn/article/details/6029021
- Error prompt:“xxx is not in the sudoers file”----Solution
//Situation System prompts "xxx is not in the sudoers file"(xxx equals the user name) w ...
- php正则表达式总结
<?php echo 'wj'; echo '<br>'; $file = '<td>移动150卡</td><!--<td></td& ...
- GUID的广泛使用
GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值. GUID ...
- PHP编程规范
好的编程规范不仅是对阅读者的负责,也是对自身的负责: ----割---- 一直以来我都是以php函数的风格来写php,所有变量,函数,类都使用小写,单词之间以下划线隔开,一直比较排斥驼峰式的代码规范, ...