hbase學習筆記(1)-表創建

hbase數據庫表創建及增刪改查操作
package com.gis.test;


	import java.io.IOException;  
	  
	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.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 HbaseDemo {  
	  
	    public static void main(String[] args) throws IOException {  
	        String tableName = "log";  
	        String columnFamily = "cf";  
	  
	        HbaseDemo.create(tableName, columnFamily);  
	  
	        // HbaseDemo.put(tableName, "row1", columnFamily, "cl1", "data");  
	        // HbaseDemo.get(tableName, "row1");  
	        // HbaseDemo.scan(tableName);  
	        // HbaseDemo.delete(tableName);  
	    }  
	  
	    // hbase操作必備  
	    private static Configuration getConfiguration() {  
	        Configuration conf = HBaseConfiguration.create();  
	        conf.set("hbase.rootdir", "hdfs://hadoop:9000/hbase");  
	        // 使用eclipse時必須添加這個,否則無法定位  
	        conf.set("hbase.zookeeper.quorum", "hadoop");  
	        return conf;  
	    }  
	  
	    // 創建一張表  
	    public static void create(String tableName, String columnFamily)  
	            throws IOException {  
	        HBaseAdmin admin = new HBaseAdmin(getConfiguration());  
	        if (admin.tableExists(tableName)) {  
	            System.out.println("table exists!");  
	        } else {  
	            HTableDescriptor tableDesc = new HTableDescriptor(tableName);  
	            tableDesc.addFamily(new HColumnDescriptor(columnFamily));  
	            admin.createTable(tableDesc);  
	            System.out.println("create table success!");  
	        }  
	    }  
	  
	    // 添加一條記錄  
	    public static void put(String tableName, String row, String columnFamily,  
	            String column, String data) throws IOException {  
	        HTable table = new HTable(getConfiguration(), tableName);  
	        Put p1 = new Put(Bytes.toBytes(row));  
	        p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes  
	                .toBytes(data));  
	        table.put(p1);  
	        System.out.println("put'" + row + "'," + columnFamily + ":" + column  
	                + "','" + data + "'");  
	    }  
	  
	    // 讀取一條記錄  
	    public static void get(String tableName, String row) throws IOException {  
	        HTable table = new HTable(getConfiguration(), tableName);  
	        Get get = new Get(Bytes.toBytes(row));  
	        Result result = table.get(get);  
	        System.out.println("Get: " + result);  
	    }  
	  
	    // 顯示所有數據  
	    public static void scan(String tableName) throws IOException {  
	        HTable table = new HTable(getConfiguration(), tableName);  
	        Scan scan = new Scan();  
	        ResultScanner scanner = table.getScanner(scan);  
	        for (Result result : scanner) {  
	            System.out.println("Scan: " + result);  
	        }  
	    }  
	  
	    // 刪除表  
	    public static void delete(String tableName) throws IOException {  
	        HBaseAdmin admin = new HBaseAdmin(getConfiguration());  
	        if (admin.tableExists(tableName)) {  
	            try {  
	                admin.disableTable(tableName);  
	                admin.deleteTable(tableName);  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	                System.out.println("Delete " + tableName + " 失敗");  
	            }  
	        }  
	        System.out.println("Delete " + tableName + " 成功");  
	    }  
	  
	}  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章