1. Software Version & Deployment:
maven:3.3.9,jdk:1.7, struts2:2.3.24.1,hibernate:4.3.6,spring:4.2.5,mysql:5.1.34,junit:4,myeclipse:2014;
hadoop2.6.4,hbase1.1.2
SOURCE Download: https://github.com/fansy1990/ssh_v3/releases
Deployment reference: http://blog.csdn.net/fansy1990/article/details/51356583
2. system function and core implementation 2.1 system menu
HBase table Management system is mainly related to the table and the table data operation;
2.2 Table Management
Directly open the table management interface, you can see the summary information of all tables, including database (namspace), table name, simple table description, etc.;
The information for this query is directly based on the admin Listtablenames method, which is called as follows:
/** * Get ALL Tables * * @return * @throws ioexception */public list
2.2.1 Table DetailsThe table details feature requires a row of records to be selected, otherwise you will be prompted:
After selecting a record, click on the table details to pop up the table details:
The background implementation through the Admin.gettabledescriptor can get the table details, but need to provide table name, table name can be passed in from the day before, as follows:
/** * Get specified table details * * @param tableName * @return * @throws ioexception */public string gettabledetails (String tableName) Throws IOException {Admin admin = hadooputils.gethbaseconnection (). Getadmin (); Htabledescriptor tabledescriptors = Admin.gettabledescriptor (Gettablename (tableName)); System.out.println (Tabledescriptors.tostringcustomizedvalues ()); System.out.println (Tabledescriptors.tostring ()); return Admin.gettabledescriptor (Gettablename (TableName)). ToString ();}
2.2.2 Table addedTable new features just provide a simple addition, that is, provide only table name, column Cluster name, as follows:
At the same time, after the background, if the table is already there will be a corresponding prompt, the background by traversing all the table names to achieve this function, the efficiency is not high, implemented as follows:
public boolean checktableexists (String tableName) throws IOException {Admin admin = hadooputils.gethbaseconnection (). Getadmin (); tablename[] tables = Admin.listtablenames (); for (TableName t:tables) {if (t.getnameasstring (). Equals (TableName)) { return true;}} return false;}
Added tables, implemented via admin createtable:public boolean savetable (String tableName, String cfs) throws IOException {Admin admin = hadooputils.gethbaseconnection () . Getadmin (); Htabledescriptor htabledescriptor = new Htabledescriptor (Gettablename (tableName)); string[] Cfsarr = Stringutils.split (cfs, Utils.comma); for (String Cf:cfsarr) {htabledescriptor.addfamily (new Hcolumndescriptor (CF));} Admin.createtable (Htabledescriptor); return true;}
2.2.3 Table DeleteTable Delete, also need to select a record, if not select the record, also prompted to select the table, select the table, click Delete, the following prompt:
If click OK, then delete, cancel;
Delete the implementation, also use the admin disabletable, deletetable method
public boolean deletetable (String tableName) throws IOException {Admin admin = hadooputils.gethbaseconnection (). Getadmin (); Admin.disabletable (Gettablename (TableName)); Admin.deletetable (Gettablename (TableName)); return true;}
Note: This should be the first to determine whether this table is the enable state, if it is not directly deleted, if it is the disable state, the execution of the above code will be problematic;2.3 Table Data ManagementWhen the Table Data management page is opened, the table Name drop-down box is initialized, and the data in the drop-down box is accessed by Ajax, using the Listtablenames method of admin;
Amount
After selecting a table, the Column Cluster Name drop box data and start Rowkey are initialized, as follows:
When querying data, you can select multiple column family names, and you can modify the start Rowkey, select the number of records and the number of versions;
Get the column cluster name based on the table name background code is implemented as follows (obtained through the Htabledescriptor Getcolumnfamilies method):
Public list<textvalue> gettablescolumnfamily (String tableName) throws IOException {list<textvalue> List = New Arraylist<> (); Admin admin = hadooputils.gethbaseconnection (). Getadmin (); Htabledescriptor tabledescriptor = Admin.gettabledescriptor (Gettablename (tableName)); hcolumndescriptor[] columndescriptors = Tabledescriptor.getcolumnfamilies (); for (Hcolumndescriptor t: columndescriptors) {List.add (New TextValue (T.getnameasstring ()));} return list;}
and the background code for start Rowkey (that is, reading only the first row of data and then returning its rowkey):
public string Gettablerowkey (String tableName) throws IOException {Table table = Hadooputils.gethbaseconnection (). GetTable (Gettablename (tableName)); Scan scan = new scan (); Resultscanner scanner = Table.getscanner (scan); Result firstrow = Scanner.next (); Scanner.close (); Table.close (); if (firstrow = = null) return "-1"; return new String ( Firstrow.getrow ());}
Based on the table name, column Cluster name, start Rowkey, version number, number of records, the background code for the data is as follows:Public list
By setting the Maxversions and StartRow of the scan to limit the number of versions and starting to traverse the position, set the Scanner.next to limit the number of records obtained;3.1 NewData is added by specifying a table name, a column cluster name, and an error if the column cluster name specifies more than one:
The data is added using the Window popup box, in order to pass the table name and the column cluster name to the window, using the URL plus parameter method (more awkward way), as follows:
var Win_table_add_data_ = $ (' #win_table_add_data '). Window ({ width:450, height:350, modal:true, left:400, top:150, title: ' Data added ', collapsible:false, minimizable:false, maximizable:false,/ / content: ' <div style= ' padding:30px 20px 10px 20px; " > ' + ' a ' + ' </div> ' content: ' <iframe id= ' tabiframe ' src= ' hbasecommand/data_add.jsp?tablename= ' + tablename_+ ' &cf= ' +cf_+ ' "frameborder=" 0 "style=" border:0;width:100%;height:100%; " > ',// href: "hbasecommand/data_add.jsp", onopen:function () { //modify the corresponding value;// $ (' #data_add_ff_ TableName '). Val (Getfakedata (' cc_data_retrieve_tablename '));// $ (' #data_add_ff_family '). Val (cf_); $ (' #data_add_ff_family '). TextBox (' SetValue ', cf_); }});
In the pop-up box, the user can enter Rowkey,column,vlaue:Table name, column family name is not editable, the user click Add, put the data directly into the table;
public boolean Savetabledata (String tableName, String cfs, String rowkey,string column, String value) throws IOException { Table table = Hadooputils.gethbaseconnection (). GetTable (Gettablename (tableName)); Put put = new put (Bytes.tobytes (rowkey));p Ut.addcolumn (Bytes.tobytes (cfs), bytes.tobytes (column), Bytes.tobytes ( value); Table.put (put); Table.close (); return true;}
3.3.2 Data removalUse Checkanddelete directly based on the data provided to prevent data from being modified at the time of deletion:
public boolean Deletetabledata (String tableName, String family,string Qualifier, String Rowkey, String value, long Timesta MP) throws IOException {Table table = Hadooputils.gethbaseconnection (). GetTable (Gettablename (tableName));D elete Delete = new Delete (Bytes.tobytes (rowkey));d Elete.addcolumn (bytes.tobytes (family), bytes.tobytes (qualifier), timestamp); Boolean flag = Table.checkanddelete (Bytes.tobytes (Rowkey), bytes.tobytes (family), Bytes.tobytes ( qualifier), bytes.tobytes (value), delete); Table.close (); return flag;}
3.3.3 Data UpdateData update, also need to select a record, pop-up box similar to the data new (more awkward way), but can be modified only the value of the content:
In addition to the values, the other input boxes are read-only, and the background implementation uses the Checkandput method:
public boolean Updatetabledata (String tableName, String cfs, String rowkey,string column, string value, long timestamp, St Ring OldValue) throws ioexception {Table table = Hadooputils.gethbaseconnection (). GetTable (Gettablename (tableName)); Put put = new put (Bytes.tobytes (rowkey));p Ut.addcolumn (Bytes.tobytes (cfs), bytes.tobytes (column), timestamp, Bytes.tobytes (value)); Table.checkandput (Bytes.tobytes (Rowkey), Bytes.tobytes (cfs), bytes.tobytes (column), Bytes.tobytes (OldValue), put); Table.close (); return true;}
4. Summary
1) The use of HBase API is not very difficult, mainly if it is inserted or update operation, need to keep records consistent, need to do row lock, then need to use the checkandxxx operation, the concrete can refer to: http://blog.csdn.net/ fansy1990/article/details/51451583;
2) data addition and Data Update pop-up box is more awkward, you can consider the use of new methods (because of the parameters to be passed);
Share, grow, be happy
Down-to-earth, focus
Reprint Please specify blog address: http://blog.csdn.net/fansy1990
HBase Table Management System