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");

	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值