Created
April 10, 2015 08:29
-
-
Save SaschaDittmann/a80c1606a2cb9b353218 to your computer and use it in GitHub Desktop.
Apache HBase mit Microsoft Azure HDInsight (Teil 1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void AddPersonToSet(CellSet set, Person person) | |
{ | |
var row = new CellSet.Row | |
{ | |
key = Encoding.UTF8.GetBytes(person.Id.ToString().PadLeft(20)) | |
}; | |
var value = new Cell | |
{ | |
column = Encoding.UTF8.GetBytes("d:firstname"), | |
data = Encoding.UTF8.GetBytes(person.FirstName) | |
}; | |
row.values.Add(value); | |
value = new Cell | |
{ | |
column = Encoding.UTF8.GetBytes("d:lastname"), | |
data = Encoding.UTF8.GetBytes(person.LastName) | |
}; | |
row.values.Add(value); | |
set.rows.Add(row); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const string ClusterUri = "https://<ClusterName>.azurehdinsight.net/"; | |
const string HadoopUsername = ""; // the default name is "admin" | |
const string HadoopPassword = ""; | |
static HBaseClient _client; | |
static void Main(string[] args) | |
{ | |
var clusterCredentials = new ClusterCredentials( | |
new Uri(ClusterUri), | |
HadoopUsername, | |
HadoopPassword) | |
_client = new HBaseClient(clusterCredentials); | |
// ... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const string HbaseTablename = "people"; | |
public void CreateTable() | |
{ | |
if (!_client.ListTables().name.Contains(HbaseTablename)) | |
{ | |
var tableSchema = new TableSchema {name = HbaseTablename}; | |
tableSchema.columns.Add(new ColumnSchema { name = "d" }); | |
_client.CreateTable(tableSchema); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person | |
{ | |
public int Id { get; set; } | |
public String FirstName { get; set; } | |
public String LastName { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void ReadRange(string startKey, string endKey) | |
{ | |
var scanSettings = new Scanner | |
{ | |
batch = 50, //Menge der Datensätze pro ScannerGetNext-Aufruf | |
startRow = Encoding.UTF8.GetBytes(startKey.PadLeft(20)), | |
endRow = Encoding.UTF8.GetBytes(endKey.PadLeft(20)) | |
}; | |
var scannerInfo = _client.CreateScanner(HbaseTablename, scanSettings); | |
CellSet next; | |
while ((next = _client.ScannerGetNext(scannerInfo)) != null) | |
{ | |
foreach (var row in next.rows) | |
{ | |
// row-Objekt verarbeiten | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void ReadSingle(string key) | |
{ | |
var cells = _client.GetCells(HbaseTablename, key.PadLeft(20)); | |
foreach (var row in cells.rows) | |
{ | |
// row-Objekt verarbeiten | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void WriteData() | |
{ | |
var set = new CellSet(); | |
for (var i = 0; i < 1000000; i++) | |
{ | |
AddPersonToSet(set, new Person | |
{ | |
Id = i, | |
FirstName = String.Format("FirstName {0}", i), | |
LastName = String.Format("LastName {0}", i), | |
}); | |
} | |
_client.StoreCells(HbaseTablename, set); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment