Hbase配置java客户端
1.修改windows配置文件
C:\WINDOWS\system32\drivers\etc\hosts
将远程hbase和zookeeper主机的IP地址加进去
54.0.88.53 HADOOP1
54.0.88.54 HADOOP2
2.加入jar包
3.加载配置文件
问题:网上很多都是自定义配置文件,根据hbase-site.xml里的参数如下配置:
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.zookeeper.quorum", "HADOOP1,HADOOP2,HADOOP3,HADOOP4,HADOOP5");
configuration.set("hbase.master", "HADOOP3:60000");
但是在运行时出现以下错误:
Exception in thread "main" org.apache.hadoop.hbase.client.RetriesExhaustedException: Failed after attempts=35, exceptions: Wed Sep 24 08:47:46 CST 2014, org.apache.hadoop.hbase.client.RpcRetryingCaller@191f801, java.io.IOException: Failed to find location, tableName=hbase:meta, row=hbasekang,,, reload=false Wed Sep 24 08:48:56 CST 2014, org.apache.hadoop.hbase.client.RpcRetryingCaller@191f801, java.io.IOException: Failed to find location, tableName=hbase:meta, row=hbasekang,,, reload=true
参考这里的方法,但是还没有很好的解决,我猜可能是configuration访问的不止这三个参数选项。
所以最好加载配置文件的方式,zookeeper的配置项和hbase的配置也可以保持一致。
1 2 3 4 5 6 | static { config = HBaseConfiguration.create(); config .addResource( new Path( "D://eclipse/myeclipse/workspace/Hive/hbaselib/hbase-site.xml" )); } |
记得这个hbase-site.xml和当前的hbase环境下的是一致的,要不然会报错:
1 | Caused by: java.io.IOException: Unable to determine ZooKeeper ensemble |
顺便普及一下:
Hbase的客户端API中,configuration相当于提供对配置参数的访问途径,任何操作都要先创建HBaseConfiguration实例。而配置文件在configuration中被当做一个个资源(Resource),也就是一组以XML格式存在的name/value对,以此来提供访问配置信息的接口,还提供了set/get方法用于读写。通过addResource方法加载xml配置文件,可以允许hadoop其他子项目和用户自定义的配置参数被加载使用。
4.贴代码
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 | /** * */ package com.util.hbase; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; 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.MasterNotRunningException; import org.apache.hadoop.hbase.ZooKeeperConnectionException; import org.apache.hadoop.hbase.client.Delete; 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.HTablePool; 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.filter.Filter; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; import com.ccb.entity.Detail3; /** * @author kangxuedan * */ public class HbaseUtil { public static Configuration config; static { config = HBaseConfiguration.create(); config .addResource( new Path( "D://eclipse/myeclipse/workspace/Hive/hbaselib/hbase-site.xml" )); } /** * 创建表 * * @throws IOException * @param tableName * 表名 * @param columns * 列族 */ public static void createTable(String tableName, String[] columns) throws IOException { HBaseAdmin Hbaseadmin = new HBaseAdmin(config); if (Hbaseadmin.tableExists(tableName)) { System.out.println( "表已经存在!" ); } else { HTableDescriptor desc = new HTableDescriptor(tableName); for (String column : columns) { desc.addFamily( new HColumnDescriptor(column)); } Hbaseadmin.createTable(desc); System.out.println( "表创建成功!" ); } } /** * 插入数据 */ // insert data // String newRowKey = "ffvs";String[] familys ={"cf1"};String[] values // ={"fdxs"}; // insert(tableName,newRowKey,familys,values); public static void insert(String tableName, String newRowKey, String[] familys, String[] values) { System.out.println( "************start insert ************" ); HTablePool pool = new HTablePool(config, 1000 ); Put put = new Put(newRowKey.getBytes()); // 插入一行数据,传入rowKey put.add(familys[ 0 ].getBytes(), null , values[ 0 ].getBytes()); // 本行数据的第三列 try { pool.getTable(tableName).put(put); } catch (IOException e) { e.printStackTrace(); } System.out.println( "************end insert************" ); } /** * 根据 rowkey删除一条记录 * * @param tablename * @param rowkey */ public static void deleteRow(String tablename, String rowkey) { try { HTable table = new HTable(config, tablename); List list = new ArrayList(); Delete d1 = new Delete(rowkey.getBytes()); list.add(d1); table.delete(list); System.out.println( "删除行成功!" ); } catch (IOException e) { e.printStackTrace(); } } /** * 删除表 * * @param tableName */ public static void dropTable(String tableName) { try { HBaseAdmin Hbaseadmin = new HBaseAdmin(config); Hbaseadmin.disableTable(tableName); Hbaseadmin.deleteTable(tableName); } catch (MasterNotRunningException e) { e.printStackTrace(); } catch (ZooKeeperConnectionException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 修改表信息 * * @param tableName */ public static void modifyTable(String tableName) { HBaseAdmin Hbaseadmin; try { Hbaseadmin = new HBaseAdmin(config); Hbaseadmin.disableTable(tableName); // modifying existing ColumnFamily addColumn, modifyColumn, // removeColumn Hbaseadmin.modifyColumn(tableName, new HColumnDescriptor( "cf1" )); Hbaseadmin.enableTable(tableName); } catch (MasterNotRunningException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ZooKeeperConnectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 查询表,返回所有记录 * * @param tableName */ public static void QueryAll(String tableName) { HTablePool pool = new HTablePool(config, 1000 ); try { ResultScanner rs = pool.getTable(tableName).getScanner( new Scan()); for (Result r : rs) { System.out.println( "rowkey: " + new String(r.getRow())); for (KeyValue keyValue : r.raw()) { System.out.println( "列:" + new String(keyValue.getFamily()) + "====值:" + new String(keyValue.getValue())); } } } catch (IOException e) { e.printStackTrace(); } } /** * 单条件查询,根据rowkey查询唯一一条记录 * * @param tableName * @return */ public static List<Detail3> QuerySingle(String tableName, String rowKey) { HTablePool pool = new HTablePool(config, 1000 ); String results = "" ; Detail3 detail3 = new Detail3(); List<Detail3> resultList = new ArrayList<Detail3>(); try { Get scan = new Get(rowKey.getBytes()); // 根据rowkey查询 Result r = pool.getTable(tableName).get(scan); System.out.println( "获得到rowkey:" + new String(r.getRow())); for (KeyValue keyValue : r.raw()) { String result = new String(keyValue.getValue(), "utf-8" ); System.out.println( "列:" + new String(keyValue.getFamily()) + "====值:" + new String(keyValue.getValue(), "utf-8" )); StringTokenizer st = new StringTokenizer(results, "," ); String[] temp = result.split( "\\|" ); detail3.setCust_no(temp[ 0 ]); detail3.setSa_tx_dt(temp[ 1 ]); detail3.setTx_log_no(temp[ 2 ]); detail3.setSa_tx_tm(temp[ 3 ]); detail3.setTemp(temp[ 4 ]); detail3.setCust_acct_no(temp[ 5 ]); detail3.setSa_tx_crd_no(temp[ 6 ]); detail3.setCr_tx_amt(temp[ 7 ]); detail3.setAcct_bal(temp[ 8 ]); detail3.setF_fare(temp[ 9 ]); detail3.setDr_cr_cod(temp[ 10 ]); detail3.setTran_cd(temp[ 11 ]); detail3.setTx_type(temp[ 12 ]); detail3.setXt_op_trl(temp[ 13 ]); detail3.setXt_op_trl2(temp[ 14 ]); detail3.setBus_inst_no(temp[ 15 ]); detail3.setCanal(temp[ 16 ]); detail3.setSa_op_acct_no_32(temp[ 17 ]); detail3.setSa_op_cust_name(temp[ 18 ]); detail3.setSa_op_bank_no(temp[ 19 ]); detail3.setCr_cust_docag_stno(temp[ 20 ]); detail3.setSa_otx_flg(temp[ 21 ]); detail3.setSa_rmrk(temp[ 22 ]); detail3.setOther(temp[ 23 ]); detail3.setTlr_no(temp[ 24 ]); resultList.add(detail3); // resultList = java.util.Arrays.asList(temp); } } catch (IOException e) { e.printStackTrace(); } return resultList; } /** * rowkey 范围查询 * * @param tableName * @return */ public static List<Detail3> QueryRange(String tableName, String cust_no,String starttime,String endtime) { String startRow = cust_no + starttime; String endRow = cust_no + endtime; String results = "" ; Detail3 detail3 = new Detail3(); List<Detail3> resultList = new ArrayList<Detail3>(); HTablePool pool = new HTablePool(config, 1000 ); try { Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(startRow)); scan.setStopRow(Bytes.toBytes(endRow)); scan.setCacheBlocks( true ); scan.setCaching( 30000 ); ResultScanner rs = pool.getTable(tableName).getScanner(scan); for (Result r : rs) { for (KeyValue kv : r.raw()) { System.out.println(String.format( "key:%s" , Bytes.toString(kv .getRow()))); System.out.println(String.format( "value:%s" , Bytes.toString(kv .getValue()))); String result = Bytes.toString(kv.getValue()); String[] temp = result.split( "\\|" ); detail3.setCust_no(temp[ 0 ]); detail3.setSa_tx_dt(temp[ 1 ]); detail3.setTx_log_no(temp[ 2 ]); detail3.setSa_tx_tm(temp[ 3 ]); detail3.setTemp(temp[ 4 ]); detail3.setCust_acct_no(temp[ 5 ]); detail3.setSa_tx_crd_no(temp[ 6 ]); detail3.setCr_tx_amt(temp[ 7 ]); detail3.setAcct_bal(temp[ 8 ]); detail3.setF_fare(temp[ 9 ]); detail3.setDr_cr_cod(temp[ 10 ]); detail3.setTran_cd(temp[ 11 ]); detail3.setTx_type(temp[ 12 ]); detail3.setXt_op_trl(temp[ 13 ]); detail3.setXt_op_trl2(temp[ 14 ]); detail3.setBus_inst_no(temp[ 15 ]); detail3.setCanal(temp[ 16 ]); detail3.setSa_op_acct_no_32(temp[ 17 ]); detail3.setSa_op_cust_name(temp[ 18 ]); detail3.setSa_op_bank_no(temp[ 19 ]); detail3.setCr_cust_docag_stno(temp[ 20 ]); detail3.setSa_otx_flg(temp[ 21 ]); detail3.setSa_rmrk(temp[ 22 ]); detail3.setOther(temp[ 23 ]); detail3.setTlr_no(temp[ 24 ]); resultList.add(detail3); } } } catch (IOException e) { e.printStackTrace(); } return resultList; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String tableName = "detail3" ; String rowKey = "442000801K750005487" ; String cust_no = "A432502" ;String starttime = "2014-06-01" ; String endtime = "2014-08-01" ; QueryRange(tableName, cust_no,starttime,endtime);} } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步