释放双眼,带上耳机,听听看~!
创建一个学生宿舍管理系统
数据表表名:student_info
列族1:students
列族2:dormitorys
列族3:staff_members
代码如下:
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 1package myhbase;
2import java.io.IOException;
3import org.apache.hadoop.conf.Configuration;
4import org.apache.hadoop.hbase.HBaseConfiguration;
5import org.apache.hadoop.hbase.HColumnDescriptor;
6import org.apache.hadoop.hbase.HTableDescriptor;
7import org.apache.hadoop.hbase.MasterNotRunningException;
8import org.apache.hadoop.hbase.ZooKeeperConnectionException;
9import org.apache.hadoop.hbase.client.HBaseAdmin;
10public class Student_info {
11 public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
12 String tableName = "student_info";
13 String columnFamily1 = "students";
14 String columnFamily2 = "dormitorys";
15 String columnFamily3 = "staff_members";
16 create(tableName, columnFamily1,columnFamily2,columnFamily3);
17 }
18
19 //创建连接
20 public static Configuration getConfiguration() {
21 Configuration conf = HBaseConfiguration.create();
22 conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
23 conf.set("hbase.zookeeper.quorum", "localhost");
24 return conf;
25 }
26
27 //创建表方法
28 public static void create(String tableName, String columnFamily1, String columnFamily2, String columnFamily3)
29 throws MasterNotRunningException, ZooKeeperConnectionException,
30 IOException {
31 HBaseAdmin hBaseAdmin = new HBaseAdmin(getConfiguration());
32 if (hBaseAdmin.tableExists(tableName)) {
33 System.err.println("Table exists!");
34 } else {
35 HTableDescriptor tableDesc = new HTableDescriptor(tableName);
36 tableDesc.addFamily(new HColumnDescriptor(columnFamily1));
37 tableDesc.addFamily(new HColumnDescriptor(columnFamily2));
38 tableDesc.addFamily(new HColumnDescriptor(columnFamily3));
39 hBaseAdmin.createTable(tableDesc);
40 System.err.println("Create Table SUCCESS!");
41 }
42 }
43}
44
45
46