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

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

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

写了个Hbase新的api的增删改查的工具类,以供参考,直接拷贝代码就能用,散仙觉得基础的功能,都有了,代码如下:

package comdhgatehbasetest;

import javautilArrayList;

import javautilList;

import orgapachehadoopconfConfiguration;

import orgapachehadoophbaseCell;

import orgapachehadoophbaseCellUtil;

import orgapachehadoophbaseHBaseConfiguration;

import orgapachehadoophbaseHColumnDescriptor;

import orgapachehadoophbaseHTableDescriptor;

import orgapachehadoophbaseTableName;

import orgapachehadoophbaseclientDelete;

import orgapachehadoophbaseclientGet;

import orgapachehadoophbaseclientHBaseAdmin;

import orgapachehadoophbaseclientHTable;

import orgapachehadoophbaseclientPut;

import orgapachehadoophbaseclientResult;

import orgapachehadoophbaseclientResultScanner;

import orgapachehadoophbaseclientScan;

import orgapachehadoophbasefilterPageFilter;

import orgapachehadoophbasefilterPrefixFilter;

import orgapachehadoophbaseutilBytes;

/

基于新的API

Hbase096版本

写的工具类

@author qindongliang

数据技术交流群: 376932160

/

public class HbaseCommons {

static Configuration conf=HBaseConfigurationcreate();

static String tableName="";

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

//String tableName="test";

//createTable(tableName, null);

}

/

批量添加数据

@param tableName 标名字

@param rows rowkey行健的集合

本方法仅作示例,其他的内容需要看自己义务改变

/

public static void insertList(String tableName,String rows[])throws Exception{

HTable table=new HTable(conf, tableName);

List<Put> list=new ArrayList<Put>();

for(String r:rows){

Put p=new Put(BytestoBytes(r));

//此处示例添加其他信息

//padd(BytestoBytes("family"),BytestoBytes("column"), 1000, BytestoBytes("value"));

listadd(p);

}

tableput(list);//批量添加

tableclose();//释放资源

}

/

创建一个表

@param tableName 表名字

@param columnFamilys 列簇

/

public static void createTable(String tableName,String[] columnFamilys)throws Exception{

//admin 对象

HBaseAdmin admin=new HBaseAdmin(conf);

if(admintableExists(tableName)){

Systemoutprintln("此表,已存在!");

}else{

//旧的写法

//HTableDescriptor tableDesc=new HTableDescriptor(tableName);

//新的api

HTableDescriptor tableDesc=new HTableDescriptor(TableNamevalueOf(tableName));

for(String columnFamily:columnFamilys){

tableDescaddFamily(new HColumnDescriptor(columnFamily));

}

admincreateTable(tableDesc);

Systemoutprintln("建表成功!");

}

adminclose();//关闭释放资源

}

/

删除一个表

@param tableName 删除的表名

/

public static void deleteTable(String tableName)throws Exception{

HBaseAdmin admin=new HBaseAdmin(conf);

if(admintableExists(tableName)){

admindisableTable(tableName);//禁用表

admindeleteTable(tableName);//删除表

Systemoutprintln("删除表成功!");

}else{

Systemoutprintln("删除的表不存在!");

}

adminclose();

}

/

插入一条数据

@param tableName 表明

@param columnFamily 列簇

@param column 列

@param value 值

/

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(BytestoBytes(rowkey));

putadd(BytestoBytes(columnFamily), BytestoBytes(column), BytestoBytes(value));

tableput(put);//放入表

tableclose();//释放资源

}

/

删除一条数据

@param tableName 表名

@param row rowkey行键

/

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

HTable table=new HTable(conf, tableName);

Delete delete=new Delete(BytestoBytes(row));

tabledelete(delete);

tableclose();

}

/

删除多条数据

@param tableName 表名

@param rows 行健集合

/

public static void deleteList(String tableName,String rows[])throws Exception{

HTable table=new HTable(conf, tableName);

List<Delete> list=new ArrayList<Delete>();

for(String row:rows){

Delete del=new Delete(BytestoBytes(row));

listadd(del);

}

tabledelete(list);

tableclose();//释放资源

}

/

获取一条数据,根据rowkey

@param tableName 表名

@param row 行健

/

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

HTable table=new HTable(conf, tableName);

Get get=new Get(BytestoBytes(row));

Result result=tableget(get);

printRecoder(result);//打印记录

tableclose();//释放资源

}

/

查看某个表下的所有数据

@param tableName 表名

/

public static void showAll(String tableName)throws Exception{

HTable table=new HTable(conf, tableName);

Scan scan=new Scan();

ResultScanner rs=tablegetScanner(scan);

for(Result r:rs){

printRecoder(r);//打印记录

}

tableclose();//释放资源

}

/

查看某个表下的所有数据

@param tableName 表名

@param rowKey 行健

/

public static void ScanPrefixByRowKey(String tableName,String rowKey)throws Exception{

HTable table=new HTable(conf, tableName);

Scan scan=new Scan();

scansetFilter(new PrefixFilter(BytestoBytes(rowKey)));

ResultScanner rs=tablegetScanner(scan);

for(Result r:rs){

printRecoder(r);//打印记录

}

tableclose();//释放资源

}

/

查看某个表下的所有数据

@param tableName 表名

@param rowKey 行健扫描

@param limit 限制返回数据量

/

public static void ScanPrefixByRowKeyAndLimit(String tableName,String rowKey,long limit)throws Exception{

HTable table=new HTable(conf, tableName);

Scan scan=new Scan();

scansetFilter(new PrefixFilter(BytestoBytes(rowKey)));

scansetFilter(new PageFilter(limit));

ResultScanner rs=tablegetScanner(scan);

for(Result r:rs){

printRecoder(r);//打印记录

}

tableclose();//释放资源

}

/

根据rowkey扫描一段范围

@param tableName 表名

@param startRow 开始的行健

@param stopRow 结束的行健

/

public void scanByStartAndStopRow(String tableName,String startRow,String stopRow)throws Exception{

HTable table=new HTable(conf, tableName);

Scan scan=new Scan();

scansetStartRow(BytestoBytes(startRow));

scansetStopRow(BytestoBytes(stopRow));

ResultScanner rs=tablegetScanner(scan);

for(Result r:rs){

printRecoder(r);

}

tableclose();//释放资源

}

/

扫描整个表里面具体的某个字段的值

@param tableName 表名

@param columnFalimy 列簇

@param column 列

/

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

HTable table=new HTable(conf, tableName);

Scan scan=new Scan();

ResultScanner rs=tablegetScanner(scan);

for(Result r:rs){

Systemoutprintln("值: " +new String(rgetValue(BytestoBytes(columnFalimy), BytestoBytes(column))));

}

tableclose();//释放资源

}

/

打印一条记录的详情

/

public static void printRecoder(Result result)throws Exception{

for(Cell cell:resultrawCells()){

Systemoutprint("行健: "+new String(CellUtilcloneRow(cell)));

Systemoutprint("列簇: "+new String(CellUtilcloneFamily(cell)));

Systemoutprint(" 列: "+new String(CellUtilcloneQualifier(cell)));

Systemoutprint(" 值: "+new String(CellUtilcloneValue(cell)));

Systemoutprintln("时间戳: "+cellgetTimestamp());

}

}

}

不支持,Hbase不支持普通的SQL语句,它可以使用HBase查询语言(HBase Query Language, HBaseQL)进行查询,HBaseQL可以用来与HBase交互,它是一种基于NoSQL的查询语言,它允许用户与HBase表进行交互,以实现常见的数据库 *** 作,如插入,更新和删除。HBaseQL并不是标准的SQL,而是基于HBase的NoSQL查询语言,它更像是一个替代 SQL的查询语言,但它的语法和概念类似于SQL,容易理解和使用。因此,HBase不支持使用普通的SQL语句来查询。

以上就是关于应使用什么技术来实现 HBase 的二级索引全部的内容,包括:应使用什么技术来实现 HBase 的二级索引、hbase支持普通的sql语句吗、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9651641.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存