h base programming

20
HBase Programming Mani [email protected]

Upload: manigandan-muthusamy

Post on 11-Nov-2014

724 views

Category:

Technology


0 download

DESCRIPTION

HBase tutorial to get started

TRANSCRIPT

Page 1: H base programming

HBase Programming

Mani [email protected]

Page 2: H base programming

CreateTablepublic static int VERSION = 5;

public static void createTable(byte[] tableName, byte[][] families,int numVersions) throws IOException {

HBaseConfiguration hbaseConfig = new HBaseConfiguration();HBaseAdmin hbaseAdmin = new HBaseAdmin(hbaseConfig);HTableDescriptor htableDesc = new HTableDescriptor(tableName);

for (byte[] family : families) {HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(family,

numVersions, HColumnDescriptor.DEFAULT_COMPRESSION,

HColumnDescriptor.DEFAULT_IN_MEMORY,HColumnDescriptor.DEFAULT_BLOCKCACHE,HColumnDescriptor.DEFAULT_TTL,HColumnDescriptor.DEFAULT_BLOOMFILTER);

htableDesc.addFamily(hColumnDescriptor);}hbaseAdmin.createTable(htableDesc);

}

Page 3: H base programming

Table Exists & Get Table Namepublic static boolean tableExist(byte[] tableName) throws IOException {

HBaseConfiguration hbaseConfig = new HBaseConfiguration();HBaseAdmin hbaseAdmin = new HBaseAdmin(hbaseConfig);return hbaseAdmin.tableExists(tableName);

}public static List<String> getTableNames() throws IOException {

HBaseConfiguration hbaseConfig = new HBaseConfiguration();HBaseAdmin hbaseAdmin = new HBaseAdmin(hbaseConfig);

List<String> tables = new ArrayList<String>();for (HTableDescriptor htableDesc : hbaseAdmin.listTables()) {

printTableDescriptors(htableDesc);tables.add(htableDesc.getNameAsString());

}return tables;

}public static void printTableDescriptors(HTableDescriptor descriptor) {

System.out.println("Table name " + descriptor.getNameAsString());for (HColumnDescriptor columnDescriptor :

descriptor.getColumnFamilies()) {System.out.println(columnDescriptor.getNameAsString());System.out.println(columnDescriptor.getMaxVersions());System.out.println(columnDescriptor.getMinVersions());

}}

Page 4: H base programming

Insert Recordpublic static void insertRecord(HTable htable, byte[] columnFamily,

boolean createColumnFamily,HashMap<String, HashMap<String, String>> creditLogMaps) throws

IOException {String rowKey;// record keyHashMap<String, String> columnData;if (!columnFamilyExist(htable, columnFamily)) {

if (!createColumnFamily)return;

elsecreateColumnFamily(htable, columnFamily,

VERSION);}for (Map.Entry<String, HashMap<String, String>> creditLogMap :

creditLogMaps.entrySet()) {rowKey = creditLogMap.getKey();columnData = creditLogMap.getValue();Put put = new Put(rowKey.getBytes());for (Map.Entry<String, String> kv :

columnData.entrySet()) {put.add(columnFamily, kv.getKey().getBytes(),

kv.getValue().getBytes());

}htable.put(put);System.out.println("Record inserted");

}}

Page 5: H base programming

Create Column Familypublic static void createColumnFamily(HTable htable,

byte[] columnFamily,int numVersions) throws IOException {

disableTable(htable.getTableName());

while (isTableEnabled(htable.getTableName()));// wait untill table is disabled

HBaseConfiguration hbaseConfig = new HBaseConfiguration();HBaseAdmin hbaseAdmin = new HBaseAdmin(hbaseConfig);

HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(columnFamily, numVersions,HColumnDescriptor.DEFAULT_COMPRESSION,HColumnDescriptor.DEFAULT_IN_MEMORY,HColumnDescriptor.DEFAULT_BLOCKCACHE,HColumnDescriptor.DEFAULT_TTL,HColumnDescriptor.DEFAULT_BLOOMFILTER);

hbaseAdmin.addColumn(htable.getTableName(), hColumnDescriptor);

enableTable(htable.getTableName());}

Page 6: H base programming

Get Column Familypublic static List<String> getColumnFamilies(HTable htable) throws IOException {

List<String> columnFamilies = new ArrayList<String>();for (HColumnDescriptor columnDesc :

htable.getTableDescriptor().getColumnFamilies()) {columnFamilies.add(columnDesc.getNameAsString());

}return columnFamilies;

}

public static boolean columnFamilyExist(HTable htable, byte[] columnFamily) throws IOException {

boolean hasColumn = false;if (htable.getTableDescriptor().getFamily(columnFamily) != null) {

hasColumn = true;}return hasColumn;

}

public static boolean isTableEnabled(byte[] tableName) throws IOException {HBaseConfiguration hbaseConfig = new HBaseConfiguration();HBaseAdmin hbaseAdmin = new HBaseAdmin(hbaseConfig);System.out.println("Table enabled " +

hbaseAdmin.isTableEnabled(tableName));return hbaseAdmin.isTableEnabled(tableName);

}

Page 7: H base programming

Enable, Disable & Drop Tablepublic static void enableTable(byte[] tableName) throws IOException {

HBaseConfiguration hbaseConfig = new HBaseConfiguration();HBaseAdmin hbaseAdmin = new HBaseAdmin(hbaseConfig);hbaseAdmin.enableTable(tableName);

}

public static void disableTable(byte[] tableName) throws IOException {HBaseConfiguration hbaseConfig = new HBaseConfiguration();HBaseAdmin hbaseAdmin = new HBaseAdmin(hbaseConfig);hbaseAdmin.disableTable(tableName);System.out.println("Table " + new String(tableName) + " disabled");

}

public static void dropTable(byte[] tableName) throws IOException {HBaseConfiguration hbaseConfig = new HBaseConfiguration();HBaseAdmin hbaseAdmin = new HBaseAdmin(hbaseConfig);hbaseAdmin.deleteTable(tableName);System.out.println("Table " + new String(tableName) + " deleted");

}

Page 8: H base programming

Get Table, Get & Delete Record

public static HTable getTable(byte[] tableName) throws IOException {HBaseConfiguration hbaseConfig = new HBaseConfiguration();HBaseAdmin hbaseAdmin = new HBaseAdmin(hbaseConfig);return new HTable(hbaseConfig, tableName);

}

public static void deleteRecord(HTable hTable, String rowKey)throws IOException {

Delete delete = new Delete(rowKey.getBytes());hTable.delete(delete);System.out.println("Record " + rowKey + " deleted ");

}

public static PResultSet getRecord(HTable hTable, String rowKey,String family) throws IOException {

Get get = new Get(rowKey.getBytes());get.addFamily(family.getBytes());PResultSet resultSet = new PResultSet(hTable, get);return resultSet;

}

Page 9: H base programming

Scan Records• public static void scanRecords(byte[] startKey, HTable hTable)• throws IOException {• Scan scan = new Scan(startKey);• ResultScanner resultScanner = hTable.getScanner(scan);

• System.out.println("#########Scan result ################");• for (Result result : resultScanner) {• System.out.println(">key is " + new

String(result.getRow()));• for (KeyValue kv : result.raw()) {• System.out.println(new

String(kv.getFamily()) + ":"• + new

String(kv.getQualifier()) + " = "• + new

String(kv.getValue()));• }• }• System.out.println("--------Scan result ends

here-------------------");• }

Page 10: H base programming

Scan Records by Filterpublic static void scanRecords(byte[] startKey, HTable hTable, TestFilter testFilter) throws IOException {

Scan scan = new Scan(startKey);

scan.setFilter(testFilter.getFilters());ResultScanner resultScanner = hTable.getScanner(scan);System.out.println("#########filter scan result ################");for (Result result : resultScanner) {

System.out.println(">key is " + new String(result.getRow()));

for (KeyValue kv : result.raw()) {System.out.println(new String(kv.getFamily()) +

":"+ new

String(kv.getQualifier()) + " = "+ new

String(kv.getValue()));}

}System.out.println("--------Scan result ends

here-------------------");}

Page 11: H base programming

Filter Object

class FilterObject {String columnFamilyName;String columnName;String value;CompareOp compareOp;

public FilterObject(String columnFamilyName, String columnName,String value, CompareOp compareOp) {

this.columnFamilyName = columnFamilyName;this.columnName = columnName;this.value = value;this.compareOp = compareOp;

}}

Page 12: H base programming

Filter Classclass TestFilter {

private List<FilterObject> filterObjects;private FilterList filterList;

public TestFilter() {filterObjects = new ArrayList<FilterObject>();filterList = new FilterList();

}

public void addFilterObject(FilterObject ft) {filterObjects.add(ft);

}

public FilterList getFilters() {for (FilterObject filterObject : filterObjects) {

filterList.addFilter(new SingleColumnValueFilter(filterObject.columnFamilyName.getBytes(),filterObject.columnName.getBytes(), filterObject.compareOp,filterObject.value.getBytes()));

}return filterList;

}

}

Page 13: H base programming

ResultSetclass PResultSet {

private String tableName;private String columnFamily;private HashMap<String, String> resutlMap;private Result rs;

public PResultSet(HTable hTable, Get get) throws IOException {this.rs = hTable.get(get);this.tableName = hTable.getTableDescriptor().getNameAsString();this.columnFamily = hTable.getTableDescriptor().getColumnFamilies()

.toString();this.resutlMap = new HashMap<String, String>();

}

public String getTableName() {return tableName;

}

public void setTableName(String tableName) {this.tableName = tableName;

}

Cont…

Page 14: H base programming

ResultSet cont….public String getColumnFamily() {

return columnFamily;}public void setColumnFamily(String columnFamily) {

this.columnFamily = columnFamily;}public HashMap<String, String> getResutlMap() {

return resutlMap;}public void setResutlMap(HashMap<String, String> resutlMap) {

this.resutlMap = resutlMap;}public String toString() {

System.out.println("TableName :" + getTableName());System.out.println("ColumnFamily : " + getColumnFamily());System.out.println("No Of Rows : " + rs.size());for (KeyValue kv : rs.raw()) {

resutlMap.put(Bytes.toString(kv.getQualifier()),Bytes.toString(kv.getValue()));

}return resutlMap.toString();

}}

Page 15: H base programming

Testing Code – Create FunctionsCreate Table Testing:

String tableName = "TrainingDB";String familyName = "CreditLog";

byte[] tableNameBytes = tableName.getBytes();byte[] familyBytes = familyName.getBytes();

byte[][] columnFamilyByteArray = new byte[][] { familyBytes };

if (tableExist(tableNameBytes)) {System.out.println("Table exist");disableTable(tableNameBytes);dropTable(tableNameBytes);

}

createTable(tableNameBytes, columnFamilyByteArray, 5);HTable hTable = getTable(tableNameBytes);System.out.println("Successfully created");System.out.println("Column exist : "

+ columnFamilyExist(hTable, familyBytes));

Page 16: H base programming

Testing Code – PutHashMap<String, HashMap<String, String>> creditLogMaps

= new HashMap<String, HashMap<String, String>>();HashMap<String, String> creditLogMap = new HashMap<String,

String>();

creditLogMap.put("Name", "Karthik");creditLogMap.put("Age", "36");creditLogMap.put("Rating", "Good");creditLogMap.put("Limit", "404$");

String rowKey = "1753-4343-4322-5423";creditLogMaps.put(rowKey, creditLogMap);

creditLogMap = new HashMap<String, String>();

creditLogMap.put("Name", "Manik");creditLogMap.put("Age", "36");creditLogMap.put("Rating", "Average");creditLogMap.put("Limit", "-2$");

String rowKey2 = "5557-4343-4322-5422";

creditLogMaps.put(rowKey2, creditLogMap);

insertRecord(hTable, familyBytes, false, creditLogMaps);

Page 17: H base programming

Testing Code - PutHashMap<String, String> transLogMap = new HashMap<String, String>();

transLogMap.put("Date", "23-NOV-2011");transLogMap.put("Amount", "$30");transLogMap.put("Balance", "$450");transLogMap.put("Bank", "Barclays");

HashMap<String, HashMap<String, String>> transLogMaps = new HashMap<String, HashMap<String, String>>();

transLogMaps.put(rowKey, transLogMap);insertRecord(hTable, "TransLog".getBytes(), true, transLogMaps);

Page 18: H base programming

Testing Code – List TablesSystem.out.println("Tables in HBase");

for (String name : getTableNames()) {System.out.println("# " + name);System.out.println("Table columns");HTable table = getTable(name.getBytes());for (String columnName : getColumnFamilies(table)) {

System.out.println("- " + columnName);}

}

Page 19: H base programming

Testing Code – Get & Scan RecordsSystem.out.println(getRecord(hTable, rowKey,

familyName).toString());System.out.println(getRecord(hTable, rowKey,

"TransLog").toString());

scanRecords(rowKey.getBytes(), hTable);

FilterObject filterObject = new FilterObject(familyName, "Age", "36",

CompareOp.EQUAL);FilterObject filterObject2 = new FilterObject(familyName, "Limit",

"-2$", CompareOp.EQUAL);

TestFilter testFilter = new TestFilter();testFilter.addFilterObject(filterObject);testFilter.addFilterObject(filterObject2);

scanRecords(rowKey.getBytes(), hTable, testFilter);

deleteRecord(hTable, rowKey);

System.out.println("After delete");scanRecords(rowKey.getBytes(), hTable);

Page 20: H base programming

Happy Coding

– Mani– [email protected]