Mapreduce构建hbase二级索引
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,import java.io.IOException;import java.util.HashMap;import java.util.Map;import java.util.Set;import
千家信息网最后更新 2025年11月07日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安全错误
数据库的锁怎样保障安全
系统时间同步服务器地址怎么去掉
制作图数据库
河北省联通dns服务器云空间
怎么操作linux里面的数据库
读者数据库培训
将归档数据保存到远程数据库
山东国行互联网科技有限公司
青海云虚拟主机云服务器
php数据库新闻调用
分布式服务器最大支持多少兆
应用软件开发案例报告
济南win10电脑服务器租用
网络安全行政处罚
地下城堡怎么查询服务器
泰拉瑞亚如何加入好友服务器
株洲it软件开发工程师暑假班
软件开发模块文档
宿城区工业网络技术大概费用
安防管理平台服务器
创欧网络技术(上海)有限公司
如何做好网络安全保护工作
万豪国际证实数据库遭黑客攻击
数据库入门查看建的表的结构
档案数字化软件开发方案
妙月互联网科技有限公司
手机怎么掌握网络技术
客户端软件开发有哪些职责
北京安永网络安全怎么样
服务器安装虚拟机报错误代码
中山网络安全产品