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

	}

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java API操作HBase非常简单和方便。HBase提供了一个Java库,可以使用它来连接和与HBase进行交互。下面是使用Java API操作HBase的步骤: 1. 首先,需要导入HBaseJava库。可以在项目的构建文件(例如pom.xml)中添加HBase相关依赖项,或者手动将HBase库添加到项目的类路径中。 2. 创建HBase的配置对象,并设置必要的配置参数。配置对象可以指定HBase连接地址、端口号等信息。 3. 使用HBase的配置对象创建一个HBase连接对象。连接对象允许与HBase进行通信。 4. 通过连接对象创建一个HBase的管理员对象。管理员对象用于对HBase的表进行管理,如创建表、删除表等操作。 5. 创建HBase表的描述符对象,并指定表的名称、列族等信息。 6. 使用管理员对象创建HBase表。可以使用表的描述符对象来定义表的结构。 7. 使用HBase表的描述符对象创建一个表对象。表对象用于与HBase的表进行交互。 8. 使用表对象执行各种操作,如插入数据、更新数据、删除数据等。可以使用行键(row key)和列族名(column family)来定位和操作特定的数据。 9. 关闭与HBase连接,释放资源。 通过以上步骤,可以使用Java API连接操作HBase。在实际应用中,还可以根据具体需求来添加其他操作,如查询数据、扫描表等。使用Java API操作HBase可以灵活地控制和管理HBase中的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值