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. root用户改动普通用户文件

    首先使用别的用户登录入LINUX系统,切换成root用户.进入到须要改动的用户主文件夹,对该用户文件夹下的文件进行改动

  2. MySQL-视频跟随练习笔记

    在表中添加某列 alter table tbl_user add email varchar(50); 在表中删除某列 alter table tbl_user drop email; 重命名某列 a ...

  3. mwc config.h 中文注释

    #ifndef CONFIG_H_ #define CONFIG_H_ /*************************************************************** ...

  4. spring05配置文件之间的关系

    一:配置文件包含关系 1.创建对应的实体类 public class Student { //学生实体类 private String name; //姓名 private Integer age; ...

  5. 简单水池&&迷宫问题

    #include <iostream> #include <stdio.h> #include <cstring> using namespace std; int ...

  6. Highcharts 设置折线图的显示与隐藏

    1.初始化隐藏某条曲线 series 配置如: [ {name:"发帖", postCountData}, {name:"删帖帖", deleteCountDa ...

  7. SQL从入门到基础 - 02 SQLServer的使用

    一.SQLServer的管理 服务器名称:ICECOA-81DEA7A2.\SQLEXPRESS 1. 数据库->表->字段->主键 2. 编辑表 二.数据类型 1. bit:相当于 ...

  8. 静默安装ORACLE【weber出品必属精品】

    安装配置系统环境安装linux ,所有服务都不选择,只是选择安装开发工具,不要安装防火墙(当然也可以在后面关闭) 打开终端,执行如下命令,检查安装包,没有的都要安装 make, glibc, liba ...

  9. MD5加密运算

    //MD5 对字符串的加密 -(void)demo1 { NSString *str = @"love"; //对字符串进行MD5加密 str = str.md5String; N ...

  10. PHP String

    PHP 5 String 函数 PHP String 函数是 PHP 核心的组成部分.无需安装即可使用这些函数. 函数 描述 addcslashes() 返回在指定的字符前添加反斜杠的字符串. add ...