专栏已收集头歌大数据所有答案
第一关 JavaAPI获取表的列表:
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(); //使用create()静态方法就可以得到Configuration对象
Connection conn = ConnectionFactory.createConnection(conf); //config为前文的配置对象
Admin admin = conn.getAdmin(); //使用连接对象获取Admin对象
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 *********/
}
}
第二关 修改表:
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(); //使用create()静态方法就可以得到Configuration对象
Connection conn = ConnectionFactory.createConnection(conf); //config为前文的配置对象
Admin admin = conn.getAdmin(); //使用连接对象获取Admin对象
TableName tableName1 = TableName.valueOf("t_emp2");
TableName tableName2 = TableName.valueOf("t_dept2");
ColumnFamilyDescriptorBuilder buildFamily = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("data"));//创建builder对象
buildFamily.setBlocksize(1024*1024);//设置缓存大小
buildFamily.setBlockCacheEnabled(false);//设置关闭缓存,默认是true
buildFamily.setCompressionType(Compression.Algorithm.GZ);//默认是NONE
ColumnFamilyDescriptor family = buildFamily.build();//构建Family对象
admin.modifyColumnFamily(tableName1, family);//调用修改方法,方法接收两个参数:TableName,ColumnFamilyDescriptor
admin.deleteColumnFamily(tableName1, Bytes.toBytes("data1"));//删除表中名为data的列族
ColumnFamilyDescriptorBuilder buildFamily1 = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("data1"));//创建builder对象
buildFamily1.setMinVersions(2);
buildFamily1.setMaxVersions(5);
buildFamily1.setInMemory(true); //默认是false
buildFamily1.setTimeToLive(60*60*24); //以秒为单位,超过这个时间设置的就会在下一次大合并中被删除
ColumnFamilyDescriptor family1 = buildFamily1.build();//构建Family对象
admin.modifyColumnFamily(tableName2, family1);//调用修改方法,方法接收两个参数:TableName,ColumnFamilyDescriptor
admin.deleteColumnFamily(tableName2, Bytes.toBytes("data"));//删除表中名为data的列族
/********* 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 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);
admin.deleteTable(testName);
/********* 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对象
// TableName tableName1 = TableName.valueOf(Bytes.toBytes(tableName));//定义表名
// HTableDescriptor htd = new HTableDescriptor(tableName1);//定义表对象
// for(String s: columnNames){
// htd.addFamily(new HColumnDescriptor(Bytes.toBytes(s)));
// }
//构建一个Test_teacher_info表
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 *********/
}
}
标签:Java,admin,tableName,TableName,对象,头歌,API,conf,import
From: https://blog.csdn.net/gjw3037109961/article/details/140503324