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的配置也可以保持一致。
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客户端的更多相关文章
- HBase的java客户端测试(二)---DML操作
测试准备 [首先同步时间:] for node in CloudDeskTop master01 master02 slave01 slave02 slave03;do ssh $node " ...
- HBase的java客户端测试(一)---DDL操作
测试准备 [首先同步时间:] for node in CloudDeskTop master01 master02 slave01 slave02 slave03;do ssh $node " ...
- 大数据学习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 ...
- Java客户端访问HBase集群解决方案(优化)
测试环境:Idea+Windows10 准备工作: <1>.打开本地 C:\Windows\System32\drivers\etc(系统默认)下名为hosts的系统文件,如果提示当前用户 ...
- Hbase入门(五)——客户端(Java,Shell,Thrift,Rest,MR,WebUI)
Hbase的客户端有原生java客户端,Hbase Shell,Thrift,Rest,Mapreduce,WebUI等等. 下面是这几种客户端的常见用法. 一.原生Java客户端 原生java客户端 ...
- Jaeger的客户端采样配置(Java版)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- hbase总结~hbase配置和使用
Base配置和使用文档......................................................................................... ...
- Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结
转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...
- HBase的Java Api连接失败的问题及解决方法
分布式方式部署的HBase,启动正常,Shell操作正常,使用HBase的Java Api操作时总是连接失败,信息如下: This server is in the failed servers li ...
随机推荐
- C# Linq基本常用用法
1.什么是Linq? Lanaguage Interated Query(语言集成查询),Linq 是集成C# 和VB这些语言中用于提供数据查询能力的一个新特性. 这里只介绍两种基本常用用法. 学习方 ...
- Python笔记001-----简介及常用的库
1.Python是一种解释性语言,大部分代码要比编译型语言(如C++,java等)运行要慢点多.2.对于高并发,多线程的应用程序而言,Python并不是理想语言,python有全局解释器锁(Globa ...
- unicode文件处理(如果是ANSI编码就不需要了)
1.unicode文件的打开必须用rb模式. 3.wchar_t str[100] = { 0 }; 这个占200个字节. 2.宽字符对应的处理 fgetc fgetwc fputc fputwc f ...
- Mysql了解及安装
1.数据库由两部分来构成的 打开一个连接工具,用工具给MySQL发送命令,实际上是给数据库当中的服务下的命令,在服务当中解析命令,最终将命令转化成对物理库上文件IO的操作. 所以数据库的安装位置有两个 ...
- C#使用Redis
一,引入dll 1.ServiceStack.Common.dll 2.ServiceStack.Interfaces.dll 3.ServiceStack.Redis.dll 4.ServiceSt ...
- Jmeter之http性能测试实战 NON-GUI模式 进行分布式压力测试——干货(十二)
Apache JMeter Distributed Testing Step-by-step This short tutorial explains how to use multiple syst ...
- inotify-tools使用方法详解
inotify-tools 是为linux下inotify文件监控工具提供的一套c的开发接口库函数,同时还提供了一系列的命令行工具,这些工具可以用来监控文件系统的事件. inotify-tools是用 ...
- ContextLoaderListener加载过程
在web.xml中,配置ContextLoaderListener <!-- 配置Listener,用来创建Spring容器 --> <listener> <listen ...
- Golang Linux Shell编程(一)
1.调用系统命令 exec包执行外部命令,它将os.StartProcess进行包装使得它更容易映射到stdin和stdout,并且利用pipe连接i/o func Command(name stri ...
- CentOS下安装go语言编译环境
1.下载Go语言的安装包 这里给大家一个百度的分享连接http://pan.baidu.com/s/1qY3xPaG下载到CentOS的系统之中 $ tar -xzf go1.5.2.linux-xx ...