第一篇(查看)已经介绍了关于通过Thrift访问HBase基本使用,这一篇说一些关于通过Thrift对HBase进行查询的操作。通过HBase.Thrift 所生成的接口类中,其中Hbase.cs为核心类,所有客户端操作HBase的接口的定义和实现都在此类中,如果想查看Thrift服务端的代码,请参考HBase源代码。
以下是类中所定义的查询接口:
List<TRowResult> getRow(byte[] tableName, byte[] row, Dictionary<byte[], byte[]> attributes);
List<TRowResult> getRowWithColumns(byte[] tableName, byte[] row, List<byte[]> columns, Dictionary<byte[], byte[]> attributes);
List<TRowResult> getRowTs(byte[] tableName, byte[] row, long timestamp, Dictionary<byte[], byte[]> attributes);
List<TRowResult> getRowWithColumnsTs(byte[] tableName, byte[] row, List<byte[]> columns, long timestamp, Dictionary<byte[], byte[]> attributes);
List<TRowResult> getRows(byte[] tableName, List<byte[]> rows, Dictionary<byte[], byte[]> attributes);
List<TRowResult> getRowsWithColumns(byte[] tableName, List<byte[]> rows, List<byte[]> columns, Dictionary<byte[], byte[]> attributes);
List<TRowResult> getRowsTs(byte[] tableName, List<byte[]> rows, long timestamp, Dictionary<byte[], byte[]> attributes);
List<TRowResult> getRowsWithColumnsTs(byte[] tableName, List<byte[]> rows, List<byte[]> columns, long timestamp, Dictionary<byte[], byte[]> attributes);
int scannerOpenWithScan(byte[] tableName, TScan scan, Dictionary<byte[], byte[]> attributes);
int scannerOpen(byte[] tableName, byte[] startRow, List<byte[]> columns, Dictionary<byte[], byte[]> attributes);
int scannerOpenWithStop(byte[] tableName, byte[] startRow, byte[] stopRow, List<byte[]> columns, Dictionary<byte[], byte[]> attributes);
int scannerOpenWithPrefix(byte[] tableName, byte[] startAndPrefix, List<byte[]> columns, Dictionary<byte[], byte[]> attributes);
int scannerOpenTs(byte[] tableName, byte[] startRow, List<byte[]> columns, long timestamp, Dictionary<byte[], byte[]> attributes);
int scannerOpenWithStopTs(byte[] tableName, byte[] startRow, byte[] stopRow, List<byte[]> columns, long timestamp, Dictionary<byte[], byte[]> attributes);
List<TRowResult> scannerGet(int id);
List<TRowResult> scannerGetList(int id, int nbRows);
void scannerClose(int id);
结合项目的应用介绍几个比较常用的接口(其实只要用过HBaseAPI的童鞋,上面这些接口就不在话下了):
1.getRow(这类查询简单,通过rowkey获取数据,接口的大部分参数类型为字节数组)
结果:
2.scannerOpenWithStop(通过RowKey的范围获取数据)
/// <summary>
/// 通过RowKey的范围获取数据
/// </summary>
/// <param name="tablename"></param>
/// <param name="stRowkey"></param>
/// <param name="?"></param>
/// <remarks>结果集包含StartRowKey列值,不包含EndRowKey的列值</remarks>
static void GetDataFromHBaseThroughRowKeyRange(string tablename,
string stRowkey,string endRowkey)
{
transport.Open();
int ScannerID = client.scannerOpenWithStop(Encoding.UTF8.GetBytes(tablename),
Encoding.UTF8.GetBytes(stRowkey), Encoding.UTF8.GetBytes(endRowkey),
new List<byte[]> { Encoding.UTF8.GetBytes("i:Data") }, null);
List<TRowResult> reslut = client.scannerGetList(ScannerID, 100);
foreach (var key in reslut)
{
Console.WriteLine(Encoding.UTF8.GetString(key.Row));
foreach (var k in key.Columns)
{
Console.Write(Encoding.UTF8.GetString(k.Key) + "\t");
Console.WriteLine(Encoding.UTF8.GetString(k.Value.Value));
Console.WriteLine("++++++++++++++++++++++++++++++++++++++");
}
}
}
//调用
static void Main(string[] args)
{
GetDataFromHBaseThroughRowKeyRange("HStudy", "001", "006");
}
结果:
3.scannerOpenWithPrefix(通过RowKey的前缀进行匹配查询)
/// <summary>
/// 通过Rowkey前缀Fliter
/// </summary>
/// <param name="tablename"></param>
/// <param name="startrowkey"></param>
/// <param name="endrowkey"></param>
static void GetDataFromHBaseThroughRowKeyPrefix(string tablename, string Prefixrowkey)
{
transport.Open();
int ScannerID = client.scannerOpenWithPrefix(Encoding.UTF8.GetBytes(tablename), Encoding.UTF8.GetBytes(Prefixrowkey), new List<byte[]> { Encoding.UTF8.GetBytes("i:Data") }, null);
/*
* scannerGetList(string ID),源码中其实调用scannerGetList(string ID,int nbRow)方法,nbRow传值为1
*/
List<TRowResult> reslut = client.scannerGetList(ScannerID,100);
foreach (var key in reslut)
{
Console.WriteLine(Encoding.UTF8.GetString(key.Row));
foreach (var k in key.Columns)
{
Console.Write(Encoding.UTF8.GetString(k.Key) + "\t");
Console.WriteLine(Encoding.UTF8.GetString(k.Value.Value));
Console.WriteLine("++++++++++++++++++++++++++++++++++++++");
}
}
}
//调用
static void Main(string[] args)
{
GetDataFromHBaseThroughRowKeyPrefix("HStudy", "00");
}
4.scannerOpenWithScan(通过过滤器进行查询)
这个接口是所有查询接口中最麻烦的一个吧,因为它用到了过滤器,也就是HBaseAPI中的Filter。这个接口的参数中有一个参数类型为TScan,基本结构如下:
public partial class TScan : TBase
{
private byte[] _startRow;
private byte[] _stopRow;
private long _timestamp;
private List<byte[]> _columns;
private int _caching;
private byte[] _filterString;
}
前面的几个参数不多说,这里说一下_filterString (关于HaseAPI中各种Filter这里就不多说),以常见的SingleColumnValueFilter为例,如果我想定义一个查询PatientName为小红的一个过滤器:
stringfilterString = "SingleColumnValueFilter('s','PatientName',=,'substring:小红')";
byte[]_filterString = Encoding.UTF8.GetBytes(filterString);
如果要定义多个过滤器,过滤器之间用‘AND’连接。
/// <summary>
/// 通过Filter进行数据的Scanner
/// </summary>
/// <param name="tablename"></param>
/// <param name="filterString"></param>
static void GetDataFromHBaseThroughFilter(string tablename, string filterString,List<byte[]> _cols)
{
TScan _scan = new TScan();
//SingleColumnValueFilter('i', 'Data', =, '2')
_scan.FilterString =Encoding.UTF8.GetBytes(filterString);
_scan.Columns = _cols;
transport.Open();
int ScannerID = client.scannerOpenWithScan(Encoding.UTF8.GetBytes(tablename), _scan,null);
List<TRowResult> reslut = client.scannerGetList(ScannerID, 100);
foreach (var key in reslut)
{
Console.WriteLine(Encoding.UTF8.GetString(key.Row));
foreach (var k in key.Columns)
{
Console.Write(Encoding.UTF8.GetString(k.Key) + "\t");
Console.WriteLine(Encoding.UTF8.GetString(k.Value.Value));
Console.WriteLine("++++++++++++++++++++++++++++++++++++++");
}
}
}
static void Main(string[] args)
{
GetDataFromHBaseThroughRowKeyRange("HImages", "123.456.1", "123.456.9");
List<byte[]> _byte = new List<byte[]>();
_byte.Add(Encoding.UTF8.GetBytes("s:PatientName"));
_byte.Add(Encoding.UTF8.GetBytes("s:StudyInstanceUID"));
_byte.Add(Encoding.UTF8.GetBytes("s:PatientSex"));
string filterString = "((SingleColumnValueFilter('s','PatientName',=,'substring:Jim')) AND (SingleColumnValueFilter('s','PatientSex',=,'substring:10')))";
string filterString = "SingleColumnValueFilter('s','PatientName',=,'substring:小红')";
GetDataFromHBaseThroughFilter("HStudy", filterString, _byte);
Console.ReadLine();
}
关于通过Thrift查询HBase就说到这。