永发信息网

应使用什么技术来实现 HBase 的二级索引

答案:3  悬赏:70  手机版
解决时间 2021-11-10 19:34
  • 提问者网友:眉目添风霜
  • 2021-11-10 12:08
应使用什么技术来实现 HBase 的二级索引
最佳答案
  • 五星知识达人网友:怙棘
  • 2021-11-10 12:24
现在比较著名的方案是华为提出的。要点是1) 保证主表和索引表在同一个regionserver上(通过自定义的balancer实现) 2) 使用coprocessor实现索引表的创建和插入。已经开源:Huawei-Hadoop/hindex · GitHub
全部回答
  • 1楼网友:山君与见山
  • 2021-11-10 13:27
应使用什么技术来实现 HBase 的二级索引
写了个Hbase新的api的增删改查的工具类,以供参考,直接拷贝代码就能用,散仙觉得基础的功能,都有了,代码如下:
  package com.dhgate.hbase.test;

import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;


public class HbaseCommons {

static Configuration conf=HBaseConfiguration.create();
static String tableName="";

public static void main(String[] args)throws Exception {

//String tableName="test";
//createTable(tableName, null);

}


public static void insertList(String tableName,String rows[])throws Exception{
HTable table=new HTable(conf, tableName);
List list=new ArrayList();
for(String r:rows){
Put p=new Put(Bytes.toBytes(r));
//此处示例添加其他信息
//p.add(Bytes.toBytes("family"),Bytes.toBytes("column"), 1000, Bytes.toBytes("value"));
list.add(p);
}
table.put(list);//批量添加
table.close();//释放资源
}


public static void createTable(String tableName,String[] columnFamilys)throws Exception{
//admin 对象
HBaseAdmin admin=new HBaseAdmin(conf);
if(admin.tableExists(tableName)){
System.out.println("此表,已存在!");
}else{
//旧的写法
//HTableDescriptor tableDesc=new HTableDescriptor(tableName);
//新的api
HTableDescriptor tableDesc=new HTableDescriptor(TableName.valueOf(tableName));

for(String columnFamily:columnFamilys){
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
}

admin.createTable(tableDesc);
System.out.println("建表成功!");

}
admin.close();//关闭释放资源

}


public static void deleteTable(String tableName)throws Exception{
HBaseAdmin admin=new HBaseAdmin(conf);
if(admin.tableExists(tableName)){
admin.disableTable(tableName);//禁用表
admin.deleteTable(tableName);//删除表
System.out.println("删除表成功!");
}else{
System.out.println("删除的表不存在!");
}
admin.close();
}


public static void insertOneRow(String tableName,String rowkey,String columnFamily,String column,String value)throws Exception{

HTable table=new HTable(conf, tableName);
Put put=new Put(Bytes.toBytes(rowkey));
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);//放入表
table.close();//释放资源

}


public static void deleteOneRow(String tableName,String row)throws Exception{

HTable table=new HTable(conf, tableName);
Delete delete=new Delete(Bytes.toBytes(row));
table.delete(delete);
table.close();
}


public static void deleteList(String tableName,String rows[])throws Exception{
HTable table=new HTable(conf, tableName);
List list=new ArrayList();
for(String row:rows){
Delete del=new Delete(Bytes.toBytes(row));
list.add(del);
}
table.delete(list);
table.close();//释放资源

}


public static void getOneRow(String tableName,String row)throws Exception{
HTable table=new HTable(conf, tableName);
Get get=new Get(Bytes.toBytes(row));
Result result=table.get(get);
printRecoder(result);//打印记录
table.close();//释放资源
}


public static void showAll(String tableName)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
printRecoder(r);//打印记录
}
table.close();//释放资源
}


public static void ScanPrefixByRowKey(String tableName,String rowKey)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
scan.setFilter(new PrefixFilter(Bytes.toBytes(rowKey)));
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
printRecoder(r);//打印记录
}
table.close();//释放资源
}


public static void ScanPrefixByRowKeyAndLimit(String tableName,String rowKey,long limit)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
scan.setFilter(new PrefixFilter(Bytes.toBytes(rowKey)));
scan.setFilter(new PageFilter(limit));
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
printRecoder(r);//打印记录
}
table.close();//释放资源
}


public void scanByStartAndStopRow(String tableName,String startRow,String stopRow)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
scan.setStartRow(Bytes.toBytes(startRow));
scan.setStopRow(Bytes.toBytes(stopRow));
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
printRecoder(r);
}
table.close();//释放资源

}

public static void getValueDetail(String tableName,String columnFalimy,String column)throws Exception{

HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
System.out.println("值: " +new String(r.getValue(Bytes.toBytes(columnFalimy), Bytes.toBytes(column))));
}
table.close();//释放资源

}


public static void printRecoder(Result result)throws Exception{
for(Cell cell:result.rawCells()){
System.out.print("行健: "+new String(CellUtil.cloneRow(cell)));
System.out.print("列簇: "+new String(CellUtil.cloneFamily(cell)));
System.out.print(" 列: "+new String(CellUtil.cloneQualifier(cell)));
System.out.print(" 值: "+new String(CellUtil.cloneValue(cell)));
System.out.println("时间戳: "+cell.getTimestamp());
}
}

}
  • 2楼网友:老鼠爱大米
  • 2021-11-10 13:16
1、使用solr/ES自己开发二级索引。
2、使用Lily Hbase(solr+hbase)实现二级索引。
3、华为Hbase二级索引方案,已经在githup开源,但2、3年没更新。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯