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
| package com.thorine.test;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class TestDML {
private static Connection connection = null; private static Admin admin = null;
static { try { Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum", "bigdata1:2181,bigdata2:2181,bigdata3:2181"); connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } }
public static void close(){ if (admin != null) { try { admin.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } }
public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn),Bytes.toBytes(value));
table.put(put); table.close(); }
public static void getData(String tableName, String rowKey, String cf, String cn) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));
get.setMaxVersions(5);
Result result = table.get(get);
for (Cell cell : result.rawCells()) { System.out.print("ROW:" + Bytes.toString(CellUtil.cloneRow(cell))); System.out.print(", CN:" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.print(", VALUE:" + Bytes.toString(CellUtil.cloneValue(cell))); System.out.println(", CF:" + Bytes.toString(CellUtil.cloneFamily(cell))); }
table.close(); }
public static void scanTable(String tableName) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan(Bytes.toBytes("10010"),Bytes.toBytes("1003")); ResultScanner resultScanner = table.getScanner(scan); for (Result result : resultScanner) { for (Cell cell : result.rawCells()) { System.out.print("ROW:" + Bytes.toString(CellUtil.cloneRow(cell))); System.out.print(", CN:" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.print(", VALUE:" + Bytes.toString(CellUtil.cloneValue(cell))); System.out.println(", CF:" + Bytes.toString(CellUtil.cloneFamily(cell)));
} } table.close(); }
public static void deleteData(String tableName,String rowKey, String cf, String cn) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumns(Bytes.toBytes(cf),Bytes.toBytes(cn));
delete.addFamily(Bytes.toBytes(cf));
table.delete(delete); table.close(); }
public static void main(String[] args) throws IOException {
deleteData("stu","1002","",""); close(); } }
|