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.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * 查询hbase表指定列簇的全部数据输出到HDFS上
 * @author mengyao
 *
 */
public class ReadHbase extends Configured implements Tool {

private static String tableName;
    private static String outputDir;

static class ReadHbaseMapper extends TableMapper<Text, Text> {
        private static Text k = new Text();
        private static Text v = new Text();
        @Override
        protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
            StringBuffer sb = new StringBuffer("");
            for(java.util.Map.Entry<byte[], byte[]> val : value.getFamilyMap(Bytes.toBytes("info")).entrySet()){
                String str = new String(val.getValue());
                if (str != null) {
                    sb.append(new String(val.getKey()));
                    sb.append(":");
                    sb.append(str);
                    sb.append(",");
                }
            }
            String line = sb.toString();
            k.set(key.get());
            v.set(new String(line.substring(0,line.length()-1)));
            context.write(k, v);
        }
    }

static class ReadHbaseReduce extends Reducer<Text, Text, Text, Text> {
        private Text result = new Text();
        @Override
        protected void reduce(Text key, Iterable<Text> value, Context context) throws IOException, InterruptedException {
            for (Text val : value) {
                result.set(val);
                context.write(key, result);
            }
        }
    }
    
    @Override
    public int run(String[] arg0) throws Exception {
        tableName = arg0[0];
        outputDir = arg0[1];
        Job job = Job.getInstance(getConf(), ReadHbase.class.getSimpleName());
        job.setJarByClass(ReadHbase.class);
        job.setReducerClass(ReadHbaseReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        FileOutputFormat.setOutputPath(job, new Path(outputDir));
        TableMapReduceUtil.initTableMapperJob(tableName, new Scan(), ReadHbaseMapper.class, Text.class, Text.class, job);
        TableMapReduceUtil.addDependencyJars(job);
        return job.waitForCompletion(true) ? 0 : 1;
    }
    
    public static void main(String[] args) throws Exception {
        String[] otherArgs = new GenericOptionsParser(args).getRemainingArgs();
        if (otherArgs.length != 2) {
            System.err.println(" Usage:" + ReadHbase.class.getSimpleName() + " <tableName> <outputDir> ");
            System.exit(2);
        }
        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");
        int status = ToolRunner.run(conf, new ReadHbase(), otherArgs);
        System.exit(status);
    }

}

使用MapReduce查询Hbase表指定列簇的全部数据输出到HDFS(一)的更多相关文章

  1. Mapreduce读取Hbase表,写数据到多个Hbase表中

    Job端的变化: 通过设置conf,配置输出表,在reduce中获取输出表名字 Configuration conf = job.getConfiguration(); //输出表1 conf.set ...

  2. Mapreduce读取Hbase表,写数据到一个Hbase表中

    public class LabelJob { public static void main(String[] args) throws Exception { Job job = Job.getI ...

  3. SQL Server 查询数据库表的列数

    select count(*) from sysobjects a join syscolumns b on a.id=b.id where a.name='表名' go

  4. SQL SERVER 查询一个表有多少列

    ) from syscolumns where id = object_id('tbname') 或者 select * from syscolumns where id = object_id('t ...

  5. oracle 在已有表新增列内批量加数据

    创建每列随机值的语句 create table TEST_ZHAA01A_03 as select rownum as id, to_char(sysdate + rownum/24/3600, 'y ...

  6. c# 遍历 Mysql 所有表所有列,查找目标数据

    在 Mysql 的 information_schema 库中 COLUMNS 表中存放了所有表的所有列. using MySql.Data.MySqlClient; using System; us ...

  7. sql查询单表之中大于2条的数据

    SELECT COUNT(字段)AS COUNT,字段FROM 表名 GROUP BY 字段 HAVING COUNT >=

  8. 仅对原表新增列的全量数据.csv

    w

  9. mysql 查询指定数据库所有表, 指定表所有列, 指定列所有表 所有外键及索引, 以及索引的创建和删除

    查询指定 数据库 中所有 表 (指定数据库的,所有表) // 可以把 TABLE_NAME 换成 * 号, 查看更丰富的信息 SELECT TABLE_NAME FROM information_sc ...

随机推荐

  1. Android之Notification的多种用法

    我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. 我们也 ...

  2. 源代码解读Cas实现单点登出(single sign out)功能实现原理--转

    关于Cas实现单点登入(single sing on)功能的文章在网上介绍的比较多,想必大家多多少少都已经有所了解,在此就不再做具体介绍.如果不清楚的,那只能等我把single sign on这块整理 ...

  3. 使用正则表达式给网址添加a标签

    在内容中存在链接地址的时候,我们在前台显示时一定想自动的将地址添加上a标签,方便用户进入链接.使用正则表达式就能轻松实现. Jsvascript正则替换 //javascript 正则替换 var s ...

  4. web04--cookie

    1.创建1.jsp <body> <form action="cookie/2.jsp" method="post"> 姓名:<i ...

  5. C# 面向对象 , 继承

    继承 class A { Console.WriteLine("hello world"); } class B:A { } 以上书写,表示B 是A 的子类,  B 同时继承A 中 ...

  6. bootstrap初探

    bootstrap资源 http://getbootstrap.com http://github.com/twbs http://www.bootcss.com bootstrap栅格系统 容器:流 ...

  7. vmware-tools(vmware workstation 10.0.4)安装的时候遇到的bug

    有个GitHub,专门解决C++编译的时候出的问题 地址

  8. java关键字 (jdk6),各自的含义是什么?

    Abstract 抽象的 一个Java语言中的关键字,用在类的声明中来指明一个类是不能被实例化的,但是可以被其它类继承.一个抽象类可以使用抽象方法,抽象方法不需要实现,但是需要在子类中被实现. bre ...

  9. iOS更改ShareSDK默认的分享功能界面

    ShareSDK的集成这里就不详细介绍了, 官网的都已经够详细了.. 官方的默认分享样式如下: 贴上我的源代码: // 创建分享图片 NSString *imageURLString = @" ...

  10. mac brew 安装包下载失败解决

    1.FQ或者用别的方式把安装包下载下来 2.查看缓存存储目录 brew --cache 3.将下载的包拷贝到缓存目录中,再此执行安装命令,如果安装还是去下载,检查下缓存目录是否多出一个下载中的文件,将 ...