#最近无论工作还是生活都弄的一团糟,可能是我太浮躁了,整天游戏人生浑浑噩噩的过。算了不说这些扫兴的事情了。
关于C#访问Thrift的文章,这应该是第三篇了,也是最后一篇了吧。Thrfit 最麻烦的应该是查询那部分,第二篇我也详细的说了一下,这一篇文章,说一下Thrift中使用很平凡的API(新建表,删除表,插入数据,更新数据,删除数据),最后发一下,为了方便使用Thrift写的一个Helper类。
void createTable(byte[] tableName, List<ColumnDescriptor> columnFamilies);
void mutateRow(byte[] tableName, byte[] row, List<Mutation> mutations, Dictionary<byte[], byte[]> attributes);
void mutateRowTs(byte[] tableName, byte[] row, List<Mutation> mutations, long timestamp, Dictionary<byte[], byte[]> attributes);
void mutateRows(byte[] tableName, List<BatchMutation> rowBatches, Dictionary<byte[], byte[]> attributes);
void mutateRowsTs(byte[] tableName, List<BatchMutation> rowBatches, long timestamp, Dictionary<byte[], byte[]> attributes);
void deleteTable(byte[] tableName);
void deleteAll(byte[] tableName, byte[] row, byte[] column, Dictionary<byte[], byte[]> attributes);
void deleteAllTs(byte[] tableName, byte[] row, byte[] column, long timestamp, Dictionary<byte[], byte[]> attributes);
void deleteAllRow(byte[] tableName, byte[] row, Dictionary<byte[], byte[]> attributes);
void deleteAllRowTs(byte[] tableName, byte[] row, long timestamp, Dictionary<byte[], byte[]> attributes);
值得注意的是,Thrift 插入行和更新行使用的同一函数(mutateRow等一类函数),使用过HBaseAPI的童鞋,这点不足为奇。这几个API都比较简单,下面我就直接贴出Helper类及简单的测试类。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThriftHelper;
using IThrift;
using Thrift.Transport;
using Thrift.Protocol;
namespace Test
{
class Program
{
/*
* 表名: HTest
* 列簇: i
* 子列: Data
*/
static void Main(string[] args)
{
#region Test
Helper.Open();
Printer("CreateTable:");
ColumnDescriptor _cd = new ColumnDescriptor();
_cd.Name = Encoding.UTF8.GetBytes("i");
if (Helper.CreateTable("ITest", new List<ColumnDescriptor> { _cd }))
Printer("CreateTable is Success");
else
Printer("CreateTable Occurred Error");
Printer("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
Printer("MutateRowHBase:");
Mutation _mutation = new Mutation();
_mutation.Column = Encoding.UTF8.GetBytes("i:one");
_mutation.Value = Encoding.UTF8.GetBytes("1");
if (Helper.MutateRowHBase("ITest", "001", new List<Mutation> { _mutation }))
Printer("MutateRowHBase is Success");
else
Printer("MutateRowHBase Occurred Error");
Printer("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
Printer("GetDataFromHBase:");
List<TRowResult> _result = Helper.GetDataFromHBase("ITest", "001");
Printer(_result);
Printer("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
Printer("MutateRowHBase:");
_mutation = new Mutation();
_mutation.Column = Encoding.UTF8.GetBytes("i:one");
_mutation.Value = Encoding.UTF8.GetBytes("-1");
if (Helper.MutateRowHBase("ITest", "001", new List<Mutation> { _mutation }))
Printer("MutateRowHBase is Success");
else
Printer("MutateRowHBase Occurred Error");
Printer("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
Printer("GetDataFromHBase:");
_result = Helper.GetDataFromHBase("ITest", "001");
Printer(_result);
Printer("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
Printer("DeleteRow:");
if (Helper.DeleteAllRow("ITest", "001"))
Printer("DeleteAllRow is Success");
else
Printer("DeleteAllRow Occurred Error");
Helper.Close();
#endregion
Console.ReadKey();
}
static void Printer(List<TRowResult> reslut)
{
if (reslut.Count == 0)
return;
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 Printer(string conent)
{
Console.Write(conent);
}
}
}
Helper类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Thrift;
using IThrift;
using Thrift.Transport;
using Thrift.Protocol;
namespace ThriftHelper
{
public static class Helper
{
static TTransport transport = new TSocket("192.168.2.200", 9090);
static TProtocol tProtocol = new TBinaryProtocol(transport);
static Hbase.Client client = new Hbase.Client(tProtocol);
public static void Open()
{
transport.Open();
}
public static void Close()
{
transport.Close();
}
/// <summary>
/// 通过rowkey获取数据
/// </summary>
/// <param name="tablename"></param>
/// <param name="rowkey"></param>
public static List<TRowResult> GetDataFromHBase(string tablename, string rowkey)
{
List<TRowResult> reslut = client.getRow(Encoding.UTF8.GetBytes(tablename), Encoding.UTF8.GetBytes(rowkey), null);
return reslut;
}
/// <summary>
/// 通过Rowkey前缀Fliter
/// </summary>
/// <param name="tablename"></param>
/// <param name="startrowkey"></param>
/// <param name="endrowkey"></param>
public static List<TRowResult> GetDataFromHBaseThroughRowKeyPrefix(string tablename, string Prefixrowkey,List<string> _cols)
{
List<byte[]> _bytes = new List<byte[]>();
foreach (string str in _cols)
_bytes.Add(Encoding.UTF8.GetBytes(str));
int ScannerID = client.scannerOpenWithPrefix(Encoding.UTF8.GetBytes(tablename), Encoding.UTF8.GetBytes(Prefixrowkey),
_bytes, null);
/*
* scannerGetList(string ID),源码中其实调用scannerGetList(string ID,int nbRow)方法,nbRow传值为1
*/
List<TRowResult> reslut = client.scannerGetList(ScannerID, 100);
return reslut;
}
/// <summary>
/// 通过RowKey的范围获取数据
/// </summary>
/// <param name="tablename"></param>
/// <param name="stRowkey"></param>
/// <param name="?"></param>
/// <remarks>结果集包含StartRowKey列值,不包含EndRowKey的列值</remarks>
public static List<TRowResult> GetDataFromHBaseThroughRowKeyRange(string tablename,
string stRowkey, string endRowkey,List<string> _cols)
{
List<byte[]> _bytes = new List<byte[]>();
foreach (string str in _cols)
_bytes.Add(Encoding.UTF8.GetBytes(str));
int ScannerID = client.scannerOpenWithStop(Encoding.UTF8.GetBytes(tablename),
Encoding.UTF8.GetBytes(stRowkey), Encoding.UTF8.GetBytes(endRowkey),
_bytes, null);
List<TRowResult> reslut = client.scannerGetList(ScannerID, 100);
return reslut;
}
/// <summary>
/// 通过Filter进行数据的Scanner
/// </summary>
/// <param name="tablename"></param>
/// <param name="filterString"></param>
public static List<TRowResult> 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;
int ScannerID = client.scannerOpenWithScan(Encoding.UTF8.GetBytes(tablename), _scan, null);
List<TRowResult> reslut = client.scannerGetList(ScannerID, 100);
return reslut;
}
public static bool MutateRowHBase(string tablename, string rowkey, List<Mutation> _mutations)
{
try
{
client.mutateRow(Encoding.UTF8.GetBytes(tablename), Encoding.UTF8.GetBytes(rowkey), _mutations, null);
return true;
}
catch (Exception e)
{
return false;
}
}
public static bool MutateRowsHBase(string tablename, List<BatchMutation> _BatchMutation)
{
try
{
client.mutateRows(Encoding.UTF8.GetBytes(tablename), _BatchMutation, null);
return true;
}
catch (Exception e)
{
return false;
}
}
public static bool DeleteRowHBase(string tablename, string rowkey, string column)
{
try
{
client.deleteAll(Encoding.UTF8.GetBytes(tablename), Encoding.UTF8.GetBytes(rowkey),
Encoding.UTF8.GetBytes(column), null);
return true;
}
catch (Exception e)
{
return false;
}
}
public static bool DeleteAllRow(string tablename, string rowkey)
{
try
{
client.deleteAllRow(Encoding.UTF8.GetBytes(tablename), Encoding.UTF8.GetBytes(rowkey), null);
return true;
}
catch (Exception e)
{
return false;
}
}
public static bool DeleteTable(string tablename)
{
try
{
client.deleteTable(Encoding.UTF8.GetBytes(tablename));
return true;
}
catch (Exception e)
{
return false;
}
}
public static bool CreateTable(string tablename, List<ColumnDescriptor> _cols)
{
try
{
client.createTable(Encoding.UTF8.GetBytes(tablename), _cols);
return true;
}
catch (Exception e)
{
return false;
}
}
}
}
好了,关于Thrift的基本操作就写到这,当然Thrift也支持Hbase中比较高级的操作,在以后的博客会不断更新。谢谢大家,个人水平有限,不足之处请谅解。