1. 程式人生 > >HBase(操作API)

HBase(操作API)

HBase操作API

package org.xiaowu.test;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
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.MasterNotRunningException;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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;
import org.junit.Before;
import org.junit.Test;

public class HBaseTest {
	Configuration conf=null;  
	HBaseAdmin admin=null;
	
	@Before
	public void getConf() throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
	    conf =HBaseConfiguration.create();
		conf.set("hbase.rootdir","hdfs://mylinux:8020/hbase");
		conf.set("hbase.zookeeper.quorum", "mylinux");
		admin = new  HBaseAdmin(conf);
	}
	
	/**
	 * 建立名稱空間
	 * */
	@Test
	public void createNamespace() throws MasterNotRunningException, ZooKeeperConnectionException, IOException {	  
		String namespaceName = "hadoop99";
		NamespaceDescriptor ss = NamespaceDescriptor.create(namespaceName).build();
		admin.createNamespace(ss);
		System.out.println("建立"+namespaceName+"成功");
	}
	
	/**
	 * 刪除名稱空間
	 * */
	@Test
	public void deleteNamespace() throws IOException {
		String namespaceName = "hadoop99";
		admin.deleteNamespace(namespaceName);
		System.out.println("刪除"+namespaceName+"成功");
	}
	
	/**
	 * 檢視名稱空間
	 * */
	@Test
	public void listNamespace() throws IOException {
		NamespaceDescriptor[] arr = admin.listNamespaceDescriptors();
		for (NamespaceDescriptor namespace : arr) {
			System.out.println(namespace);
		}
	}
	
	
	/**
	 * 建立表
	 * @throws IOException 
	 * */
	@Test
	public void createTable() throws IOException {
		 TableName tableName = TableName.valueOf("hadoop99:test");
		 //表的描述管理器
		 HTableDescriptor tbDesc = new HTableDescriptor(tableName);
		 //列族描述管理器
		 HColumnDescriptor colDesc = new HColumnDescriptor("info");
		 
		 //為該表加入列族
		 tbDesc.addFamily(colDesc);
		 admin.createTable(tbDesc);
	}
	
	/**
	 * 刪除表資訊
	 * @throws IOException 
	 * */
		@Test
		public void dropTable() throws IOException {
			String tableName = "hadoop99:test";
			admin.disableTable(tableName);
			admin.deleteTable(tableName);
			System.out.println("刪除成功");
		}
	
	
	/**
	 * 檢視所有表資訊
	 * */
	@Test
	public void listTable() throws IOException {
		TableName[] arr = admin.listTableNames();
		for (TableName tableName : arr) {
			System.out.println(tableName);
		}
	}
	
	/**
	 * 增加資料
	 * @throws IOException 
	 * */
	@Test
	public void putDate() throws IOException {
		HTable table =new HTable(conf,TableName.valueOf("hadoop99:test"));
	    
		//rowKey
		Put put = new Put(Bytes.toBytes("1001"));
		
		//列族
		HColumnDescriptor[] famlis = table.getTableDescriptor().getColumnFamilies();
		
		for (HColumnDescriptor hColumnDescriptor : famlis) {
			String cm = hColumnDescriptor.getNameAsString();
			System.out.println(cm);
			put.add(Bytes.toBytes(cm), Bytes.toBytes("age"), Bytes.toBytes("20"));
			//put.add(Bytes.toBytes(cm), Bytes.toBytes("name"), Bytes.toBytes("zhangsan"));
		}
		table.put(put);
	    table.close();
	}
	
	/**
	 * 獲得資料
	 * @throws IOException 
	 * */
	@Test
	public void getDate() throws IOException {
		HTable table = new HTable(conf,TableName.valueOf("hadoop99:test"));
	    Get get = new Get(Bytes.toBytes("1001"));
	    
	    Result result = table.get(get);
	    Cell[] cells = result.rawCells();
	    
	    for (Cell cell : cells) {
			System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
			System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
			System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
			System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
		    System.out.println(cell.getTimestamp());
	    }
	    table.close();
	}
	
	/**
	 * 查看錶裡的資料
	 * @throws IOException 
	 * */
	@Test
	public void scanDate() throws IOException {
		HTable table = new HTable(conf,TableName.valueOf("hadoop99:test"));
		Scan scan = new Scan();
		scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"));
		ResultScanner scanner = table.getScanner(scan);
		for(Result result:scanner) {
			 Cell[] cells = result.rawCells();
			 for (Cell cell : cells) {
				  System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
				  System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
				  System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
				  System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
			      System.out.println(cell.getTimestamp());
			 }
		}
		table.close();
	}
}