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

   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());// 插入一行数据,传入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);} }

  

Hbase配置java客户端的更多相关文章

  1. HBase的java客户端测试(二)---DML操作

    测试准备 [首先同步时间:] for node in CloudDeskTop master01 master02 slave01 slave02 slave03;do ssh $node " ...

  2. HBase的java客户端测试(一)---DDL操作

    测试准备 [首先同步时间:] for node in CloudDeskTop master01 master02 slave01 slave02 slave03;do ssh $node " ...

  3. 大数据学习day11------hbase_day01----1. zk的监控机制,2动态感知服务上下线案例 3.HDFS-HA的高可用基本的工作原理 4. HDFS-HA的配置详解 5. HBASE(简介,安装,shell客户端,java客户端)

    1. ZK的监控机制 1.1 监听数据的变化  (1)监听一次 public class ChangeDataWacher { public static void main(String[] arg ...

  4. Java客户端访问HBase集群解决方案(优化)

    测试环境:Idea+Windows10 准备工作: <1>.打开本地 C:\Windows\System32\drivers\etc(系统默认)下名为hosts的系统文件,如果提示当前用户 ...

  5. Hbase入门(五)——客户端(Java,Shell,Thrift,Rest,MR,WebUI)

    Hbase的客户端有原生java客户端,Hbase Shell,Thrift,Rest,Mapreduce,WebUI等等. 下面是这几种客户端的常见用法. 一.原生Java客户端 原生java客户端 ...

  6. Jaeger的客户端采样配置(Java版)

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  7. hbase总结~hbase配置和使用

    Base配置和使用文档......................................................................................... ...

  8. Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结

    转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...

  9. HBase的Java Api连接失败的问题及解决方法

    分布式方式部署的HBase,启动正常,Shell操作正常,使用HBase的Java Api操作时总是连接失败,信息如下: This server is in the failed servers li ...

随机推荐

  1. 《struts2》:指定多个配置文件和默认Action

    转载:http://m.blog.csdn.net/article/details?id=51212968

  2. 【转】nagios 命令解释

    nagios 命令解释 check_ssh                 界面拼装参数格式如下共3个元素:                         命令!端口!连接超时时间          ...

  3. java之Servlet监听器Listener

    常用应用场景:单点登录.统计在线人数 一.简介 (一)概述 1.Listener 用于监听 java web程序中的事件,例如创建.修改.删除Session.request.context等,并触发响 ...

  4. selenium模拟浏览器对搜狗微信文章进行爬取

    在上一篇博客中使用redis所维护的代理池抓取微信文章,开始运行良好,之后运行时总是会报501错误,我用浏览器打开网页又能正常打开,调试了好多次都还是会出错,既然这种方法出错,那就用selenium模 ...

  5. windows下安装配置python + selenium 来驱动firefox

    第一步,首先下载安装python ,我下载的是3.5版本,这个版本,自带了pip工具,不需要安装pip了 :) 链接地址:python 3.5 第二步,执行pip install selenium 安 ...

  6. 终于放弃Windows下丑陋的cmd

    微软万年不变的cmd命令行工具,简直其丑无比,交互性极差.但是作为开发者有时离不开.最近用了几款开源替代方案.发现几款不错的cmd替代者.例如Cmder.babun.ConsoleZ.win-bash ...

  7. SpringMVC源码情操陶冶-DispatcherServlet父类简析

    阅读源码有助于陶冶情操,本文对springmvc作个简单的向导 springmvc-web.xml配置 <servlet> <servlet-name>dispatch< ...

  8. HDU1013,1163 ,2035九余数定理 快速幂取模

    1.HDU1013求一个positive integer的digital root,即不停的求数位和,直到数位和为一位数即为数根. 一开始,以为integer嘛,指整型就行吧= =(too young ...

  9. redis新手入门,摸不着头脑可以看看<二>

    对<Redis开发与运维>的理解--下文中引号部分来自该书,略有修改 P19.  Redis有序集合(图2-1) "Redis有序集合和集合一样也是某种类型元素的集合,不重复.不 ...

  10. Mac通过brew安装reds、memcached

    redis brew install php70-redis 配置文件: /usr/local/etc/php/7.0/conf.d/ext-redis.ini memcached brew inst ...