释放双眼,带上耳机,听听看~!
已经创建了一个学生宿舍管理系统
数据表表名: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
47
48 1package myhbase;
2import java.io.IOException;
3import org.apache.hadoop.conf.Configuration;
4import org.apache.hadoop.hbase.HBaseConfiguration;
5import org.apache.hadoop.hbase.MasterNotRunningException;
6import org.apache.hadoop.hbase.ZooKeeperConnectionException;
7import org.apache.hadoop.hbase.client.HTable;
8import org.apache.hadoop.hbase.client.Put;
9import org.apache.hadoop.hbase.util.Bytes;
10public class PutData {
11 public static void main(String[] args) throws MasterNotRunningException,
12 ZooKeeperConnectionException, IOException {
13 String tableName = "student_info";
14 String columnFamily1 = "students";
15 String columnFamily2 = "dormitorys";
16 String columnFamily3 = "staff_members";
17 put(tableName, "001", columnFamily1, "student_id", "164804098");
18 put(tableName, "001", columnFamily1, "student_name", "tingtingrugai");
19 put(tableName, "001", columnFamily1, "gender", "male");
20 put(tableName, "001", columnFamily1, "tel", "123456789");
21 put(tableName, "001", columnFamily2, "hostel_No.", "115");
22 put(tableName, "001", columnFamily2, "bed", "3");
23 put(tableName, "001", columnFamily3, "staff_nameA", "zhangsan");
24 put(tableName, "001", columnFamily3, "staff_ageA", "40");
25 put(tableName, "001", columnFamily3, "staff_genderA", "male");
26
27 }
28
29 public static Configuration getConfiguration() {
30 Configuration conf = HBaseConfiguration.create();
31 conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
32 conf.set("hbase.zookeeper.quorum", "localhost");
33 return conf;
34 }
35 public static void put(String tableName, String row, String columnFamily,
36 String column, String data) throws IOException {
37 HTable table = new HTable(getConfiguration(), tableName);
38 Put put = new Put(Bytes.toBytes(row));
39 put.add(Bytes.toBytes(columnFamily),
40 Bytes.toBytes(column),
41 Bytes.toBytes(data));
42 table.put(put);
43 System.err.println("SUCCESS");
44 }
45}
46
47
48