1. 程式人生 > >JAVA API 操作HBASE(一)

JAVA API 操作HBASE(一)

使用java API操作HBase

實現功能

建立表

刪除表

新增列

新增列名稱

列出所有表名稱

列出所有表下的列名稱

使用到的Hbase操作類

HBaseConfiguration     配置hbase配置資訊

HBaseAdmin 使用其進行Hbase資料表的操作

package hbase;

import org.apache.hadoop.hbase.HBaseConfiguration;

@SuppressWarnings("deprecation")
public abstract class HBaseAb {
	
	protected static HBaseConfiguration conf = null;
	
	static{
		conf = new HBaseConfiguration();
		conf.set("hbase.zookeeper.quorum", "master");
	}

}


package hbase;

import java.io.IOException;

import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

/**
 * HBASE 資料定義語言(DDL)
 * 
 * 定義對Hbase資料庫表的操作 包括: 表的建立 表的刪除 表的修改: 增加列 ,刪除列.....
 * 
 * 使用HbaseAdmin
 * 
 * @author Administrator
 */
public class HbaseDDL extends HBaseAb {

	static HBaseAdmin admin = null;
	static {
		try {
			admin = new HBaseAdmin(conf);
		} catch (MasterNotRunningException e) {
			e.printStackTrace();
		} catch (ZooKeeperConnectionException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create data tables have multiple columns
	 * 
	 * @param tableName
	 * @param familys
	 * @throws Exception
	 */
	public void createTable(String tableName, String[] familys)
			throws Exception {
<span style="font-family: Arial, Helvetica, sans-serif;">		<span style="white-space:pre">		</span>if (admin.tableExists(tableName)) {</span>
			throw new Exception("Table Already Exists");
		}
		HTableDescriptor table = new HTableDescriptor(tableName);
		for (int i = 0; i < familys.length; i++) {
			String family = familys[i];
			HColumnDescriptor column = new HColumnDescriptor(family);
			table.addFamily(column);
		}
		admin.createTable(table);
		System.out.println("Create Table:" + tableName);
	}

	/**
	 * For the existing table add column
	 * 
	 * @param tableName
	 * @param columnName
	 */
	public void addFamily(String tableName, String columnName) throws Exception {
		if (!admin.tableExists(tableName)) {
			throw new Exception("Table Not Exists");
		}
		admin.addColumn(tableName, new HColumnDescriptor(columnName));
	}
	
	
	/**
	 * Drop Table
	 * @param tableName
	 * @throws Exception
	 */
	public void DropTable(String tableName) throws Exception{
		admin.disableTable(tableName);
		admin.deleteTable(tableName);
	}
	
	
	/**
	 * Drop Table Family
	 * @param tableName
	 * @param familyName
	 * @throws Exception
	 */
	public void dropTableFamily(String tableName ,String familyName) throws Exception{
		admin.deleteColumn(tableName, familyName);
	}
	
	/**
	 * A list of all the tables in the column name
	 * @param tableName 
	 * @return
	 * @throws Exception
	 */
	public String[] getFamilys(String tableName) throws Exception{
		if (!admin.tableExists(tableName)) {
			throw new Exception("Table Not Exists");
		}
		TableName name = TableName.valueOf(tableName);
		HTableDescriptor td = admin.getTableDescriptor(name);
		HColumnDescriptor[] hcds = td.getColumnFamilies();
		String[] familys = new String[hcds.length];
		for (int i = 0; i < hcds.length; i++) {
			HColumnDescriptor hcd = hcds[i];
			System.out.println("Table Name:"+tableName+ " Family Name:" + hcd.getNameAsString());
			familys[i] = hcd.getNameAsString();
		}
		return familys;
	}
	
	/**
	 * List the names of all the data table
	 * @return An array of data table name
	 * @throws Exception 
	 */
	public String[] listTableNames() throws Exception {
			TableName[] tableName = admin.listTableNames();
			String [] tableNames = new String[tableName.length]; 
			for (int i = 0; i < tableName.length; i++) {
				TableName name = tableName[i];
				tableNames[i] = name.getNameAsString();
				System.out.println("TableName:"+name.getNameAsString());
			}
			return tableNames;
	}
	
	/**
	 * The existence of data table
	 * @param tableName
	 * @return
	 * @throws Exception
	 */
	public boolean tableExists(String tableName) throws Exception {
		return admin.tableExists(tableName);
	}

	public static void main(String[] args) throws Exception {
		HbaseDDL hbaseDDL = new HbaseDDL();
		String[] familys = { "zhuss" };
		String[] tableNames = hbaseDDL.listTableNames();
		
		for (int i = 0; i < tableNames.length; i++) {
			String string = tableNames[i];
			hbaseDDL.getFamilys(string);
		}
		
//		hbaseDDL.dropTableFamily("zhuss","shun");
//		 hbaseDDL.createTable("zhuss",familys);
//		
//		System.out.println(hbaseDDL.tableExists("zhuss"));
//		hbaseDDL.addColmn("zhuss", "shun");

	}

}