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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
1package myhbase;
2import java.io.IOException;
3import java.util.logging.Filter;
4
5import javax.xml.crypto.dsig.keyinfo.KeyValue;
6
7import org.apache.hadoop.conf.Configuration;
8import org.apache.hadoop.hbase.HBaseConfiguration;
9import org.apache.hadoop.hbase.HColumnDescriptor;
10import org.apache.hadoop.hbase.HTableDescriptor;
11import org.apache.hadoop.hbase.MasterNotRunningException;
12import org.apache.hadoop.hbase.ZooKeeperConnectionException;
13import org.apache.hadoop.hbase.client.HBaseAdmin;
14import org.apache.hadoop.hbase.client.HTable;
15import org.apache.hadoop.hbase.client.Result;
16import org.apache.hadoop.hbase.client.ResultScanner;
17import org.apache.hadoop.hbase.client.Scan;
18import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
19import org.apache.hadoop.hbase.filter.RegexStringComparator;
20import org.apache.hadoop.hbase.filter.RowFilter;
21import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
22import org.apache.hadoop.hbase.filter.SubstringComparator;
23import org.apache.hadoop.hbase.util.Bytes;
24
25import com.google.common.collect.Table;
26public class GetData{
27  public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
28      String tableName = "student_info";
29      String columnFamily1 = "students";
30      String column1 = "student_id";
31
32      String val1 = "123456789";
33      System.out.println(val1);
34     
35      String columnFamily2 = "dormitorys" ;
36      String column2 = "dormitory";
37      String val2 = "Nanyuan Building4";
38     
39      getStudentsinfo(tableName,columnFamily1, column1,val1);
40      //getStaffsinfo(tableName,columnFamily2, column2,val2);
41      //getAll(tableName);
42  }
43
44  public static Configuration getConfiguration() {
45      Configuration conf = HBaseConfiguration.create();
46      conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
47      conf.set("hbase.zookeeper.quorum", "localhost");
48      return conf;
49  }
50 
51  public static void getStudentsinfo(String tableName, String columnFamily1, String column1, String val1)
52          throws MasterNotRunningException, ZooKeeperConnectionException,
53          IOException {
54      HTable table =new HTable(getConfiguration(), tableName);
55      Scan scan =new Scan();
56      SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(columnFamily1),Bytes.toBytes(column1),CompareOp.EQUAL,
57              new SubstringComparator(val1));
58      filter.setFilterIfMissing(true);
59      scan.setFilter(filter);
60      ResultScanner scanner = table.getScanner(scan);
61     
62      for(Result result:scanner){    
63          System.out.println(Bytes.toString(result.getRow()));
64          byte[] value1 = result.getValue(Bytes.toBytes("students"),Bytes.toBytes("student_name"));
65          byte[] value2 = result.getValue(Bytes.toBytes("students"),Bytes.toBytes("gender"));
66          byte[] value3 = result.getValue(Bytes.toBytes("students"),Bytes.toBytes("dept"));
67          byte[] value4 = result.getValue(Bytes.toBytes("students"),Bytes.toBytes("tel"));
68          byte[] value5 = result.getValue(Bytes.toBytes("students"),Bytes.toBytes("admission_time"));
69          byte[] value6 = result.getValue(Bytes.toBytes("dormitorys"),Bytes.toBytes("hostel_No."));
70          System.out.println("student_name:"+Bytes.toString(value1));
71          System.out.println("gender:"+Bytes.toString(value2));
72          System.out.println("dep:"+Bytes.toString(value3));
73          System.out.println("tel:"+Bytes.toString(value4));
74          System.out.println("admission_time:"+Bytes.toString(value5));
75          System.out.println("hostel_No.:"+Bytes.toString(value6));
76          }
77      System.out.println("===========get students info end===========");
78  }
79 
80  public static void getStaffsinfo(String tableName, String columnFamily2, String column2, String val2)
81          throws MasterNotRunningException, ZooKeeperConnectionException,
82          IOException {
83      HTable table =new HTable(getConfiguration(), tableName);
84      Scan scan =new Scan();
85      SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(columnFamily2),Bytes.toBytes(column2),CompareOp.EQUAL,
86              new SubstringComparator(val2));
87      filter.setFilterIfMissing(true);
88      scan.setFilter(filter);
89      ResultScanner scanner = table.getScanner(scan);
90     
91      for(Result result:scanner){    
92          System.out.println(Bytes.toString(result.getRow()));
93          byte[] value1 = result.getValue(Bytes.toBytes("staff members"),Bytes.toBytes("staff name"));
94          byte[] value2 = result.getValue(Bytes.toBytes("staff members"),Bytes.toBytes("staff age"));
95          byte[] value3 = result.getValue(Bytes.toBytes("staff members"),Bytes.toBytes("staff gender"));
96          byte[] value4 = result.getValue(Bytes.toBytes("staff members"),Bytes.toBytes("staff tel"));
97          System.out.println(Bytes.toString(value1));
98          System.out.println(Bytes.toString(value2));
99          System.out.println(Bytes.toString(value3));
100         System.out.println(Bytes.toString(value4));
101         System.out.println("---------------");
102         }
103     System.out.println("===========get staffsindo end===========");
104 }
105
106 public static void getAll(String tableName)throws MasterNotRunningException, ZooKeeperConnectionException,
107 IOException{
108     Configuration conf = HBaseConfiguration.create();
109     HTable hTable = new HTable(conf,tableName);
110     Scan scan =new Scan();
111     //ResultScanner scanner1 = hTable.getScanner(scan);
112     //for (Result res : scanner1) {
113       //     System.out.println(res);
114        //}
115     try {
116            Scan scan1 = new Scan();
117            ResultScanner rss = hTable.getScanner(scan1);
118            
119            for(Result r:rss){
120             System.out.println("\n row: "+new String(r.getRow()));
121              
122             for(org.apache.hadoop.hbase.KeyValue kv:r.raw()){
123              
124              System.out.println("family=>"+new String(kv.getFamily(),"utf-8")
125                    +"  value=>"+new String(kv.getValue(),"utf-8")
126              +"  qualifer=>"+new String(kv.getQualifier(),"utf-8")
127              +"  timestamp=>"+kv.getTimestamp());    
128             }
129            }
130            rss.close();
131           } catch (IOException e) {
132            // TODO Auto-generated catch block
133            e.printStackTrace();
134           }  
135           System.out.println("=============end show All Records=============");
136          }
137
138
139
140 }
141
142
143
144
145
146
147

根据提供的宿舍号码来查询该宿舍下宿舍成员信息:

例如:查询115宿舍的成员信息:


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
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;
10import org.apache.hadoop.hbase.client.HTable;
11import org.apache.hadoop.hbase.client.Result;
12import org.apache.hadoop.hbase.client.ResultScanner;
13import org.apache.hadoop.hbase.client.Scan;
14import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
15import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
16import org.apache.hadoop.hbase.filter.SubstringComparator;
17import org.apache.hadoop.hbase.util.Bytes;
18public class ShowData{
19  public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
20      String tableName = "student_info";
21
22      String columnFamily2 = "dormitorys";
23      String column = "hostel_No.";
24      String val = "115";
25      find(tableName,columnFamily2, column,val);
26  }
27
28  public static Configuration getConfiguration() {
29      Configuration conf = HBaseConfiguration.create();
30      conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
31      conf.set("hbase.zookeeper.quorum", "localhost");
32      return conf;
33  }
34 
35  public static void find(String tableName, String columnFamily2, String column, String val)
36          throws MasterNotRunningException, ZooKeeperConnectionException,
37          IOException {
38      HTable table =new HTable(getConfiguration(), tableName);
39      Scan scan =new Scan();
40      SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(columnFamily2),Bytes.toBytes(column),CompareOp.EQUAL,
41              new SubstringComparator(val));
42      filter.setFilterIfMissing(true);
43      scan.setFilter(filter);
44      ResultScanner scanner = table.getScanner(scan);
45     
46      for(Result result:scanner){    
47          System.out.println(Bytes.toString(result.getRow()));
48          byte[] value1 = result.getValue(Bytes.toBytes("students"),Bytes.toBytes("student_id"));
49          byte[] value2 = result.getValue(Bytes.toBytes("students"),Bytes.toBytes("student_name"));
50          System.out.println(Bytes.toString(value1));
51          System.out.println(Bytes.toString(value2));
52          System.out.println("---------------");
53          }
54      System.out.println("===========find end===========");
55  }
56 
57 
58}
59
60
61

 

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

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

2021-10-23 10:13:25

安全运维

设计模式的设计原则

2021-12-12 17:36:11

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