| | 1 | {{{ |
| | 2 | #!java |
| | 3 | import java.io.IOException; |
| | 4 | |
| | 5 | import org.apache.hadoop.hbase.HBaseConfiguration; |
| | 6 | import org.apache.hadoop.hbase.HColumnDescriptor; |
| | 7 | import org.apache.hadoop.hbase.HTableDescriptor; |
| | 8 | import org.apache.hadoop.hbase.client.HBaseAdmin; |
| | 9 | import org.apache.hadoop.hbase.client.HTable; |
| | 10 | import org.apache.hadoop.hbase.client.Scanner; |
| | 11 | import org.apache.hadoop.hbase.io.BatchUpdate; |
| | 12 | import org.apache.hadoop.hbase.io.Cell; |
| | 13 | import org.apache.hadoop.hbase.io.RowResult; |
| | 14 | import org.apache.hadoop.hbase.util.Bytes; |
| | 15 | |
| | 16 | public class hbase { |
| | 17 | |
| | 18 | public static void main(String args[]) throws IOException { |
| | 19 | |
| | 20 | String table_name = "waue"; |
| | 21 | String colomn_family = "family:"; |
| | 22 | String column_quolify= "qf"; |
| | 23 | String hbase_row = "w-row"; |
| | 24 | String value = "0911311311"; |
| | 25 | |
| | 26 | HBaseConfiguration config = new HBaseConfiguration(); |
| | 27 | |
| | 28 | |
| | 29 | HBaseAdmin admin = new HBaseAdmin(config); |
| | 30 | if (!admin.tableExists(table_name)) { |
| | 31 | System.out.println("HTable : " + table_name |
| | 32 | + " creating ... please wait"); |
| | 33 | HTableDescriptor tableDesc = new HTableDescriptor(table_name); |
| | 34 | // add column family |
| | 35 | tableDesc.addFamily(new HColumnDescriptor(colomn_family)); |
| | 36 | admin.createTable(tableDesc); |
| | 37 | } |
| | 38 | |
| | 39 | HTable table = new HTable(config, table_name); |
| | 40 | |
| | 41 | |
| | 42 | BatchUpdate batchUpdate = new BatchUpdate(hbase_row); |
| | 43 | |
| | 44 | |
| | 45 | batchUpdate.put(colomn_family+column_quolify, Bytes |
| | 46 | .toBytes(value)); |
| | 47 | |
| | 48 | |
| | 49 | batchUpdate.delete(colomn_family+"cellIWantDeleted"); |
| | 50 | |
| | 51 | |
| | 52 | table.commit(batchUpdate); |
| | 53 | |
| | 54 | |
| | 55 | Cell cell = table.get(hbase_row, colomn_family+column_quolify); |
| | 56 | System.out.print(cell.getValue()); |
| | 57 | |
| | 58 | |
| | 59 | Scanner scanner = |
| | 60 | table.getScanner(new String[] { colomn_family+column_quolify }); |
| | 61 | |
| | 62 | RowResult rowResult = scanner.next(); |
| | 63 | |
| | 64 | while (rowResult != null) { |
| | 65 | |
| | 66 | System.out.println("Found row: " |
| | 67 | + Bytes.toString(rowResult.getRow()) |
| | 68 | + " with value: " |
| | 69 | + rowResult.get(Bytes |
| | 70 | .toBytes(colomn_family+column_quolify))); |
| | 71 | rowResult = scanner.next(); |
| | 72 | } |
| | 73 | |
| | 74 | for (RowResult result : scanner) { |
| | 75 | System.out.println("Found row: " |
| | 76 | + Bytes.toString(rowResult.getRow()) |
| | 77 | + " with value: " |
| | 78 | + rowResult.get(Bytes |
| | 79 | .toBytes(colomn_family+column_quolify))); |
| | 80 | } |
| | 81 | |
| | 82 | scanner.close(); |
| | 83 | } |
| | 84 | } |
| | 85 | |
| | 86 | |
| | 87 | }}} |