HBase10.1 Basic Operations (Java code)

Source: Internet
Author: User

public class Hquery {
private static Connhbase connhbase=new connhbase ();

/*************** Building Table ****************************/
public void creattable (String tbname,string...colfamily) throws Exception {

TableName TableName = tablename.valueof (tbname);Get table name


/* Table Descriptor */
Htabledescriptor Tabledesc = new Htabledescriptor (tableName);
for (String cols:colfamily) {
Tabledesc.addfamily (New Hcolumndescriptor (cols));//Add Column Family
}
/* Create an administrator */
Admin admin = connhbase.getconnect (). Getadmin ();


/* Create a table */
Admin.createtable (TABLEDESC);
}

/*************** inserting and updating data ****************************/
public void Createcell (String tablename,string colfamily,string rowkey,string column,string value) throws IOException {

Table table = Connhbase.getconnect (). GetTable (Tablename.valueof (TableName));//Tables Instance
hcolumndescriptor[] columnfamilies = Table.gettabledescriptor (). Getcolumnfamilies ();//Get all the column families in the table
/* Insert Device */
Put put = new put (Bytes.tobytes (RowKey));//Set line number, RowKey
/* Traverse the column family to find a matching column family */
for (int i = 0; i < columnfamilies.length; i++) {
String familyname = columnfamilies[i].getnameasstring (); Get column Family name
If the column family is specified
if (Familyname.equals (colfamily)) {
Put.addcolumn (Bytes.tobytes (familyname), bytes.tobytes (column), bytes.tobytes (value));//write
}
}
Table.put (Put); Run Write
}

/************ Querying cell data ***********/
Public list<cell> GetRow (String tablename,string rowKey) throws IOException {
Table table = Connhbase.getconnect (). GetTable (Tablename.valueof (TableName));//Tables Instance
Get get = new Get (Bytes.tobytes (RowKey));Querying a specified row
Result result = Table.get (get);Execute Query
list<cell> listcells = Result.listcells ();Specify all columns for rows and all column families
/* Traverse cells */
/* for (Cell cell:listcells) {
SYSTEM.OUT.PRINTLN ("Column family:" + bytes.tostring (cellutil.clonefamily (cell)));
SYSTEM.OUT.PRINTLN ("Column name:" + bytes.tostring (Cellutil.clonequalifier (cell)));
SYSTEM.OUT.PRINTLN ("column value:" + bytes.tostring (Cellutil.clonevalue (cell)));
System.out.println ("timestamp:" + cell.gettimestamp ());
List.add (cell)
}*/
return listcells;
}

/*********** Full table Scan ************/
Public list<cell> scantable (String tableName) throws IOException {
List<cell> Cells=null;
Table table = Connhbase.getconnect (). GetTable (Tablename.valueof (TableName));//Tables Instance
Resultscanner Resultscanner = Table.getscanner (New Scan ()); Finder for full table
java.util.iterator<result> results = Resultscanner.iterator ();Result iterator
while (Results.hasnext ()) {
Result result = Results.next ();
Cells = Result.listcells ();
/* for (Cell cell:cells) {
SYSTEM.OUT.PRINTLN ("Column family:" + bytes.tostring (cellutil.clonefamily (cell)));
SYSTEM.OUT.PRINTLN ("Column name:" + bytes.tostring (Cellutil.clonequalifier (cell)));
SYSTEM.OUT.PRINTLN ("column value:" + bytes.tostring (Cellutil.clonevalue (cell)));
System.out.println ("timestamp:" + cell.gettimestamp () + "\ n------------------");
}*/
}
Scan Scan =new scan ();
Resultscanner.close ();Close Resource
return cells;

}

/********* Delete Cells *********/
public void Deletecell (string colfamily, String column, String RowKey, String tableName) throws IOException {

Table table = Connhbase.getconnect (). GetTable (Tablename.valueof (TableName));//Tables Instance
Delete del = new Delete (Bytes.tobytes (RowKey));Action deletes the specified row key
Del.addcolumn (Bytes.tobytes (colfamily), bytes.tobytes (column));Specify columns for column families
Table.delete (DEL);Perform the Delete


}

/********* Delete the specified line ***************/
public void DeleteRow (String tablename,string rowKey) throws IOException {

Table table = Connhbase.getconnect (). GetTable (Tablename.valueof (TableName));//Tables Instance
Delete DeleteRow = new Delete (Bytes.tobytes (RowKey));
Table.delete (DeleteRow);

}

/*********** Delete Table **************/
public void deletetable (String tableName) throws IOException {

Admin admin = connhbase.getconnect (). Getadmin ();
Admin.disabletable (tablename.valueof (TableName)); Close table
Admin.deletetable (tablename.valueof (TableName));Delete a table
}
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HBase10.1 Basic Operations (Java code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.