The previous blog said that using the HBase client API to manipulate the tables in the admin HBase, today we'll see how to manipulate the data in the table through the API. introduce
In HBase the data in the data table, we are generally through the table, put, get, delete,scan,result and so on several classes to achieve. Tables are table objects, a table in the database, and we can add, modify, delete, and query operations on the table. Put is primarily used to perform write/update operations on records in a datasheet. Get is mainly used to perform query operations on records in a datasheet. The key is to perform a query operation on the records in the datasheet. Scan is used to perform query operations in a datasheet. Result is used to hold the results record of the query. Write Data Operations when writing data, we need to first get to the table object that needs to be manipulated. A put object is then created to perform the update operation, and a given row name is required to create the object. Then add the action you want to perform in the Put object, where you add the data. When the data is filled in, the put operation is performed on the table. Finally, don't forget to close the table.
private void Putrow (string row, string Username, string password, string home, String office) throws IOException {
Tab Le table = connection.gettable (tablename.valueof ("user"));
Put on = new put (bytes.tobytes (row));
Put.addcolumn (Bytes.tobytes ("base"), Bytes.tobytes ("username"), bytes.tobytes (username));
Put.addcolumn (Bytes.tobytes ("base"), Bytes.tobytes ("password"), bytes.tobytes (password));
Put.addcolumn (Bytes.tobytes ("Address"), Bytes.tobytes ("Home"), Bytes.tobytes (home);
Put.addcolumn (Bytes.tobytes ("Address"), Bytes.tobytes ("office"), Bytes.tobytes (office);
Table.put (put);
Table.close ();
}
Get DataYou need to get to the table object that you want to manipulate. Create a Get object to perform a fetch operation, and you need to tell it what row of data to get when you create a getting object. It then performs a get operation on the table to fetch the data. After fetching the data, the data is kept in the result object, and we can get the desired value through some methods of the result object. Finally, don't forget to close the table.
private void GetRow (String row) throws IOException {Table table = connection.gettable (tablename.valueof ("User
"));
Get get = new Get (bytes.tobytes (row));
Result result = Table.get (get);
if (Bytes.tostring (Result.getrow ())!= null) {StringBuilder SB = new StringBuilder ();
Sb.append (Bytes.tostring (Result.getrow ()));
Sb.append ("[");
Sb.append ("base:username=" + bytes.tostring (Result.getvalue (bytes.tobytes ("base"), Bytes.tobytes ("username")));
Sb.append (", base:password=" + bytes.tostring (Result.getvalue (bytes.tobytes ("base"), Bytes.tobytes ("password")));
Sb.append (", address:home=" + bytes.tostring (Result.getvalue (bytes.tobytes ("Address"), Bytes.tobytes ("Home"))); Sb.append (", address:office=" + bytes.tostring (Result.getvalue bytes.tobytes ("Address"), Bytes.tobytes ("Offic
E ")));
Sb.append ("]");
System.out.println (Sb.tostring ());
} Table.close (); }
Delete DataYou need to get to the table object that you want to manipulate. Creates a Delete object to perform the deletion, and when you create the Delete object you need to tell it which row of data to delete. Then delete the data by performing a delete operation on the table. Finally, don't forget to close the table.
private void DeleteRow (String row) throws IOException {
table table = connection.gettable (tablename.valueof ("user") );
Delete delete = new Delete (bytes.tobytes (row));
Table.delete (delete);
Table.close ();
}
Querying DataYou need to get to the table object that you want to manipulate. Creates a scan object to perform a query operation. Then perform the scan operation on the table and get the Resultscanner object. We then perform an iterative operation on the Resultscanner to get the values. Finally, don't forget to close the table.
private void GetRows () throws IOException {
table table = connection.gettable (tablename.valueof ("user"));
Scan Scan = new Scan ();
Resultscanner Resultscanner = Table.getscanner (scan);
Iterator<result> it = Resultscanner.iterator ();
while (It.hasnext ()) {result result
= It.next ();
GetRow (result);
}
Table.close ();
}
Complete Code
Finally, the complete example code for performing database operations.
Package my.hbasestudy;
Import org.apache.hadoop.conf.Configuration;
Import org.apache.hadoop.hbase.HBaseConfiguration;
Import Org.apache.hadoop.hbase.TableName;
Import org.apache.hadoop.hbase.client.*;
Import org.apache.hadoop.hbase.util.Bytes;
Import java.io.IOException;
Import Java.util.Iterator;
Import java.util.List;
public class Testapi {private static final String table_name = "user";
private static final String column_family_base = "BASE";
private static final String column_family_address = "Address";
private static final String Column_username = "USERNAME";
private static final String Column_password = "PASSWORD";
private static final String Column_home = "Home";
private static final String Column_office = "OFFICE";
Private Connection Connection;
public static void Main (string[] args) throws Exception {Configuration config = hbaseconfiguration.create (); Connection Connection = connectionfactory.createconnection (config);
Long T1 = System.currenttimemillis ();
Testapi t = new Testapi (connection);
T.listtable ();
T.createtable ();
T.listtable ();
T.putrows ();
T.getrows ();
T.deleterows ();
T.getrows ();
T.deletetable ();
Long t2 = System.currenttimemillis ();
System.out.println ("Time:" + (T2-T1));
Connection.close ();
Public Testapi (Connection Connection) {this.connection = Connection;
private void Listtable () throws IOException {Admin admin = connection.getadmin ();
try {list<tabledescriptor> tabledescriptors = admin.listtabledescriptors (); for (Tabledescriptor tabledescriptor:tabledescriptors) {tablename TableName = Tabledescriptor.gettablen
Ame ();
System.out.println ("Table:" + tablename);
System.out.println ("\texists:" + admin.tableexists (tablename)); System.out.println ("\tenabled:" + admin.istableenabled (tablename));
finally {admin.close ();
} private void CreateTable () throws IOException {Admin admin = connection.getadmin ();
try {tabledescriptor Tabledesc = Tabledescriptorbuilder.newbuilder (tablename.valueof (table_name))
. addcolumnfamily (Columnfamilydescriptorbuilder.newbuilder (Bytes.tobytes (column_family_base)). Build ())
. addcolumnfamily (Columnfamilydescriptorbuilder.newbuilder (Bytes.tobytes (column_family_address)). Build ())
. build ();
Admin.createtable (TABLEDESC);
finally {admin.close ();
} private void Deletetable () throws IOException {Admin admin = connection.getadmin ();
try {admin.disabletable (tablename.valueof (table_name));
Admin.deletetable (TABLENAME.VALUEOF (table_name)); } finAlly {Admin.close (); } private void Putrows () throws IOException {for (int i = 0; i < i++) {Putrow ("row
_ "+ I," user_ "+ I," password_ "+ I," home_ "+ I," office_ "+ i); } private void Putrow (string row, string Username, string password, string home, String office) throws Ioexcept
Ion {Table table = connection.gettable (tablename.valueof (table_name));
Put on = new put (bytes.tobytes (row));
Put.addcolumn (Bytes.tobytes (column_family_base), Bytes.tobytes (Column_username), Bytes.tobytes (USERNAME));
Put.addcolumn (Bytes.tobytes (column_family_base), Bytes.tobytes (Column_password), Bytes.tobytes (PASSWORD));
Put.addcolumn (Bytes.tobytes (column_family_address), Bytes.tobytes (Column_home), Bytes.tobytes (home));
Put.addcolumn (Bytes.tobytes (column_family_address), Bytes.tobytes (Column_office), bytes.tobytes (OFFICE);
Table.put (Put);
Table.close ();
}private void GetRows () throws ioexception {Table table = connection.gettable (tablename.valueof (table_name));
Scan Scan = new Scan ();
Resultscanner Resultscanner = Table.getscanner (scan);
Iterator<result> it = Resultscanner.iterator ();
while (It.hasnext ()) {result result = It.next ();
GetRow (result);
} table.close (); } private void GetRow (String row) throws IOException {Table table = connection.gettable (tablename.valueof (TA
Ble_name));
Get get = new Get (bytes.tobytes (row));
Result result = Table.get (get);
GetRow (result);
Table.close (); private void GetRow (result result) {if (Bytes.tostring (Result.getrow ())!= null) {Stringbuild
ER sb = new StringBuilder ();
Sb.append (Bytes.tostring (Result.getrow ()));
Sb.append ("["); Sb.append ("base:username=" + bytes.tostring (Result.getvalue (bytes.tobyteS ("base"), Bytes.tobytes ("username")));
Sb.append (", base:password=" + bytes.tostring (Result.getvalue (bytes.tobytes ("base"), Bytes.tobytes ("password")));
Sb.append (", address:home=" + bytes.tostring (Result.getvalue (bytes.tobytes ("Address"), Bytes.tobytes ("Home"))); Sb.append (", address:office=" + bytes.tostring (Result.getvalue (bytes.tobytes ("Address"), Bytes.tobytes ("office")
)));
Sb.append ("]");
System.out.println (Sb.tostring ()); } private void DeleteRows () throws ioexception {Table table = connection.gettable (tablename.valueof (TA
Ble_name));
Scan Scan = new Scan ();
Resultscanner Resultscanner = Table.getscanner (scan);
Iterator<result> it = Resultscanner.iterator ();
while (It.hasnext ()) {result result = It.next ();
Delete delete = new Delete (Result.getrow ());
Table.delete (delete);
} table.close ();
}private void DeleteRow (String row) throws IOException {Table table = connection.gettable (tablename.valueof (table_
NAME));
Delete delete = new Delete (bytes.tobytes (row));
Table.delete (delete);
Table.close (); }
}