Book Image

Learning HBase

By : Shashwat Shriparv
Book Image

Learning HBase

By: Shashwat Shriparv

Overview of this book

Table of Contents (18 chapters)
Learning HBase
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Data operation code


Data operation code is all about adding a record, deleting a record, and getting a single record. It will also help in scanning the table and listing all records.

Let's see how we can build our own function to perform various tasks:

public void addOneRecordToTable(String tableObjName, String rowKey,
String colFamName, String columnName, String data) throws Exception
{
  try
  {
    HTable tableObj = new HTable(configurationObj, tableObjName);
    Put put = new Put(Bytes.toBytes(rowKey));
    put.add(Bytes.toBytes(colFamName), Bytes.toBytes(columnName), Bytes
    .toBytes(data));
    tableObj.put(put);
  }
  catch (IOException exception)
  {
    exception.printStackTrace();
  }
}
public  void delRecordFromTable(String tableObjName, String rowKey) throws IOException
{
  HTable tableObj = new HTable(configurationObj, tableObjName);
  List<Delete>list = new ArrayList<Delete>();
  Delete delObj = new Delete(rowKey.getBytes());
  list.add(delObj);
  tableObj.delete...