时间: 2020-09-17|57次围观|0 条评论

1.修改windows配置文件

C:\WINDOWS\system32\drivers\etc\hosts

将远程hbase和zookeeper主机的IP地址加进去

54.0.88.53      HADOOP1
54.0.88.54      HADOOP2

2.加入jar包

Hbase配置java客户端插图

 

Hbase配置java客户端插图1

Hbase配置java客户端插图2

 


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=falseWed 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的配置也可以保持一致。

 

   static {        config = HBaseConfiguration.create();        config                .addResource(new Path(                        "D://eclipse/myeclipse/workspace/Hive/hbaselib/hbase-site.xml"));    }

 

 记得这个hbase-site.xml和当前的hbase环境下的是一致的,要不然会报错:

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.贴代码

/** *  */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());// 插入一行数据,传入rowKeyput.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,// removeColumnHbaseadmin.modifyColumn(tableName, new HColumnDescriptor("cf1"));Hbaseadmin.enableTable(tableName);} catch (MasterNotRunningException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ZooKeeperConnectionException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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 stubString 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);}}

  

 

文章转载于:https://www.cnblogs.com/kxdblog/p/4034248.html

原著是一个有趣的人,若有侵权,请通知删除

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《Hbase配置java客户端
   

还没有人抢沙发呢~