您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > educoder平台HBase开发:-Java-API-管理表
第1关:JavaAPI获取表的列表packagestep1;importjava.util.ArrayList;importjava.util.List;importorg.apache.hadoop.conf.*;importorg.apache.hadoop.hbase.*;importorg.apache.hadoop.hbase.client.*;importorg.apache.hadoop.hbase.util.*;publicclassTask{publicvoidshowTableList()throwsException{/*********Begin*********/Configurationconf=HBaseConfiguration.create();Connectionconnection=ConnectionFactory.createConnection(conf);Adminadmin=connection.getAdmin();try{ListTableDescriptortableDescriptors=admin.listTableDescriptors();for(TableDescriptortableDescriptor:tableDescriptors){TableNametableName=tableDescriptor.getTableName();System.out.println(Table:+tableName);System.out.println(\texists:+admin.tableExists(tableName));System.out.println(\tenabled:+admin.isTableEnabled(tableName));}}finally{admin.close();}/*********End*********/}}------------------------------------------------------------------第2关:修改表packagestep2;importjava.io.IOException;importorg.apache.hadoop.conf.*;importorg.apache.hadoop.hbase.*;importorg.apache.hadoop.hbase.client.*;importorg.apache.hadoop.hbase.io.compress.Compression;importorg.apache.hadoop.hbase.util.*;publicclassTask{publicvoidupdateTables()throwsException{/*********Begin*********/Configurationconfig=newConfiguration();Connectionconn=ConnectionFactory.createConnection(config);Adminadmin=conn.getAdmin();String[]tableNames={t_emp2,t_dept2};//获取表的描述对象for(inti=0;itableNames.length;i++){TableNametableName=TableName.valueOf(Bytes.toBytes(tableNames[i]));//删除if(i==0){ColumnFamilyDescriptorBuilderbuildFamily=ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(data));//构建Builder对象buildFamily.setBlockCacheEnabled(false);buildFamily.setBlocksize(1024*1024*1);buildFamily.setCompressionType(Compression.Algorithm.GZ);ColumnFamilyDescriptorfamily=buildFamily.build();admin.modifyColumnFamily(tableName,family);//修改admin.deleteColumnFamily(tableName,Bytes.toBytes(data1));//删除}else{ColumnFamilyDescriptorBuilderbuildFamily=ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(data1));//构建Builder对象buildFamily.setMinVersions(2);buildFamily.setMaxVersions(5);buildFamily.setInMemory(true);buildFamily.setTimeToLive(60*60*24);ColumnFamilyDescriptorfamily=buildFamily.build();admin.modifyColumnFamily(tableName,family);admin.deleteColumnFamily(tableName,Bytes.toBytes(data));}}/*********End*********/}}---------------------------------------------------------------------第3关:禁用表、启用表、删除表packagestep3;importjava.io.IOException;importorg.apache.hadoop.conf.*;importorg.apache.hadoop.hbase.*;importorg.apache.hadoop.hbase.client.*;importorg.apache.hadoop.hbase.util.*;publicclassTask{privatestaticConfigurationconf;privatestaticConnectionconn;static{//静态代码块初始化config对象try{if(conf==null){conf=HBaseConfiguration.create();}}catch(Exceptione){thrownewRuntimeException(e);}}/***刪除表*@paramtableName表名*@throwsException*/publicvoiddeleteTable(StringtableName)throwsException{/*********Begin*********/if(tableName!=null&&!tableName.isEmpty()){Adminadmin=getConnection().getAdmin();TableNametableNameObj=TableName.valueOf(Bytes.toBytes(tableName));admin.disableTable(tableNameObj);admin.deleteTable(tableNameObj);}//closeConnect(conn);/*********End*********/}/***创建表*@paramtableName表名*@paramcolumnNames列族的动态数组*@throwsException*/publicvoidcreateTable(StringtableName,String...columnNames)throwsException{/*********Begin*********/if(tableName!=null&&!tableName.isEmpty()){Adminadmin=getConnection().getAdmin();TableNametableNameObj=TableName.valueOf(Bytes.toBytes(tableName));if(!admin.tableExists(tableNameObj)){TableDescriptorBuilderbuilder=TableDescriptorBuilder.newBuilder(tableNameObj);for(StringcolumnName:columnNames){ColumnFamilyDescriptorBuildercolumnBuild=ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnName));ColumnFamilyDescriptorfamily=columnBuild.build();builder.setColumnFamily(family);}admin.createTable(builder.build());}}/*********End*********/}/***启用表*@paramtableName*@throwsException*/publicvoidenableTable(StringtableName)throwsException{/*********Begin*********/if(tableName!=null&&!tableName.isEmpty()){Adminadmin=getConnection().getAdmin();TableNametableNameObj=TableName.valueOf(Bytes.toBytes(tableName));if(admin.isTableDisabled(tableNameObj)){admin.enableTable(tableNameObj);}}//closeConnect(conn);/*********End*********/}/***禁用表*@paramtableName*/publicvoiddisableTable(StringtableName)throwsException{/*********Begin*********/if(tableName!=null&&!tableName.isEmpty()){Adminadmin=getConnection().getAdmin();TableNametableNameObj=TableName.valueOf(Bytes.toBytes(tableName));admin.disableTable(tableNameObj);}/*********End*********/}/***关闭连接对象*@paramconn*/privatestaticvoidcloseConnect(Connectionconn){//实现关闭连接对象功能if(null!=conn){try{conn.close();}catch(Exceptione){}}}/***获取连接对象*/privatestaticConnectiongetConnection(){//实现一个返回连接对象的函数try{if(conn==null||conn.isClosed()){conn=ConnectionFactory.createConnection(conf);}}catch(Exceptione){e.printStackTrace();}returnconn;}}
本文标题:educoder平台HBase开发:-Java-API-管理表
链接地址:https://www.777doc.com/doc-5242635 .html