標籤:param 示範 desc 資料 script keep target ado art
hbase安裝方法請參考:hbase-0.94安裝方法具體解釋
hbase經常使用的shell命令請參考:hbase經常使用的shell命令範例
java操作hbase,在eclipse中建立一個java項目。將hbase安裝檔案根資料夾的jar包和lib檔案夾下jar包匯入項目,然後就能夠編寫java代碼操作hbase了。
以下代碼給出來一個簡單的示範範例
/** * @date 2015-07-23 21:28:10 * @author sgl */package com.songguoliang.hbase;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.KeyValue;import org.apache.hadoop.hbase.client.Delete;import org.apache.hadoop.hbase.client.Get;import org.apache.hadoop.hbase.client.HBaseAdmin;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.util.Bytes;public class HBaseTest {//聲明靜態配置,HBaseConfigurationprivate static Configuration conf=null;static{conf=HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", "sdw1,sdw2");}/** * 建立一個表 * @date 2015-07-23 21:44:32 * @author sgl * @param tableName 表名 * @param columnFamilys 列族 * @throws IOException */public static void createTable(String tableName,String[]columnFamilys) throws IOException{HBaseAdmin admin =new HBaseAdmin(conf);if (admin.tableExists(tableName)) {System.out.println("table already exists!");}else{HTableDescriptor tableDesc=new HTableDescriptor(tableName);for(int i=0;i<columnFamilys.length;i++){tableDesc.addFamily(new HColumnDescriptor(columnFamilys[i]));}admin.createTable(tableDesc);System.out.println("create table "+tableName+" success!");}}/** * 加入一條記錄 * @date 2015-07-23 22:16:30 * @author sgl * @param tableName 表名 * @param rowKey 行健 * @param family 列族 * @param qualifier 限定符 * @param value 值 * @throws IOException */public static void addRecord(String tableName,String rowKey,String family,String qualifier,String value) throws IOException{HTable table =new HTable(conf, tableName);Put put=new Put(Bytes.toBytes(rowKey));put.add(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));table.put(put);System.out.println("insert record "+rowKey+" to table "+tableName+" success");}/** * 刪除一行記錄 * @date 2015-07-23 22:17:53 * @author sgl * @param tableName 表名 * @param rowKey 行健 * @throws IOException */public static void deleteRecord(String tableName,String rowKey) throws IOException{HTable table=new HTable(conf, tableName);List<Delete>list=new ArrayList<Delete>();Delete delete=new Delete(rowKey.getBytes());list.add(delete);table.delete(list);System.out.println("delete record "+ rowKey+" success!");}/** * 擷取一行記錄 * @date 2015-07-23 22:21:33 * @author sgl * @param tableName 表名 * @param rowKey 行健 * @throws IOException */public static void getOneRecord(String tableName,String rowKey) throws IOException{HTable table=new HTable(conf, tableName);Get get=new Get(rowKey.getBytes());Result rs=table.get(get);for(KeyValue kv:rs.raw()){System.out.print(new String(kv.getRow()) + " " ); System.out.print(new String(kv.getFamily()) + ":" ); System.out.print(new String(kv.getQualifier()) + " " ); System.out.print(kv.getTimestamp() + " " ); System.out.println(new String(kv.getValue())); }}/** * 擷取全部資料 * @date 2015-07-23 22:26:19 * @author sgl * @param tableName 表名 * @throws IOException */public static void getAllRecord(String tableName) throws IOException{HTable table=new HTable(conf, tableName);Scan scan=new Scan();ResultScanner scanner=table.getScanner(scan);for(Result result:scanner){for(KeyValue kv:result.raw()){System.out.print(new String(kv.getRow()) + " "); System.out.print(new String(kv.getFamily()) + ":"); System.out.print(new String(kv.getQualifier()) + " "); System.out.print(kv.getTimestamp() + " "); System.out.println(new String(kv.getValue())); }}}/** * 刪除一個表 * @date 2015-07-23 22:29:35 * @author sgl * @param tableName 表名 * @throws IOException */public static void deleteTable(String tableName) throws IOException{HBaseAdmin admin=new HBaseAdmin(conf);admin.disableTable(tableName);admin.deleteTable(tableName);System.out.println("delete table "+tableName+" success!");}public static void main(String[] args) {try {String tableName="scores";String[]columnFamilys={"grade","course"};HBaseTest.createTable(tableName, columnFamilys);HBaseTest.addRecord(tableName, "sgl", "grade", "", "5");HBaseTest.addRecord(tableName, "sgl", "course", "", "90");HBaseTest.addRecord(tableName, "sgl", "course", "math", "97");HBaseTest.addRecord(tableName, "sgl", "course", "art", "87");HBaseTest.addRecord(tableName, "guoguo", "grade", "", "4");HBaseTest.addRecord(tableName, "guoguo", "course", "math", "89");System.out.println("********get one record*********");HBaseTest.getOneRecord(tableName, "sgl");System.out.println("********get all record*********");HBaseTest.getAllRecord(tableName);System.out.println("********delete one record*********");HBaseTest.deleteRecord(tableName, "guoguo");HBaseTest.getAllRecord(tableName);System.out.println("********delete table*********");HBaseTest.deleteTable(tableName);} catch (IOException e) {e.printStackTrace();}}}
java操作hbase範例