-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhbase_create.java
More file actions
87 lines (65 loc) · 3.2 KB
/
hbase_create.java
File metadata and controls
87 lines (65 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package hbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.junit.Before;
public class hbase_create {
// 与HBase数据库的连接对象
static Connection connection;
// 数据库元数据操作对象
static Admin admin;
@Before
public static void main(String[] args) throws IOException,
MasterNotRunningException,ZooKeeperConnectionException {
// 取得一个数据库连接的配置参数对象
Configuration conf = HBaseConfiguration.create();
// 设置连接参数:HBase数据库所在的主机IP
conf.set("hbase.zookeeper.quorum", "172.1.1.1,192.1.1.1,192.1.1.2");
//conf.set("hbase.zookeeper.quorum", "ly1f-r021701-vm05.local,ly1f-r021701-vm06.local,ly1f-r021701-vm07.local");
System.out.println("---------------连接1-----------------");
// 设置连接参数:HBase数据库使用的端口
conf.set("hbase.zookeeper.property.clientPort", "2181");
System.out.println("---------------连接2-----------------");
conf.set("hbase.master", "192.1.10.1:60000");
//conf.set("zookeeper.znode.parent","/hbase");
conf.set("zookeeper.znode.parent", "/hbase-unsecure");
// 取得一个数据库连接对象
connection = ConnectionFactory.createConnection(conf);
System.out.println("---------------连接3-----------------");
// 取得一个数据库元数据操作对象
admin = connection.getAdmin();
System.out.println("---------------连接4-----------------");
System.out.println("---------------创建表 START-----------------");
// 数据表表名
String tableNameString = "t_test_qcf11";
// 新建一个数据表表名对象
TableName tableName = TableName.valueOf(tableNameString);
System.out.println("tablename:"+tableName);
System.out.println("---------------创建表 STARTing-----------------");
// 如果需要新建的表已经存在
if(admin.tableExists(tableName)){
System.out.println("表已经存在!");
}
// 如果需要新建的表不存在
else{
// 数据表描述对象
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
// 列族描述对象
HColumnDescriptor family= new HColumnDescriptor("base");
System.out.println("---------------创建表 ing-----------------");
// 在数据表中新建一个列族
hTableDescriptor.addFamily(family);
// 新建数据表
admin.createTable(hTableDescriptor);
}
System.out.println("---------------创建表 END-----------------");
}
}