MapReduce直接写入HBase

代码如下

package com.hbase.mapreduce;

import java.io.IOException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.codec.digest.DigestUtils;
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.KeyValue;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
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.Tool;
import org.apache.hadoop.util.ToolRunner; /**
* @author:FengZhen
* @create:2018年9月14日
*/
public class ImportFromFile extends Configured implements Tool{ private static String addr="HDP233,HDP232,HDP231";
private static String port="2181";
public static final String NAME = "ImportFromFile";
public enum Counters { LINES } static class ImportMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> { private byte[] family = null;
private byte[] qualifier = null; @Override
protected void setup(Mapper<LongWritable, Text, ImmutableBytesWritable, Put>.Context context)
throws IOException, InterruptedException {
String column = context.getConfiguration().get("conf.column");
byte[][] colkey = KeyValue.parseColumn(Bytes.toBytes(column));
family = colkey[0];
if (colkey.length > 1) {
qualifier = colkey[1];
}
} @Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, ImmutableBytesWritable, Put>.Context context)
throws IOException, InterruptedException {
try {
String lineString = value.toString();
//行键是经过MD5散列之后随机生成的键值
byte[] rowkey = DigestUtils.md5(lineString);
Put put = new Put(rowkey);
//存储原始数据到给定的表中的一列
put.addColumn(family, qualifier, Bytes.toBytes(lineString));
context.write(new ImmutableBytesWritable(rowkey), put);
context.getCounter(Counters.LINES).increment(1L);
} catch (Exception e) {
e.printStackTrace();
}
}
} /**
* 使用Apache Commons CLI类解析命令行参数。
* @param args
* @return
*/
private static CommandLine parseArgs(String[] args) {
Options options = new Options();
Option option = new Option("t", "table", true, "table to import into -must exist");
option.setArgName("table-name");
option.setRequired(true);
options.addOption(option); option = new Option("c", "column", true, "column to store row data into -must exit");
option.setArgName("family:qualifier");
option.setRequired(true);
options.addOption(option); option = new Option("i", "input", true, "the directory or file to read from");
option.setArgName("path-in-HDFS");
option.setRequired(true);
options.addOption(option); options.addOption("d", "debug", false, "switch on DEBUG log level"); CommandLineParser parser = new PosixParser();
CommandLine cmd = null;
try {
cmd = parser.parse(options, args);
} catch (ParseException e) {
e.printStackTrace();
System.err.println("ERROR: " + e.getMessage() + "\n");
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(NAME + " ", options, true);
System.exit(1);
}
return cmd;
} public int run(String[] arg0) throws Exception {
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum",addr);
configuration.set("hbase.zookeeper.property.clientPort", port);
//String[] otherArgs = new GenericOptionsParser(configuration, arg0).getRemainingArgs();
//CommandLine commandLine = parseArgs(arg0); // String table = commandLine.getOptionValue("t");
// String input = commandLine.getOptionValue("i");
// String column = commandLine.getOptionValue("c"); String table = arg0[0];
String input = arg0[1];
String column = arg0[2];
configuration.set("conf.column", column); Job job = Job.getInstance(configuration);
job.setJobName("ImportFromFile");
job.setJarByClass(ImportFromFile.class);
job.setMapperClass(ImportMapper.class);
job.setOutputFormatClass(TableOutputFormat.class);
job.getConfiguration().set(TableOutputFormat.OUTPUT_TABLE, table);
job.setOutputKeyClass(ImmutableBytesWritable.class);
job.setOutputValueClass(Writable.class);
//这是一个只包含map阶段的作业,框架会直接跳过reduce阶段
job.setNumReduceTasks(0); FileInputFormat.addInputPath(job, new Path(input));
return job.waitForCompletion(true) ? 0 : 1;
} public static void main(String[] args) throws Exception {
String[] params = new String[] {"test_table_mr", "hdfs://fz/data/fz/input/hbase", "data:info"};
int exitCode = ToolRunner.run(new ImportFromFile(), params);
System.exit(exitCode);
}
}

MapReduce-读取文件写入HBase的更多相关文章

  1. Mapreduce的文件和hbase共同输入

    Mapreduce的文件和hbase共同输入 package duogemap;   import java.io.IOException;   import org.apache.hadoop.co ...

  2. MapReduce和Spark写入Hbase多表总结

    作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 大家都知道用mapreduce或者spark写入已知的hbase中的表时,直接在mapreduc ...

  3. shell读取文件写入新文件

    #!/bin/sh #系统简称 SYST="HVPS" #发送行号 SEND1234SEND=" #接收行号 RECV1234RECV=" cd /home/w ...

  4. python小练习之读取文件写入excel

    文件是个json文件 内容为: 导入excel后的格式为 屡一下思路 一步步怎么实现: 1 首先需要读取json文件 然后将读取的内容转为字典 2 将excel的列名写入一个list中 然后遍历执行写 ...

  5. Python学习笔记五(读取提取写入文件)

    #Python打开读取一个文件内容,然后写入一个新的文件中,并对某些字段进行提取,写入新的字段的脚本,与大家共同学习. import os import re def get_filelist(dir ...

  6. 【HBase】HBase与MapReduce集成——从HDFS的文件读取数据到HBase

    目录 需求 步骤 一.创建maven工程,导入jar包 二.开发MapReduce程序 三.结果 需求 将HDFS路径 /hbase/input/user.txt 文件的内容读取并写入到HBase 表 ...

  7. 使用MapReduce读取HBase数据存储到MySQL

    Mapper读取HBase数据 package MapReduce; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hba ...

  8. 用mapreduce读取hdfs数据到hbase上

    hdfs数据到hbase过程 将HDFS上的文件中的数据导入到hbase中 实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具 hbase先创建好表   cre ...

  9. MapReduce-从HBase读取数据处理后再写入HBase

    MapReduce-从HBase读取处理后再写入HBase 代码如下 package com.hbase.mapreduce; import java.io.IOException; import o ...

随机推荐

  1. 字符设备驱动程序--LED驱动

    编写驱动程序需要编写那些代码: 1.硬件相关的驱动程序 2.Makefile的编译程序 3.还需要编写一个相关的测试程序 比如说:一个摄像头驱动程序 1.驱动程序的编写,需要编写一些硬件相关的操作,编 ...

  2. jmeter返回结果是乱码及Unicode转中文

    1.结果树中有乱码的处理方式 到bin目录下面修改jmeter的配置文件 使用jmeter进行压测时,如果响应数据的返回结果有乱码,则修改jmeter.properties文件中的该字段的值改为utf ...

  3. [Spring MVC]学习笔记--form表单标签的使用

    github例子地址: https://github.com/lemonbar/spring-mvc-jsp 效果图 关于spring mvc的标签的讲解, 有一篇blog已经讲的很细了. http: ...

  4. 《挑战程序设计竞赛》2.2 贪心法-区间 POJ2376 POJ1328 POJ3190

    POJ2376 Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14585   Accepte ...

  5. 剖析与优化 Go 的 web 应用

    https://mp.weixin.qq.com/s/HDsbZLOK3h8-XjejvPH2sA https://studygolang.com/articles/12685

  6. HNOI2014

    本蒟蒻到现在才把$HNOI2014$的坑填完... $AFO$之后码力急速下降... 感觉都没有码力了... 附上题解: $DAY1$: $T1$: BZOJ3571: [Hnoi2014]画框 $T ...

  7. Python3 面向对象(1)

    面向.概述 面向过程: 根据业务逻辑从上到下写垒代码面向过程的设计的核心是过程,过程即解决问题的步骤, 面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西 优点: 极大降低了程序的 ...

  8. python cookbook第三版学习笔记二十一:利用装饰器强制函数上的类型检查

    在演示实际代码前,先说明我们的目标:能对函数参数类型进行断言,类似下面这样: @typeassert(int, int) ... def add(x, y): ...     return x + y ...

  9. C语言定义一个指针变量

    10.2.1 定义一个指针变量 对指针变量的定义包括三个内容: (1) 指针类型说明,即定义变量为一个指针变量: (2) 指针变量名: (3) 变量值(指针)所指向的变量的数据类型. 其一般形式为: ...

  10. Hibernate Student_Course_Score设计

    示例: 设计代码,实现在数据库中建student表.course表.和score表,展现三者关系 student表:id.name course表:id.name score表:id.score.st ...