HBase中Java操作数据库增删改查——删除数据/删除数据表

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

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

数据表表名:student_info

列族1:students

列族2:dormitorys

列族3:staff_members

例如:删除数据表中行键为001的数据:


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
1package myhbase;
2
3import java.io.IOException;
4import java.util.ArrayList;
5
6import org.apache.hadoop.conf.Configuration;
7import org.apache.hadoop.hbase.HBaseConfiguration;
8import org.apache.hadoop.hbase.client.Delete;
9import org.apache.hadoop.hbase.client.HTable;
10import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.Column;
11import org.apache.hadoop.hbase.util.Bytes;
12
13import com.sun.xml.bind.v2.schemagen.xmlschema.List;
14
15public class DeleteData {
16  public static void main(String[] args) throws IOException {
17     
18      //deletedata("student_info");
19      deleterowkey("student_info","001");
20  }
21 
22  public static Configuration getConfiguration() {
23      Configuration conf = HBaseConfiguration.create();
24      conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
25      conf.set("hbase.zookeeper.quorum", "localhost");
26      return conf;
27  }
28 
29  public static void deletedata(String tableName) throws IOException{
30      Configuration conf = HBaseConfiguration.create();
31      HTable table = new HTable(conf, "student_info");
32     
33      Delete delete = new Delete(Bytes.toBytes("001"));
34      delete.deleteColumn(Bytes.toBytes("students"), Bytes.toBytes("student_name"));
35      table.delete(delete);
36      table.close();
37      System.out.println("data deleted!");
38  }
39 
40  public static void deleterowkey(String tableName,String rowkey){
41      try{
42          Configuration conf = HBaseConfiguration.create();
43          HTable table = new HTable(conf, "student_info");
44          ArrayList list = new ArrayList();
45          Delete d1 = new Delete(rowkey.getBytes());
46          list.add(d1);
47          table.delete(list);
48          System.out.println("rowkey deleted!");
49      }catch(IOException e){
50          e.printStackTrace();
51      }
52  }
53 
54}
55
56
57

删除数据表student_info:


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
1package myhbase;
2
3import java.io.IOException;
4
5import org.apache.hadoop.conf.Configuration;
6import org.apache.hadoop.hbase.HBaseConfiguration;
7import org.apache.hadoop.hbase.client.HBaseAdmin;
8
9
10public class DeleteTable {
11  public static void main(String[] args) throws IOException{
12      deletetable("student_info");
13  }
14
15  public static Configuration getConfiguration() {
16      Configuration conf = HBaseConfiguration.create();
17      conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
18      conf.set("hbase.zookeeper.quorum", "localhost");
19      return conf;
20  }
21 
22  public static void deletetable(String tableName) throws IOException{
23      Configuration conf = HBaseConfiguration.create();
24      HBaseAdmin admin = new HBaseAdmin(conf);
25      if(admin.tableExists(tableName)){
26          admin.disableTable(tableName);
27          admin.deleteTable(tableName);
28          System.out.println("========Table deleted=======");  
29      }else{
30          System.out.println("The table was not found!");
31      }
32
33  }
34}
35
36

 

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

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

2021-10-23 10:13:25

安全运维

设计模式的设计原则

2021-12-12 17:36:11

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