hbase运行jar包_关于hbase api的个人总结(带jar包)

1 packagecloudy.hbase.dao.imp;2

3 importjava.io.IOException;4

5 importorg.apache.hadoop.conf.Configuration;6 importorg.apache.hadoop.hbase.HBaseConfiguration;7 importorg.apache.hadoop.hbase.HColumnDescriptor;8 importorg.apache.hadoop.hbase.HTableDescriptor;9 importorg.apache.hadoop.hbase.KeyValue;10 importorg.apache.hadoop.hbase.client.Delete;11 importorg.apache.hadoop.hbase.client.Get;12 importorg.apache.hadoop.hbase.client.HBaseAdmin;13 importorg.apache.hadoop.hbase.client.HTable;14 importorg.apache.hadoop.hbase.client.HTablePool;15 importorg.apache.hadoop.hbase.client.Put;16 importorg.apache.hadoop.hbase.client.Result;17 importorg.apache.hadoop.hbase.client.ResultScanner;18 importorg.apache.hadoop.hbase.client.Scan;19 importorg.apache.hadoop.hbase.util.Bytes;20

21 public classhbase_demo {22 //声明静态配置

23 static Configuration conf = null;24

25 static{26 conf =HBaseConfiguration.create();27 conf.set("hbase.zookeeper.quorum", "min1:2181,min2:2181,min3:2181"); //声明 zk列表28 }29

30 /*

31 * 创建表32 *33 * @tableName 表名34 *35 * @family 列族列表36 */

37 public static void creatTable(String tableName, String[] family) throwsException {38 HBaseAdmin admin = newHBaseAdmin(conf);39 HTableDescriptor desc = newHTableDescriptor(tableName);40 for (int i = 0; i < family.length; i++) { //hbase创建表的时候必须带所依赖的列蔟41 desc.addFamily(newHColumnDescriptor(family[i]));42 }43 if(admin.tableExists(tableName)) {44 System.out.println("table Exists!");45 System.exit(0);46 } else{47 admin.createTable(desc);48 System.out.println("create table Success!");49 }50 System.out.println("=========================================================================================================");51 }52

53 /*

54 * 为表添加数据(适合知道有多少列族的固定表)55 *56 * @rowKey rowKey57 *58 * @tableName 表名59 *60 * @column1 第一个列族列表61 *62 * @value1 第一个列的值的列表63 *64 * @column2 第二个列族列表65 *66 * @value2 第二个列的值的列表67 */

68 public static void addData(String rowKey, String tableName, String[] column1, String[] value1, String[] column2, String[] value2) throwsIOException {69 Put put = new Put(Bytes.toBytes(rowKey));//设置rowkey

70 HTable table = new HTable(conf, Bytes.toBytes(tableName));//HTabel负责跟记录相关的操作如增删改查等//

71 //获取表

72 HColumnDescriptor[] columnFamilies = table.getTableDescriptor() //获取所有的列族

73 .getColumnFamilies();74

75 for (int i = 0; i < columnFamilies.length; i++) { // 通过遍历列族添加每个列族所需要的信息76 String familyName = columnFamilies[i].getNameAsString(); //获取列族名

77 if (familyName.equals("article")) { //article列族put数据

78 for (int j = 0; j < column1.length; j++) {79 put.add(Bytes.toBytes(familyName), Bytes.toBytes(column1[j]), Bytes.toBytes(value1[j])); //put是包含rowkey的实例化对象,rowkey1,rowkey2,rowkey380 }81 }82 if (familyName.equals("author")) { //author列族put数据

83 for (int j = 0; j < column2.length; j++) {84 put.add(Bytes.toBytes(familyName), Bytes.toBytes(column2[j]), Bytes.toBytes(value2[j]));85 }86 }87 }88 table.put(put); //table接受每个rowkey所在的put对象89 System.out.println("add data Success!");90 System.out.println("===========================================================================================================");91 }92

93 /*

94 * 根据rwokey查询rowkey下面的信息95 *96 * @rowKey rowKey97 *98 * @tableName 表名99 */

100 public static Result getResult(String tableName, String rowKey) throwsIOException {101 Get get = newGet(Bytes.toBytes(rowKey));102 HTable table = new HTable(conf, Bytes.toBytes(tableName));//获取表

103 Result result =table.get(get);104 for(KeyValue kv : result.list()) {105 System.out.println("family:" +Bytes.toString(kv.getFamily()));106 System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));107 System.out.println("value:" +Bytes.toString(kv.getValue()));108 System.out.println("Timestamp:" +kv.getTimestamp());109 System.out.println("----------Base on rowkey to search data--------");110 }111 returnresult;112 }113

114 /*

115 * 遍历查询hbase表116 *117 * @tableName 表名118 */

119 public static void getResultScann(String tableName) throwsIOException {120 Scan scan = newScan();121 ResultScanner rs = null;122 HTable table = newHTable(conf, Bytes.toBytes(tableName));123 try{124 rs =table.getScanner(scan);125 for(Result r : rs) {126 for(KeyValue kv : r.list()) {127 System.out.println("row:" +Bytes.toString(kv.getRow()));128 System.out.println("family:" +Bytes.toString(kv.getFamily()));129 System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));130 System.out.println("value:" +Bytes.toString(kv.getValue()));131 System.out.println("timestamp:" +kv.getTimestamp());132 System.out.println("-------------------------------------------");133 }134 }135 } finally{136 rs.close();137 System.out.println("==========================all the table (Base on tablename)===================");138 }139 }140

141 /*

142 * 遍历查询hbase表143 *144 * @tableName 表名145 */

146 public static void getResultScann(String tableName, String start_rowkey, String stop_rowkey) throwsIOException {147 Scan scan = newScan();148 scan.setStartRow(Bytes.toBytes(start_rowkey));149 scan.setStopRow(Bytes.toBytes(stop_rowkey));150 ResultScanner rs = null;151 HTable table = newHTable(conf, Bytes.toBytes(tableName));152 try{153 rs =table.getScanner(scan);154 for(Result r : rs) {155 for(KeyValue kv : r.list()) {156 System.out.println("row:" +Bytes.toString(kv.getRow()));157 System.out.println("family:" +Bytes.toString(kv.getFamily()));158 System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));159 System.out.println("value:" +Bytes.toString(kv.getValue()));160 System.out.println("timestamp:" +kv.getTimestamp());161 System.out.println("-------------------------------------------");162 }163 }164 } finally{165 rs.close();166 System.out.println("==================rowkey reduce to search data");167 }168 }169

170 /*

171 * 查询表中的某一列172 *173 * @tableName 表名174 *175 * @rowKey rowKey176 */

177 public static void getResultByColumn(String tableName, String rowKey, String familyName, String columnName) throwsIOException {178 HTable table = newHTable(conf, Bytes.toBytes(tableName));179 Get get = newGet(Bytes.toBytes(rowKey));180 get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); //获取指定列族和列修饰符对应的列

181 Result result =table.get(get);182 for(KeyValue kv : result.list()) {183 System.out.println("family:" +Bytes.toString(kv.getFamily()));184 System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));185 System.out.println("value:" +Bytes.toString(kv.getValue()));186 System.out.println("Timestamp:" +kv.getTimestamp());187 System.out.println("-------------------查询最小列中的数据-----------------------");188 }189 }190

191 /*

192 * 更新表中的某一列193 *194 * @tableName 表名195 *196 * @rowKey rowKey197 *198 * @familyName 列族名199 *200 * @columnName 列名201 *202 * @value 更新后的值203 */

204 public static void updateTable(String tableName, String rowKey, String familyName, String columnName, String value) throwsIOException {205 HTable table = newHTable(conf, Bytes.toBytes(tableName));206 Put put = newPut(Bytes.toBytes(rowKey));207 put.add(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes(value));//只能用于修改最小列中的values值,如果最小列名更改的话,会报错,显示没有找到208 table.put(put);209 System.out.println("update table Success! table and rowkey");210 }211

212 /*

213 * 查询某列数据的多个版本214 *215 * @tableName 表名216 *217 * @rowKey rowKey218 *219 * @familyName 列族名220 *221 * @columnName 列名222 */

223 public static void getResultByVersion(String tableName, String rowKey, String familyName, String columnName) throwsIOException {224 HTable table = newHTable(conf, Bytes.toBytes(tableName));225 Get get = newGet(Bytes.toBytes(rowKey));226 get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));227 get.setMaxVersions(5);228 Result result =table.get(get);229 for(KeyValue kv : result.list()) {230 System.out.println("family:" +Bytes.toString(kv.getFamily()));231 System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));232 System.out.println("value:" +Bytes.toString(kv.getValue()));233 System.out.println("Timestamp:" +kv.getTimestamp());234 System.out.println("-------------------------------------------");235 }236 /*

237 * List> results = table.get(get).list(); Iterator> it =238 * results.iterator(); while (it.hasNext()) {239 * System.out.println(it.next().toString()); }240 */

241 }242

243 /*

244 * 删除指定的列245 *246 * @tableName 表名247 *248 * @rowKey rowKey249 *250 * @familyName 列族名251 *252 * @columnName 列名253 */

254 public static void deleteColumn(String tableName, String rowKey, String falilyName, String columnName) throwsIOException {255 HTable table = newHTable(conf, Bytes.toBytes(tableName));256 Delete deleteColumn = newDelete(Bytes.toBytes(rowKey));257 deleteColumn.deleteColumns(Bytes.toBytes(falilyName), Bytes.toBytes(columnName));258 table.delete(deleteColumn);259 System.out.println(falilyName + ":" + columnName + "is deleted!");260 }261

262 /*

263 * 删除所有的列264 *265 * @tableName 表名266 *267 * @rowKey rowKey268 */

269 public static void deleteAllColumn(String tableName, String rowKey) throwsIOException {270 HTable table = newHTable(conf, Bytes.toBytes(tableName));271 Delete deleteAll = newDelete(Bytes.toBytes(rowKey));272 table.delete(deleteAll);273 System.out.println("all columns are deleted!base on rowkey");274 }275

276 /*

277 * 删除表278 *279 * @tableName 表名280 */

281 public static void deleteTable(String tableName) throwsIOException {282 HBaseAdmin admin = newHBaseAdmin(conf);283 admin.disableTable(tableName);284 admin.deleteTable(tableName);285 System.out.println(tableName + "is deleted!,disable+delelete");286 }287

288 public static void main(String[] args) throwsException {289

290 //创建表

291 String tableName = "test";292 String[] family = {"article", "author"};293 creatTable(tableName, family);294

295 //为表添加数据

296

297 String[] column1 = {"title", "content", "tag"};298 String[] value1 = {"Head First HBase", "HBase is the Hadoop database. Use it when you need random, realtime read/write access to your Big Data.", "Hadoop,HBase,NoSQL"};299 String[] column2 = {"name", "nickname"};300 String[] value2 = {"nicholas", "lee"};301

302 addData("rowkey1", "test", column1, value1, column2, value2);303 addData("rowkey2", "test", column1, value1, column2, value2);304 addData("rowkey3", "test", column1, value1, column2, value2);305

306 //遍历查询307 //getResultScann("test", "rowkey1", "rowkey5");308 //根据row key范围遍历查询309 //getResultScann("test", "rowkey4", "rowkey5");310

311 //查询312 //getResult("test", "rowkey1");313

314 //查询某一列的值315 //getResultByColumn("test", "rowkey1", "author", "name");316

317 //更新列318 //updateTable("test", "rowkey1", "author", "name", "bin");319

320 //查询某一列的值321 //getResultByColumn("test", "rowkey1", "author", "name");322

323 //查询某列的多版本324 //getResultByVersion("test", "rowkey1", "author", "name");325

326 //删除一列327 //deleteColumn("test", "rowkey1", "author", "nickname");328

329 //删除所有列330 //deleteAllColumn("test", "rowkey1");331

332 //删除表333 //deleteTable("test");

334

335 }336 }


版权声明:本文为weixin_42365604原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。