赞
踩
实现列出HBase
中所有表的表名、表是否存在,表是否可用的功能。
- package step1;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import org.apache.hadoop.conf.*;
- import org.apache.hadoop.hbase.*;
- import org.apache.hadoop.hbase.client.*;
- import org.apache.hadoop.hbase.util.*;
-
- public class Task {
-
- public void showTableList() throws Exception {
- /********* Begin *********/
- Configuration conf=HBaseConfiguration.create();
- Connection conn=ConnectionFactory.createConnection(conf);
- Admin admin=conn.getAdmin();
- List<TableDescriptor> tableDescriptors = admin.listTableDescriptors();
- for(TableDescriptor tableDescriptor:tableDescriptors){
- System.out.println("Table:"+tableDescriptor.getTableName());
- System.out.println("\texists:" + admin.tableExists(tableDescriptor.getTableName()));
- System.out.println("\tenabled:" + admin.isTableEnabled(tableDescriptor.getTableName()));
- }
- /********* End *********/
- }
- }
1.对块大小进行调整时主要取决于用户平均读取数据的大小和数据平均键值对规模。
在右侧编辑器begin-end
处编写程序修改表的结构,要求如下:
t_emp2
;
data
的bolckSize
为1M
;data
的块缓存;data
的压缩格式为GZ
;t_dept2
;
data1
的最小单元时间版本为2
,最大时间版本为5
;data1
的激进缓存策略;data1
的生存时间为一天
;t_emp2
的列族data1
;t_dept2
的列族data
。- package step2;
-
- import java.io.IOException;
-
-
- import org.apache.hadoop.conf.*;
- import org.apache.hadoop.hbase.*;
- import org.apache.hadoop.hbase.client.*;
- import org.apache.hadoop.hbase.io.compress.Compression;
- import org.apache.hadoop.hbase.util.*;
-
- public class Task {
-
- public void updateTables()throws Exception{
- /********* Begin *********/
- Configuration conf=HBaseConfiguration.create();
- Connection conn=ConnectionFactory.createConnection(conf);
- Admin admin=conn.getAdmin();
-
- TableName tablename1=TableName.valueOf("t_emp2");
- ColumnFamilyDescriptorBuilder buildFamily1 = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("data"));//构建Builder对象
- buildFamily1.setBlocksize(1024*1024); //修改列族的bolcksize为1M
- buildFamily1.setBlockCacheEnabled(false); //关闭缓存
- buildFamily1.setCompressionType(Compression.Algorithm.GZ); //设置列族的压缩格式为GZ
-
- TableName tablename2=TableName.valueOf("t_dept2");
- ColumnFamilyDescriptorBuilder buildFamily2 = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("data1"));//构建Builder对象
- buildFamily2.setMinVersions(2);//设置列族data1的最小单元时间版本为2
- buildFamily2.setMaxVersions(5);//最大时间版本为5
- buildFamily2.setInMemory(true); //开启列族data1的激进缓存策略
- buildFamily2.setTimeToLive(60*60*24);//设置列族data1的生存时间为一天
-
- ColumnFamilyDescriptor family1 = buildFamily1.build();//构建Family对象
- admin.modifyColumnFamily(tablename1, family1);//调用修改方法,方法接收两个参数:TableName,ColumnFamilyDescriptor
- admin.deleteColumnFamily(tablename1, Bytes.toBytes("data1"));//删除表t_dept2的列族data
-
- ColumnFamilyDescriptor family2 = buildFamily2.build();//构建Family对象
- admin.modifyColumnFamily(tablename2, family2);//调用修改方法,方法接收两个参数:TableName,ColumnFamilyDescriptor
- admin.deleteColumnFamily(tablename2, Bytes.toBytes("data"));//删除表t_emp2的列族data1
- /********* End *********/
- }
- }
请在右侧编辑器begin-end
中编写代码补全函数,实现函数要实现的功能,具体功能请查看右侧注释。
- package step3;
-
- import java.io.IOException;
-
- import org.apache.hadoop.conf.*;
- import org.apache.hadoop.hbase.*;
- import org.apache.hadoop.hbase.client.*;
- import org.apache.hadoop.hbase.util.*;
-
- public class Task {
-
- /**
- * 刪除表
- * @param tableName 表名
- * @throws Exception
- */
- public void deleteTable(String tableName)throws Exception{
- /********* Begin *********/
- Configuration config = new Configuration();
- Connection conn = ConnectionFactory.createConnection(config);
- Admin admin = conn.getAdmin();
- TableName tablename = TableName.valueOf(tableName);
- admin.disableTable(tablename);
- admin.deleteTable(tablename);
-
- /********* End *********/
- }
-
- /**
- * 创建表
- * @param tableName 表名
- * @param columnNames 列族的动态数组
- * @throws Exception
- */
- public void createTable(String tableName,String... columnNames)throws Exception{
- /********* Begin *********/
- Configuration conf = HBaseConfiguration.create(); //使用create()静态方法就可以得到Configuration对象
- Connection conn = ConnectionFactory.createConnection(conf); //config为前文的配置对象
- Admin admin = conn.getAdmin(); //使用连接对象获取Admin对象
- TableDescriptorBuilder test_teacher_info = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
- for(String s: columnNames){
- ColumnFamilyDescriptor of = ColumnFamilyDescriptorBuilder.of(s);
- test_teacher_info.setColumnFamily(of);
- }
- //构建
- TableDescriptor build = test_teacher_info.build();
-
- admin.createTable(build);
-
- /********* End *********/
- }
-
- /**
- * 启用表
- * @param tableName
- * @throws Exception
- */
- public void enableTable(String tableName) throws Exception{
- /********* Begin *********/
- Configuration conf = HBaseConfiguration.create(); //使用create()静态方法就可以得到Configuration对象
- Connection conn = ConnectionFactory.createConnection(conf); //config为前文的配置对象
- Admin admin = conn.getAdmin(); //使用连接对象获取Admin对象
- TableName demoName = TableName.valueOf(Bytes.toBytes(tableName));
- admin.enableTable(demoName);
-
- /********* End *********/
- }
-
- /**
- * 禁用表
- * @param tableName
- */
- public void disableTable(String tableName)throws Exception{
- /********* Begin *********/
- Configuration conf = HBaseConfiguration.create(); //使用create()静态方法就可以得到Configuration对象
- Connection conn = ConnectionFactory.createConnection(conf); //config为前文的配置对象
- Admin admin = conn.getAdmin(); //使用连接对象获取Admin对象
- TableName testName = TableName.valueOf(Bytes.toBytes(tableName));
- admin.disableTable(testName);
-
- /********* End *********/
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。