Mapreduce构建hbase二级索引
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,import java.io.IOException;import java.util.HashMap;import java.util.Map;import java.util.Set;import
千家信息网最后更新 2026年10月31日Mapreduce构建hbase二级索引
import java.io.IOException;import java.util.HashMap;import java.util.Map;import java.util.Set;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.client.Put;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.MultiTableOutputFormat;import org.apache.hadoop.hbase.mapreduce.TableInputFormat;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.mapreduce.Job;import org.apache.hadoop.util.GenericOptionsParser;public class IndexBuilder { private class MyMapper extends TableMapper { private Map indexes = new HashMap(); private String columnFamily; @Override protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException { Set keys = indexes.keySet(); for (byte[] k : keys) { ImmutableBytesWritable indexTableName = indexes.get(k); byte[] val = value.getValue(Bytes.toBytes(columnFamily), k); Put put = new Put(val);// 索引表的rowkey为原始表的值 put.add(Bytes.toBytes("f1"), Bytes.toBytes("id"), key.get());// 索引表的内容为原始表的rowkey context.write(indexTableName, put); } } @Override protected void setup(Context context) throws IOException, InterruptedException { Configuration conf = context.getConfiguration(); String tableName = conf.get("tableName"); columnFamily = conf.get("columnFamily"); String[] qualifiers = conf.getStrings("qualifiers"); // indexes的key为列名,value为索引表名 for (String q : qualifiers) { indexes.put( Bytes.toBytes(q), new ImmutableBytesWritable(Bytes.toBytes(tableName + "-" + q))); } } } public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration conf = HBaseConfiguration.create(); String[] otherargs = new GenericOptionsParser(conf, args) .getRemainingArgs();// 去除掉没有用的命令行参数 // 输入参数:表名,列族名,列名 if (otherargs.length < 3) { System.exit(-1); } String tableName = otherargs[0]; String columnFamily = otherargs[1]; conf.set("tableName", tableName); conf.set("columnFamily", columnFamily); String[] qualifiers = new String[otherargs.length - 2]; for (int i = 0; i < qualifiers.length; i++) { qualifiers[i] = otherargs[i + 2]; } conf.setStrings("qualifiers", qualifiers); Job job = new Job(conf, tableName); job.setJarByClass(IndexBuilder.class); job.setMapperClass(MyMapper.class); job.setNumReduceTasks(0); job.setInputFormatClass(TableInputFormat.class); // 可以输出多张表 job.setOutputFormatClass(MultiTableOutputFormat.class); Scan scan = new Scan(); scan.setCaching(1000); TableMapReduceUtil.initTableMapperJob(tableName, scan, MyMapper.class, ImmutableBytesWritable.class, Put.class, job); job.waitForCompletion(true); }}
索引
原始
参数
内容
命令
多张
有用
输入
输出
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技企业
科技互联网龙头
致骋互联网科技
小马过河互联网科技
睿民互联网科技
猫创互联网科技
互联网科技网站
撒么互联网科技公司
天扬科技互联网广告
互联网金融监管科技
镁科技互联网资讯平台
福州新华互联网科技学校 知乎
花神(南京)互联网科技有限公司
科技类公司包括互联网吗
爱医互联网络科技
新氧科技和美国互联网有关系吗
国产互联网科技公司
保山浩联互联网科技有限公司
杭州科技互联网公司有哪些
如何强化互联网金融科技
互联网科技海报
互联网和科技技术
互联网科技的英文
世界互联网领先科技成果
西安互联网科技公司
北京禾田雨橡互联网科技
互联网科技的未来发展
互联网高科技股票
亳州好赞互联网科技有限公司
西安左小互联网科技有限公司