JAVA API 操作HBASE(一)
2014-10-15 23:13
417 查看
使用java API操作HBase
实现功能
创建表
删除表
添加列
添加列名称
列出所有表名称
列出所有表下的列名称
使用到的Hbase操作类
HBaseConfiguration 配置hbase配置信息
HBaseAdmin 使用其进行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"); } }
相关文章推荐
- 使用Java API对HBase进行CRUD操作
- HBase Java API使用操作例子
- [置顶] Java API连接HBase 进行增删查改操作
- 使用Java API对HBase进行操作
- HBase单机模式下Java API操作示例
- HBase操作(Shell与Java API)
- HBase的eclipse操作Java API
- 用java API来操作HBase
- Hbase-1.1.2 Java API 操作
- Java API 连接Hbase数据库以及这次项目实践所涉及的操作
- 使用java api操作hbase数据库
- hbase java api操作
- 1006-HBase操作实战(JAVA API状态)
- HBase操作(Shell与Java API)
- 使用Java Api 对HBase进行简单操作
- 使用Java Api 对HBase进行简单操作
- 1006-HBase操作实战(JAVA API模式)
- 通过Java Api对Hbase进行操作
- hbase开发,hbase表操作及其java api实现
- hbase开发,hbase表操作及其java api实现