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
| package com.ghgj.hbase.test1610; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; public class HBaseAPIDemo1610 implements HBaseDemoInterface { private static final String ROWKEY = "p001"; private static final String ROWKEY2 = "p002"; private static final String FAMILY1 = "cf1"; private static final String FAMILY2 = "cf2"; private static final String KEY = "name"; private static final String VALUE = "huangbo"; private static final String TABLE_NAME = "person"; private static final String[] COLUMN_FAMILY = new String[] { FAMILY1, FAMILY2 }; static Configuration conf = null; static HBaseAdmin admin = null; static HTable table = null; static { try { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "hadoop03:2181,hadoop04:2181,hadoop05:2181"); admin = new HBaseAdmin(conf); table = new HTable(conf, TABLE_NAME); } catch (IOException e) { System.out.println("报错"); } } public static void main(String[] args) throws Exception { HBaseAPIDemo1610 hbase = new HBaseAPIDemo1610(); hbase.createTable(TABLE_NAME, COLUMN_FAMILY); HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(TABLE_NAME)); for (int i = 0; i < COLUMN_FAMILY.length; i++) { HColumnDescriptor cf1 = new HColumnDescriptor(COLUMN_FAMILY[i]); htd.addFamily(cf1); } hbase.createTable(TABLE_NAME, htd); hbase.descTable(TABLE_NAME); hbase.getAllTables(); hbase.modifyTable(TABLE_NAME); hbase.putData(TABLE_NAME, ROWKEY, FAMILY1, KEY, VALUE); String[] column1 = new String[] { "name1", "age", "province" }; String[] value1 = new String[] { "huangbo", "33", "xinjiang" }; String[] column2 = new String[] { "gender" }; String[] value2 = new String[] { "male" }; hbase.addData(TABLE_NAME, ROWKEY2, column1, value1, column2, value2); Result result = hbase.getResult(TABLE_NAME, ROWKEY2); System.out.println(result.toString()); List<KeyValue> list = result.list(); for (int i = 0; i < list.size(); i++) { KeyValue kv = list.get(i); printKeyValye(kv); } Result result1 = hbase.getResult(TABLE_NAME, ROWKEY2, FAMILY1, "province"); List<Cell> cells = result1.listCells(); for (int i = 0; i < cells.size(); i++) { Cell cell = cells.get(i); printCell(cell); } ResultScanner resultScann = hbase.getResultScann(TABLE_NAME); printResultScanner(resultScann);
Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes("user")); scan.setStopRow(Bytes.toBytes("zk002")); scan.setTimeRange(1488252774189l, 1488252774191l); ResultScanner resultScann1 = hbase.getResultScann(TABLE_NAME, scan); printResultScanner(resultScann1); Result resultByVersion = hbase.getResultByVersion(TABLE_NAME, ROWKEY, FAMILY1, "name", 3); printResult(resultByVersion); System.out.println("-------------------"); ResultScanner rs = hbase.getResultByVersion(ROWKEY, FAMILY1, "name", 3); printResultScanner(rs); hbase.dropTable(TABLE_NAME); } public static void printResultScanner(ResultScanner resultScann) { for (Result result : resultScann) { printResult(result); } } public static void printResult(Result result) { List<Cell> cells = result.listCells(); for (int i = 0; i < cells.size(); i++) { Cell cell = cells.get(i); printCell(cell); } } public static void printCell(Cell cell) { System.out.println(Bytes.toString(cell.getRow()) + "\t" + Bytes.toString(cell.getFamily()) + "\t" + Bytes.toString(cell.getQualifier()) + "\t" + Bytes.toString(cell.getValue()) + "\t" + cell.getTimestamp()); } public static void printKeyValye(KeyValue kv) { System.out.println(Bytes.toString(kv.getRow()) + "\t" + Bytes.toString(kv.getFamily()) + "\t" + Bytes.toString(kv.getQualifier()) + "\t" + Bytes.toString(kv.getValue()) + "\t" + kv.getTimestamp()); } @Override public void createTable(String tableName, String[] family) throws Exception { HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName)); for (int i = 0; i < family.length; i++) { HColumnDescriptor cf1 = new HColumnDescriptor(family[i]); htd.addFamily(cf1); } admin.createTable(htd); boolean tableExists = admin.tableExists(Bytes.toBytes(tableName)); System.out.println(tableExists ? "创建表成功" : "创建失败"); } @Override public void createTable(String tableName, HTableDescriptor htd) throws Exception { admin.createTable(htd); boolean tableExists = admin.tableExists(Bytes.toBytes(tableName)); System.out.println(tableExists ? "创建表成功" : "创建失败"); } @Override public void descTable(String tableName) throws Exception { HTableDescriptor tableDescriptor = table.getTableDescriptor(); HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies(); for (HColumnDescriptor hcd : columnFamilies) { System.out.println(Bytes.toString(hcd.getName())); } } @Override public void modifyTable(String tableName) throws Exception { HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName)); htd.addFamily(new HColumnDescriptor(Bytes.toBytes("cf3"))); htd.addFamily(new HColumnDescriptor(Bytes.toBytes("cf2"))); admin.modifyTable(tableName, htd); System.out.println("修改成功"); } @Override public void getAllTables() throws Exception { TableName[] listTableNames = admin.listTableNames(); for (TableName tn : listTableNames) { System.out.println(tn.toString()); } } @Override public void putData(String tableName, String rowKey, String familyName, String columnName, String value) throws Exception { Put put = new Put(Bytes.toBytes(rowKey)); put.add(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes(value)); table.put(put); System.out.println("插入成功"); }
@Override public void addData(String tableName, String rowKey, String[] column1, String[] value1, String[] column2, String[] value2) throws Exception { List<Put> puts = new ArrayList<Put>(); for (int i = 0; i < column1.length; i++) { Put put = new Put(Bytes.toBytes(rowKey)); put.add(Bytes.toBytes(FAMILY1), Bytes.toBytes(column1[i]), Bytes.toBytes(value1[i])); puts.add(put); } for (int i = 0; i < column2.length; i++) { Put put = new Put(Bytes.toBytes(rowKey)); put.add(Bytes.toBytes(FAMILY2), Bytes.toBytes(column2[i]), Bytes.toBytes(value2[i])); puts.add(put); } table.put(puts); System.out.println("插入一堆数据成功"); } @Override public Result getResult(String tableName, String rowKey) throws Exception { Get get = new Get(Bytes.toBytes(rowKey)); Result result = table.get(get); return result; } @Override public Result getResult(String tableName, String rowKey, String familyName, String columnName) throws Exception { Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); Result result = table.get(get); return result; } @Override public ResultScanner getResultScann(String tableName) throws Exception { Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); return scanner; } @Override public ResultScanner getResultScann(String tableName, Scan scan) throws Exception { return table.getScanner(scan); } @Override public Result getResultByColumn(String tableName, String rowKey, String familyName, String columnName) throws Exception { return null; } @Override public Result getResultByVersion(String tableName, String rowKey, String familyName, String columnName, int versions) throws Exception { Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); get.setMaxVersions(versions); Result result = table.get(get); return result; } public ResultScanner getResultByVersion(String rowKey, String familyName, String columnName, int versions) throws Exception { Scan scan = new Scan(Bytes.toBytes(rowKey), Bytes.toBytes(rowKey)); scan.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); scan.setMaxVersions(versions); ResultScanner scanner = table.getScanner(scan); return scanner; } @Override public void deleteColumn(String tableName, String rowKey, String falilyName, String columnName) throws Exception { } @Override public void deleteColumn(String tableName, String rowKey) throws Exception { } @Override public void disableTable(String tableName) throws Exception { admin.disableTable(tableName); } @Override public void dropTable(String tableName) throws Exception { try { admin.deleteTable(tableName); } catch (Exception e) { disableTable(tableName); admin.deleteTable(tableName); System.out.println("ssssssss"); } finally { boolean tableExists = admin.tableExists(Bytes.toBytes(tableName)); System.out.println(tableExists ? "删除失败" : "删除成功"); } } }
|