hbase创建索引表只是为了减少查询表中数据的时间,优化查询。但是前提是表中数据很大的情况下,表数据很少完全没有必要建立索引表。。建立索引表是一种以空间换取时间的做法。。。
下面来看这道题
![heroes表](https://img-blog.csdn.net/20170802164324725?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3dhbmczNjUwNzEx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![索引表](https://img-blog.csdn.net/20170802164346086?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3dhbmczNjUwNzEx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
以下是实现索引表的代码
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
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
sb.append(str.charAt(rand.nextInt(str.length())))
}
return sb.toString()
}
public static void main(String[] args) throws Exception {
getData()
Configuration conf=HBaseConfiguration.create()
//加载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)
}
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)
}
}
运行后结果如图所示
![这里写图片描述](https://img-blog.csdn.net/20170802164810389?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3dhbmczNjUwNzEx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![这里写图片描述](https://img-blog.csdn.net/20170802164826473?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ3dhbmczNjUwNzEx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)