In order to facilitate future viewing, we summarize some of the commonly used operating hbase java code:
package com.mcq; import static org.hamcrest.CoreMatchers.describedAs; import static org.hamcrest.CoreMatchers.nullValue; import java.io.IOException; import java.io.PushbackInputStream; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellScannable; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; public class TestHbase { private static Admin admin = null; private static Connection connection = null; private static Configuration conf = null; static { // HBase profile conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.103"); // Get the connection object try { connection = ConnectionFactory.createConnection(conf); // Get the object HBase administrator admin = connection.getAdmin(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace (); } } private static void close(Connection conn, Admin admin) throws IOException { if (conn != null) { conn.close(); } if (admin != null) { admin.close(); } } // determine whether there is table public static boolean tableExist(String tableName) throws IOException { boolean tableExists = admin.tableExists(TableName.valueOf(tableName)); return tableExists; } // Create a table public static void createTable(String tableName, String... cfs) throws IOException { if (tableExist(tableName)) { System.out.println ( "table already exists"); return; } // cfs is a column family, the official suggested a table one, but can have multiple // Create a table descriptor HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName)); for (String cf : cfs) { // create a column descriptor HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf); // hColumnDescriptor.setMaxVersions (3); // set the number of versions hTableDescriptor.addFamily(hColumnDescriptor); } // Create the operating table admin.createTable(hTableDescriptor); } // Delete table public static void deleteTable(String tableName) throws IOException { if (!tableExist(tableName)) { System.out.println ( "table does not exist"); return; } // make unusable table (offline) admin.disableTable(TableName.valueOf(tableName)); // delete operation admin.deleteTable(TableName.valueOf(tableName)); System.out.println ( "table deleted"); } // add, change public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException { // Get the table object // HTable table=new HTable(conf,TableName.valueOf(tableName)); 已过时 Table table = connection.getTable(TableName.valueOf(tableName)); // Create Object put Put put = new Put(Bytes.toBytes(rowKey)); // adding data put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value)); // add operation table.put(put); } // delete public static void delete(String tableName, String rowKey, String cf, String cn) throws IOException { // get table objects Table table = connection.getTable(TableName.valueOf(tableName)); // Create delete objects Delete delete = new Delete (Bytes.toBytes (rowKey)); // Delete the entire column family delete.addColumns (Bytes.toBytes (cf), Bytes.toBytes (cn)); // delete all versions // delete.addColumn (Bytes.toBytes (cf), Bytes.toBytes (cn)); not recommended, delete only the latest version // delete operation table.delete(delete); table.close(); } // check - a full table scan, you can only get the latest version public static void scanTable(String tableName) throws IOException { // get table objects Table table = connection.getTable(TableName.valueOf(tableName)); // build Scanner Scan scan = new Scan(); ResultScanner resultScanner = table.getScanner(scan); // traverse the data and print for (Result result : resultScanner) { // rowkey Cell[] cells = result.rawCells(); for (Cell cell : cells) { // cell System.out.println("RK:" + Bytes.toString(CellUtil.cloneRow(cell)) + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell)) + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell))); } } table.close(); } // check - Gets the designated column family public static void getData(String tableName, String rowKey, String cf, String cn) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn)); // get.addFamily(cf); // get.setMaxVersions (); // default parameters are not transmitted within the table structure maxversions get.setMaxVersions(2); Result result = table.get(get); Cell[] cells = result.rawCells(); for (Cell cell : cells) { // cell System.out.println("RK:" + Bytes.toString(CellUtil.cloneRow(cell)) + ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell)) + ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + ",VALUE:" + Bytes.toString(CellUtil.cloneValue(cell))); } } public static void main(String[] args) throws IOException { // determine whether there is table // System.out.println(tableExist("student")); // System.out.println(tableExist("staff")); // Create a table // createTable("staff", "info"); // System.out.println(tableExist("staff")); // Delete table // deleteTable("staff"); // add, change // putData("student", "1001", "info", "name", "mcq"); // delete // delete("student", "1001", "info", "name"); // check - a full table scan // scanTable("student"); // check - Gets the designated column family getData("student", "1001","info","name"); // Close the resource close(connection, admin); } }