一、
几个主要 Hbase API 类和数据模型之间的对应关系:
1、 HBaseAdmin
关系: org.apache.hadoop.hbase.client.HBaseAdmin
作用:提供了一个接口来管理 HBase 数据库的表信息。它提供的方法包括:创建表,删 除表,列出表项,使表有效或无效,以及添加或删除表列族成员等。
2、 HBaseConfiguration
关系: org.apache.hadoop.hbase.HBaseConfiguration
作用:对 HBase 进行配置
3、 HTableDescriptor
关系: org.apache.hadoop.hbase.HTableDescriptor
作用:包含了表的名字极其对应表的列族
4、 HColumnDescriptor
关系: org.apache.hadoop.hbase.HColumnDescriptor
作用:维护着关于列族的信息,例如版本号,压缩设置等。它通常在创建表或者为表添 加列族的时候使用。列族被创建后不能直接修改,只能通过删除然后重新创建的方式。
列族被删除的时候,列族里面的数据也会同时被删除。
5、 HTable
关系: org.apache.hadoop.hbase.client.HTable
作用:可以用来和 HBase 表直接通信。此方法对于更新操作来说是非线程安全的。
6、 Put
关系: org.apache.hadoop.hbase.client.Put
作用:用来对单个行执行添加操作
7、 Get
关系: org.apache.hadoop.hbase.client.Get
作用:用来获取单个行的相关信息
8、 Result
关系: org.apache.hadoop.hbase.client.Result
作用:存储 Get 或者 Scan 操作后获取表的单行值。使用此类提供的方法可以直接获取值 或者各种 Map 结构( key-value 对)
二、具体增删改查 代码具体实现:
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | package com.zuoke; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * Created by 亚男 on 2018/8/14. */ public class Hbasedemo { static Configuration conf = null; private static final String ZKconnect = "192.168.190.131:2181,192.168.190.132:2181,192.168.190.133:2181"; static { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", ZKconnect); } public static void main(String[] args) { Hbasedemo a = new Hbasedemo(); //声明表名对象 String[] family = {"cf1", "cf2"}; TableName tableName = TableName.valueOf("test001".getBytes()); //声明表描述器对象 HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName); //声明列族描述器对象 for (int num = 0; num < family.length; num++) { HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(family[num].getBytes()); //吧列添加到表里面 hTableDescriptor.addFamily(hColumnDescriptor); } } /** * 创建表方法 * 参数类型Tablename tablename,String[] family * 返回值类型:boolean */ public boolean createTable(TableName tablename, String[] family) throws IOException { HBaseAdmin hBaseAdmin = new HBaseAdmin(conf); HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename); for (int i = 0; i < family.length; i++) { hTableDescriptor.addFamily(new HColumnDescriptor(family[i])); System.out.println("11111111111111" + family[i]); } if (hBaseAdmin.tableExists(tablename)) { return false; } else { hBaseAdmin.createTable(hTableDescriptor); return true; } } /** * 创建表方法 * 参数类型Tablename tablename,HTableDescriptor hTableDescriptor * 返回值类型boolean */ public Boolean createTable(TableName tablename, HTableDescriptor hTableDescriptor) throws IOException { HBaseAdmin hBaseAdmin = new HBaseAdmin(conf); boolean tableExistOrNot = hBaseAdmin.tableExists(tablename); if (tableExistOrNot == true) { } else { hBaseAdmin.createTable(hTableDescriptor); } return hBaseAdmin.tableExists(tablename); } /** * 创建表描述器 */ public void descTable(TableName tableName) throws IOException { //HBASE操作对象 HBaseAdmin admin = new HBaseAdmin(conf); //表对象 HTable hTable = new HTable(conf, tableName); //表描述器 HTableDescriptor hTableDescriptor = hTable.getTableDescriptor(); //获取列族对象 HColumnDescriptor[] columnFamilies = hTableDescriptor.getColumnFamilies(); //循环遍历列族对象 for (HColumnDescriptor h : columnFamilies ) { System.out.println(Bytes.toString(h.getName())); } } /** * 替换所有列族 */ }
|
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
|