Part6---Java创建Hbase表
01.先查看HBase中表(base) [root@192 ~]# hbase shellHBase ShellUse "help" to get list of supported commands.Use "exit" to quit this interactive shell.For Reference, please visit: http://hbase.apache.org/2.0/
·
01.先查看HBase中表
(base) [root@192 ~]# hbase shell
HBase Shell
Use "help" to get list of supported commands.
Use "exit" to quit this interactive shell.
For Reference, please visit: http://hbase.apache.org/2.0/book.html#shell
Version 2.1.1, rb60a92d6864ef27295027f5961cb46f9162d7637, Fri Oct 26 19:27:03 PDT 2018
Took 0.0017 seconds
hbase(main):001:0> list
TABLE
Gadaite
STUDENT
SYSTEM.CATALOG
SYSTEM.CHILD_LINK
SYSTEM.FUNCTION
SYSTEM.LOG
SYSTEM.MUTEX
SYSTEM.SEQUENCE
SYSTEM.STATS
SYSTEM.TASK
StudentAndCourse
11 row(s)
Took 0.2929 seconds
=> ["Gadaite", "STUDENT", "SYSTEM.CATALOG", "SYSTEM.CHILD_LINK", "SYSTEM.FUNCTION", "SYSTEM.LOG", "SYSTEM.MUTEX", "SYSTEM.SEQUENCE", "SYSTEM.STATS", "SYSTEM.TASK", "StudentAndCourse"]
hbase(main):002:0>
02.使用Hadoop的HBaseApi创建表
2.1 方式一
该方式可能已经被启用
Family:为列簇的意思
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
public class CreateHbaseTable {
public static void main(String[] args) throws Exception{
Configuration conf = HBaseConfiguration.create();
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.1.10:2181");
Connection con = ConnectionFactory.createConnection(conf);
Admin admin = con.getAdmin();
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("ideatest".getBytes()));
HColumnDescriptor f1 = new HColumnDescriptor("F1");
HColumnDescriptor f2 = new HColumnDescriptor("F2");
f2.setVersions(1, 3);
hTableDescriptor.addFamily(f1).addFamily(f2);
admin.createTable(hTableDescriptor);
admin.close();
con.close();
}
}
在hbase shell中查看结果:
生成新表:ideatest
hbase(main):002:0> list
TABLE
Gadaite
STUDENT
SYSTEM.CATALOG
SYSTEM.CHILD_LINK
SYSTEM.FUNCTION
SYSTEM.LOG
SYSTEM.MUTEX
SYSTEM.SEQUENCE
SYSTEM.STATS
SYSTEM.TASK
StudentAndCourse
ideatest
12 row(s)
Took 0.0138 seconds
=> ["Gadaite", "STUDENT", "SYSTEM.CATALOG", "SYSTEM.CHILD_LINK", "SYSTEM.FUNCTION", "SYSTEM.LOG", "SYSTEM.MUTEX", "SYSTEM.SEQUENCE", "SYSTEM.STATS", "SYSTEM.TASK", "StudentAndCourse", "ideatest"]
2.2 方式二:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
public class CreateHbaseTable2 {
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum","192.168.1.10:2181");
Connection conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
if(!admin.tableExists(TableName.valueOf("IDEATEST"))){
TableName IDEATEST = TableName.valueOf("IDEATEST");
// 表描述构造器
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(IDEATEST);
// 列族描述器构造器
ColumnFamilyDescriptorBuilder col1 = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("col1"));
ColumnFamilyDescriptorBuilder col2 = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("col2"));
// 列描述器
ColumnFamilyDescriptor build1 = col1.build();
ColumnFamilyDescriptor build2 = col2.build();
// 添加列簇
TableDescriptorBuilder builder = tableDescriptorBuilder.setColumnFamily(build1).setColumnFamily(build2);
// 表描述器
TableDescriptor tableDescriptor = builder.build();
// 创建表
admin.createTable(tableDescriptor);
}
else {
System.out.println("表已存在");
}
admin.close();
conn.close();
}
}
在Hbase shell中查看结果
已生成IDEATEST表
hbase(main):004:0> list
TABLE
Gadaite
IDEATEST
STUDENT
SYSTEM.CATALOG
SYSTEM.CHILD_LINK
SYSTEM.FUNCTION
SYSTEM.LOG
SYSTEM.MUTEX
SYSTEM.SEQUENCE
SYSTEM.STATS
SYSTEM.TASK
StudentAndCourse
ideatest
13 row(s)
Took 0.0129 seconds
=> ["Gadaite", "IDEATEST", "STUDENT", "SYSTEM.CATALOG", "SYSTEM.CHILD_LINK", "SYSTEM.FUNCTION", "SYSTEM.LOG", "SYSTEM.MUTEX", "SYSTEM.SEQUENCE", "SYSTEM.STATS", "SYSTEM.TASK", "StudentAndCourse", "ideatest"]
03.在hbase shell中删除表
不是直接使用下面的方式删除整个表
hbase(main):005:0> drop "ideatest"
ERROR: Table ideatest is enabled. Disable it first.
For usage try 'help "drop"'
Took 0.0283 seconds
hbase(main):006:0> drop table "ideatest"
NoMethodError: undefined method `table' for main:Object
3.1 先将表设定为无效状态
disable "ideatest"
3.2 判断表是否无效
is_disabled "ideatest"
3.3 查看结果
ideatest表已经被删除
hbase(main):007:0> disable "ideatest"
Took 0.4582 seconds
hbase(main):008:0> is_disabled "ideatest"
true
Took 0.0067 seconds
=> 1
hbase(main):009:0> drop "ideatest"
Took 0.2399 seconds
hbase(main):010:0> list
TABLE
Gadaite
IDEATEST
STUDENT
SYSTEM.CATALOG
SYSTEM.CHILD_LINK
SYSTEM.FUNCTION
SYSTEM.LOG
SYSTEM.MUTEX
SYSTEM.SEQUENCE
SYSTEM.STATS
SYSTEM.TASK
StudentAndCourse
12 row(s)
Took 0.0098 seconds
=> ["Gadaite", "IDEATEST", "STUDENT", "SYSTEM.CATALOG", "SYSTEM.CHILD_LINK", "SYSTEM.FUNCTION", "SYSTEM.LOG", "SYSTEM.MUTEX", "SYSTEM.SEQUENCE", "SYSTEM.STATS", "SYSTEM.TASK", "StudentAndCourse"]
更多推荐
已为社区贡献3条内容
所有评论(0)