HBase中Java操作数据库增删改查——增加数据

释放双眼,带上耳机,听听看~!

已经创建了一个学生宿舍管理系统

数据表表名: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

 

给TA打赏
共{{data.count}}人
人已打赏
安全运维

OpenSSH-8.7p1离线升级修复安全漏洞

2021-10-23 10:13:25

安全运维

设计模式的设计原则

2021-12-12 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索