1 package cn.hbase.demo; 2
3 import java.io.IOException; 4 import java.util.Iterator; 5
6 import org.apache.hadoop.conf.Configuration; 7 import org.apache.hadoop.hbase.Cell; 8 import org.apache.hadoop.hbase.CellScanner; 9 import org.apache.hadoop.hbase.CellUtil; 10 import org.apache.hadoop.hbase.HBaseConfiguration; 11 import org.apache.hadoop.hbase.HColumnDescriptor; 12 import org.apache.hadoop.hbase.HTableDescriptor; 13 import org.apache.hadoop.hbase.TableName; 14 import org.apache.hadoop.hbase.client.Admin; 15 import org.apache.hadoop.hbase.client.Connection; 16 import org.apache.hadoop.hbase.client.ConnectionFactory; 17 import org.apache.hadoop.hbase.client.Delete; 18 import org.apache.hadoop.hbase.client.Get; 19 import org.apache.hadoop.hbase.client.Put; 20 import org.apache.hadoop.hbase.client.Result; 21 import org.apache.hadoop.hbase.client.ResultScanner; 22 import org.apache.hadoop.hbase.client.Scan; 23 import org.apache.hadoop.hbase.client.Table; 24 import org.apache.hadoop.hbase.regionserver.StripeStoreFileManager; 25 import org.apache.hadoop.hbase.util.Bytes; 26
27 /**
28 * 增删改查 注意hbase中一行指的是一个列族,所以一行可能含有多条数据 29 * 30 * @author Tele 31 * 32 */
33
34 public class CrudTable { 35 private static Connection conn; 36 private static Configuration conf; 37 private static Admin admin; 38 static { 39 try { 40 conf = HBaseConfiguration.create(); 41 conn = ConnectionFactory.createConnection(conf); 42 admin = conn.getAdmin(); 43 } catch (IOException e) { 44 e.printStackTrace(); 45 } 46
47 } 48
49 public static boolean isExistTable(String tableName) throws IOException { 50 return admin.tableExists(TableName.valueOf(tableName)); 51 } 52
53 /**
54 * 创建表 55 * 56 * @param tableName 57 * @param columnFamily 列族,创建表时可以传递多个列族过来 58 * @throws IOException 59 */
60 public static void createTable(String tableName, String... columnFamily) throws IOException { 61
62 if (isExistTable(tableName)) { 63 System.out.println("已经存在表" + tableName); 64 return; 65 } else { 66 HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName)); 67 for (String cf : columnFamily) { 68 tableDescriptor.addFamily(new HColumnDescriptor(cf)); 69 } 70 admin.createTable(tableDescriptor); 71 System.out.println("成功创建了表" + tableName); 72 } 73 } 74
75 /**
76 * 创建多版本的表 77 * 78 * @param tableName 79 * @param columnFamily 列族,创建表时可以传递多个列族过来 80 * @throws IOException 81 */
82 public static void createTableMultiVersion(String tableName, String veriosn, String... columnFamily) 83 throws IOException { 84
85 if (isExistTable(tableName)) { 86 System.out.println("已经存在表" + tableName); 87 return; 88 } else { 89 HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName)); 90 for (String cf : columnFamily) { 91 tableDescriptor.addFamily(new HColumnDescriptor(cf).setVersions(1, 3)); 92 } 93 admin.createTable(tableDescriptor); 94 System.out.println("成功创建了表" + tableName); 95 } 96 } 97
98 /**
99 * 删除表之前必须先禁用表 100 * 101 * @param tableName 102 * @throws IOException 103 */
104 public static void dropTable(String tableName) throws IOException { 105 if (isExistTable(tableName)) { 106 admin.disableTable(TableName.valueOf(tableName)); 107 admin.deleteTable(TableName.valueOf(tableName)); 108 System.out.println("表" + tableName + "删除成功"); 109 } else { 110 System.out.println("表" + tableName + "不存在"); 111 } 112
113 } 114
115 /**
116 * 添加一条数据 117 * 118 * @param tableName 119 * @param rowKey 行键 120 * @param cf 列族 121 * @param cn 列名 122 * @param value 123 * @throws IOException 124 */
125 public static void addRow(String tableName, String rowKey, String cf, String cn, String value) throws IOException { 126 // 先判断表是否存在
127 if (isExistTable(tableName)) { 128 Table table = conn.getTable(TableName.valueOf(tableName)); 129 Put put = new Put(Bytes.toBytes(rowKey)); 130 put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value)); 131
132 table.put(put); 133 System.out.println("成功插入一条数据"); 134 table.close(); 135 } else { 136 System.out.println("待插入的表不存在"); 137 } 138 } 139
140 /**
141 * 删除一条数据 142 * 143 * @param tableName 144 * @param rowKey 145 * @param cf 146 * @param cn 147 * @throws IOException 148 */
149 public static void deleteRow(String tableName, String rowKey, String cf, String cn) throws IOException { 150 if (isExistTable(tableName)) { 151 Table table = conn.getTable(TableName.valueOf(tableName)); 152 Delete delete = new Delete(Bytes.toBytes(rowKey)); 153 delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn)); 154 table.delete(delete); 155 System.out.println("删除成功"); 156 table.close(); 157 } else { 158 System.out.println("表不存在"); 159 } 160
161 } 162
163 /**
164 * 删除一行数据,即删除该行数据对应的一个列族的数据 165 * 166 * @param tableName 167 * @param rowKey 168 * @param cf 169 * @throws IOException 170 */
171 public static void deleteMultiRow(String tableName, String rowKey, String cf) throws IOException { 172 if (isExistTable(tableName)) { 173 Table table = conn.getTable(TableName.valueOf(tableName)); 174 Delete delete = new Delete(Bytes.toBytes(rowKey)); 175 delete.addFamily(Bytes.toBytes(cf)); 176 table.delete(delete); 177 System.out.println("删除成功"); 178 table.close(); 179 } else { 180 System.out.println("表不存在"); 181 } 182 } 183
184 /**
185 * 查询指定列族.列名的数据,一个reuslt代表一个列的全部数据 186 * 187 * @param tableName 188 * @param rowKey 189 * @param cf 190 * @param cn 191 * @throws IOException 192 */
193 public static void getRow(String tableName, String rowKey, String cf, String cn) throws IOException { 194 if (isExistTable(tableName)) { 195 Table table = conn.getTable(TableName.valueOf(tableName)); 196 Get get = new Get(Bytes.toBytes(rowKey)); 197 get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn)); 198 Result result = table.get(get); 199
200 // 遍历result
201 CellScanner cellScanner = result.cellScanner(); 202 while (cellScanner.advance()) { 203 Cell cell = cellScanner.current(); 204 System.out.println("行键:" + Bytes.toString(CellUtil.copyRow(cell))); 205 System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell))); 206 System.out.println("列名:" + Bytes.toString(CellUtil.cloneQualifier(cell))); 207 System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell))); 208 } 209 table.close(); 210 } else { 211 System.out.println("表不存在"); 212 } 213 } 214
215 /**
216 * 查询指定列多个版本的数据 217 * 218 * @param tableName 219 * @param rowKey 220 * @param cf 221 * @param cn 222 * @throws IOException 223 */
224 public static void getRowMultiVersion(String tableName, String rowKey, String cf, String cn) throws IOException { 225 if (isExistTable(tableName)) { 226 Table table = conn.getTable(TableName.valueOf(tableName)); 227 Get get = new Get(Bytes.toBytes(rowKey)); 228
229 // get.setMaxVersions();
230 get.readAllVersions(); 231
232 get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn)); 233 Result result = table.get(get); 234
235 // 遍历result
236 CellScanner cellScanner = result.cellScanner(); 237 while (cellScanner.advance()) { 238 Cell cell = cellScanner.current(); 239 System.out.println("行键:" + Bytes.toString(CellUtil.copyRow(cell))); 240 System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell))); 241 System.out.println("列名:" + Bytes.toString(CellUtil.cloneQualifier(cell))); 242 System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell))); 243 } 244 table.close(); 245 } else { 246 System.out.println("表不存在"); 247 } 248 } 249
250 /**
251 * 查询指定列族数据 一个reuslt代表一个行键的全部数据 252 * 253 * @param tableName 254 * @param rowKey 255 * @param cf 256 * @param cn 257 * @throws IOException 258 */
259 public static void getMultiRows(String tableName, String rowKey, String cf) throws IOException { 260 if (isExistTable(tableName)) { 261 Table table = conn.getTable(TableName.valueOf(tableName)); 262 Get get = new Get(Bytes.toBytes(rowKey)); 263 get.addFamily(Bytes.toBytes(cf)); 264 Result result = table.get(get); 265
266 // 遍历result
267 CellScanner cellScanner = result.cellScanner(); 268 while (cellScanner.advance()) { 269 Cell cell = cellScanner.current(); 270 System.out.println("行键:" + Bytes.toString(CellUtil.copyRow(cell))); 271 System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell))); 272 System.out.println("列名:" + Bytes.toString(CellUtil.cloneQualifier(cell))); 273 System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell))); 274 System.out.println(); 275 } 276 table.close(); 277 } else { 278 System.out.println("表不存在"); 279 } 280 } 281
282 /**
283 * 查询全部数据 一个reuslt代表一个列族的全部数据 284 * 285 * @param tableName 286 * @throws IOException 287 */
288 public static void scanTable(String tableName) throws IOException { 289 if (isExistTable(tableName)) { 290 Table table = conn.getTable(TableName.valueOf(tableName)); 291
292 Scan scan = new Scan(); 293 ResultScanner scanner = table.getScanner(scan); 294 // 遍历scanner
295 Iterator<Result> iterator = scanner.iterator(); 296 while (iterator.hasNext()) { 297 Result result = iterator.next(); 298 // 遍历result
299 CellScanner cellScanner = result.cellScanner(); 300 while (cellScanner.advance()) { 301 Cell cell = cellScanner.current(); 302 System.out.println("行键:" + Bytes.toString(CellUtil.copyRow(cell))); 303 System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell))); 304 System.out.println("列名:" + Bytes.toString(CellUtil.cloneQualifier(cell))); 305 System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell))); 306 } 307 System.out.println(); 308 } 309
310 table.close(); 311 } else { 312 System.out.println("表不存在"); 313 } 314
315 } 316
317 /**
318 * 从指定行scan表 319 * 320 * @param tableName 321 * @param startRow 322 * @throws IOException 323 */
324 public static void scanTableByRow(String tableName, String startRow) throws IOException { 325 if (isExistTable(tableName)) { 326 Table table = conn.getTable(TableName.valueOf(tableName)); 327
328 Scan scan = new Scan(); 329 // scan.setStartRow(Bytes.toBytes(startRow));
330 scan.withStartRow(Bytes.toBytes(startRow)); 331 ResultScanner scanner = table.getScanner(scan); 332 // 遍历scanner
333 Iterator<Result> iterator = scanner.iterator(); 334 while (iterator.hasNext()) { 335 Result result = iterator.next(); 336 // 遍历result
337 CellScanner cellScanner = result.cellScanner(); 338 while (cellScanner.advance()) { 339 Cell cell = cellScanner.current(); 340 System.out.println("行键:" + Bytes.toString(CellUtil.copyRow(cell))); 341 System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell))); 342 System.out.println("列名:" + Bytes.toString(CellUtil.cloneQualifier(cell))); 343 System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell))); 344 } 345 System.out.println(); 346 } 347
348 table.close(); 349 } else { 350 System.out.println("表不存在"); 351 } 352
353 } 354
355 public static void main(String[] args) throws IOException { 356 // createTable("yeye","info","hobby"); 357 // dropTable("yeye"); 358 // addRow("yeye","1","info","name","tele"); 359 // deleteRow("yeye","1","info","name"); 360 // deleteMultiRow("yeye","1","info"); 361
362 // getRow("yeye","1","info","name"); 363 // getMultiRows("yeye","1","info"); 364
365 // scanTable("yeye"); 366
367 // scanTableByRow("yeye","2"); 368
369 // 创建多版本的表 370 // createTableMultiVersion("staff","3","info","sex");
371
372 /*
373 * addRow("staff","1","info","name","wyc"); 374 * addRow("staff","1","info","name","baba"); 375 * addRow("staff","1","info","name","yeye"); 376 */
377
378 // 查询多版本
379 getRowMultiVersion("staff", "1", "info", "name"); 380
381 } 382
383 }