创建hbase索引表之hbase与hadoop整合

hbase创建索引表只是为了减少查询表中数据的时间,优化查询。但是前提是表中数据很大的情况下,表数据很少完全没有必要建立索引表。。建立索引表是一种以空间换取时间的做法。。。
下面来看这道题

heroes表

索引表

以下是实现索引表的代码

package com.yc.hadoop.hbase.heroes;

import java.util.Enumeration;
import java.util.Random;
import java.util.ResourceBundle;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableExistsException;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto.MutationType;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.util.ResourceBundles;

import com.yc.hadoop.hbase.util.HBaseUtil;

public class Heroes {
    public static void getData(){
        try {
            HBaseUtil.createTable("heroes1", "info");
            String[] heronames = new String[] { "peter", "hiro", "sylar", "claire", "noah" };
            Random rand=new Random();
            for (int i = 1; i <= 5; i++) {
                HBaseUtil.doUpdate("heroes1", MutationType.PUT,i+"",
                        new String[]{"info","name",heronames[i-1]},
                        new String[]{"info","email",heronames[i-1]+"@heroes.com"},
                        new String[]{"info","power",randString(rand.nextInt(5)+3)});
            }
        } catch (TableExistsException e) {
            System.out.println(e.getMessage());
        }
    }
    public static String randString(int len){
        String str="abcdefgijklmnopqstuwxyz1234567890ABCDEFGIJKLMNOPQSTUWXYZ";
        StringBuilder sb=new StringBuilder();
        Random rand=new Random();
        for (int i = 0; i < len; i++) {
            sb.append(str.charAt(rand.nextInt(str.length())));

        }
        return sb.toString();
    }
    public static void main(String[] args) throws Exception {
        getData();
        Configuration conf=HBaseConfiguration.create();//加载hadoop和hbase的配置文件创建配置文件对象
        //加载hbase.properties配置文件信息
        ResourceBundle rb=ResourceBundles.getBundle("hbase");
        Enumeration<String> kvs=rb.getKeys();
        while (kvs.hasMoreElements()) {
            String key=kvs.nextElement();
            String value=rb.getString(key);
            conf.set(key,value);//设置hbase由哪个zookeeper集群协调管理
        }
        Job job=Job.getInstance(conf, "heroes1");
        job.setJarByClass(Heroes.class);
        Scan  scan=new Scan().addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"));
        //TableMapReduceUtil.initTableMapperJob("heroes", scan, HBaseIndexMapper.class, ImmutableBytesWritable.class, ImmutableBytesWritable.class, job);
        TableMapReduceUtil.initTableMapperJob("heroes1", scan, HBaseIndexMapper02.class, Text.class, Text.class, job);
        try {
            HBaseUtil.createTable("index_heroes1", "rowkey");
        } catch (TableExistsException e) {
            System.out.println(e.getMessage());
        }
        TableMapReduceUtil.initTableReducerJob("index_heroes1", HBaseIndexReducer02.class, job);
        //FileOutputFormat.setOutputPath(job,new Path("hdfs://master:9000/out"+System.currentTimeMillis()));
        System.exit(job.waitForCompletion(true) ? 0 : 1);

    }   

}
package com.yc.hadoop.hbase.heroes;

import java.io.IOException;
import java.util.List;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
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.Mapper;

public class HBaseIndexMapper02 extends TableMapper<Text, Text> {
    @Override
    protected void map(ImmutableBytesWritable key, Result value,
            Mapper<ImmutableBytesWritable, Result, Text, Text>.Context context)
                    throws IOException, InterruptedException {
        Text k=new Text(CellUtil.cloneValue(value.listCells().get(0)));
        Text v=new Text(key.get());
        System.out.println("k===>"+k+"v===>"+v);
        context.write(k,v);
    }
}
package com.yc.hadoop.hbase.heroes;

import java.io.IOException;

import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class HBaseIndexReducer02 extends TableReducer<Text, Text, ImmutableBytesWritable> {
    @Override
    protected void reduce(Text key, Iterable<Text> value, Reducer<Text, Text, ImmutableBytesWritable, Mutation>.Context context)
            throws IOException, InterruptedException {
        //把名字做行健
        //ImmutableBytesWritable k=new ImmutableBytesWritable(key.getBytes()); 这个会多加字符
        ImmutableBytesWritable k=new ImmutableBytesWritable(key.copyBytes());
        Put put=new Put(k.get());
        for (Text text : value) {
            put.addColumn(Bytes.toBytes("rowkey"),Bytes.toBytes("index"), Bytes.toBytes(text.toString()));
        }
        context.write(k, put);
    }

}

运行后结果如图所示
这里写图片描述

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值