Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Building hbase Secondary Index by Mapreduce

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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) / / the rowkey of the index table is the value put.add of the original table (Bytes.toBytes ("F1"), Bytes.toBytes ("id"), key.get (); / / the content of the index table is the rowkey context.write (indexTableName, put) of the original table. } @ 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") / / key of indexes is the column name, and value is the index table name 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 () / / remove useless command line arguments / / input parameters: table name, column family name, column name 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); / / you can output multiple tables 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);}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report