using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HBaseTest
{
class Test
{
private readonly AbstractHBaseThriftService thriftService;
public Test(String host, int port)
{
thriftService =new HBaseThriftService(host, port);
thriftService.Open();
}
public Test() : this("master", 9090)
{
}
static String RandomlyBirthday()
{
Random r =new Random();
int year =1900+ r.Next(100);
int month =1+ r.Next(12);
int date=1+ r.Next(30);
return year +"-"+ month.ToString().PadLeft(2, '0') +"-"+date.ToString().PadLeft(2, '0');
}
static String RandomlyGender()
{
Random r =new Random();
int flag = r.Next(2);
return flag ==0"M" : "F";
}
static String RandomlyUserType()
{
Random r =new Random();
int flag =1+ r.Next(10);
return flag.ToString();
}
publicvoid Close()
{
thriftService.Close();
}
publicvoid CaseForGetTables()
{
var tableNameList = thriftService.GetTables();
foreach (var existedTableName in tableNameList)
{
Console.WriteLine("Table:{0}", existedTableName);
}
}
publicvoid CaseForCreateTable(
string tableName,
IList<string> columnNameList)
{
thriftService.CreateTable(tableName, columnNameList);
}
publicvoid CaseForUpdate()
{
bool writeToWal =false;
Dictionary<String, String> attributes =new Dictionary<String, String>(0);
string table = SetTable();
// put kv pairs
System.Diagnostics.Stopwatch watch =new System.Diagnostics.Stopwatch();
watch.Start();
for (long i =0; i <50000; i++)
{
string rowKey = i.ToString().PadLeft(4, '0');
Dictionary<String, String> fieldNameva lues =new Dictionary<String, String>();
fieldNameva lues.Add("info:name", RandomNameGen.getRandomName());
fieldNameva lues.Add("info:birthday", RandomlyBirthday());
fieldNameva lues.Add("info:user_type", RandomlyUserType());
fieldNameva lues.Add("info:gender", RandomlyGender());
thriftService.Update(table, rowKey, writeToWal, fieldNameva lues, attributes);
if (i %10000==0)
{
Console.WriteLine(
"Insert [{0}] Rows Time Eclipsed:{1}",
i,
watch.ElapsedMilliseconds);
}
}
watch.Stop();
Console.WriteLine(
"Insert [{0}] Rows Time Eclipsed:{1}",
50000,
watch.ElapsedMilliseconds);
}
publicvoid CaseForDeleteCells()
{
bool writeToWal =false;
Dictionary<String, String> attributes =new Dictionary<String, String>(0);
String table = SetTable();
// put kv pairs
for (long i =5; i <10; i++)
{
String rowKey = i.ToString().PadLeft(4, '0');
List<String> columns =newList<String>(0);
columns.Add("info:birthday");
thriftService.DeleteCells(table, rowKey, writeToWal, columns, attributes);
}
}
publicvoid CaseForDeleteRow()
{
Dictionary<String, String> attributes =new Dictionary<String, String>(0);
String table = SetTable();
// delete rows
for (long i =5; i <10; i++)
{
String rowKey = i.ToString().PadLeft(4, '0');
thriftService.DeleteRow(table, rowKey, attributes);
}
}
publicvoid CaseForScan()
{
Dictionary<String, String> attributes =new Dictionary<String, String>(0);
String table = SetTable();
String startRow ="0001";
String stopRow ="0015";
List<String> columns =newList<String>(0);
columns.Add("info:name");
int id = thriftService.ScannerOpen(table, startRow, stopRow, columns, attributes);
int nbRows =2;
List<TRowResult> results = thriftService.ScannerGetList(id, nbRows);
while (results.Count >0)
{
foreach (TRowResult result in results)
{
thriftService.IterateResults(result);
}
results = thriftService.ScannerGetList(id, nbRows);
}
thriftService.ScannerClose(id);
}
publicvoid CaseForGet()
{
Dictionary<String, String> attributes =new Dictionary<String, String>(0);
String table = SetTable();
List<String>rows=newList<String>(0);
rows.Add("0009");
rows.Add("0098");
rows.Add("0999");
List<String> columns =newList<String>(0);
columns.Add("info:birthday");
columns.Add("info:gender");
List<TRowResult> results = thriftService.GetRowsWithColumns(table, rows, columns, attributes);
foreach (TRowResult result in results)
{
thriftService.IterateResults(result);
}
}
privatestring SetTable()
{
string table ="test-user";
return table;
}
}
}
Program 控制台程序启动类
准备好测试用例后,控制台程序的入口处调用即可:
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespaceHBaseTest
{classProgram
{staticvoid Main(string[] args)
{
Test test = new Test();
test.CaseForGetTables();
test.CaseForCreateTable("test-user",new[] { "info" });
test.CaseForGetTables();
test.CaseForUpdate();
test.CaseForGet();
test.CaseForScan();
test.CaseForDeleteCells();
test.CaseForDeleteRow();
Console.ReadLine();
}
}
}