「java连接hbase」java连接hbase数据库
今天给各位分享java连接hbase的知识,其中也会对java连接hbase数据库进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java调用hbase api连接出错:
- 2、如何使用Java API操作Hbase
- 3、java连接hbase,需要实现多表关联查询,比如a,b两表其中可以通过字段进行关联,请问如何join查询两个表
- 4、hbase单机模式下,使用java API远程连接hbase的问题。
- 5、java怎样给hbase插入数据
- 6、hbase连接java时,zookeeper总是连接不上,但是确实已经启动,每次连接,都报错
java调用hbase api连接出错:
别的暂时先不说,最起码zookeeper这个单词得输入对啊。
具体的配置去hbase的安装目录下找,把xml里的所有东西都set进来。
如何使用Java API操作Hbase
代码如下:
package com.dhgate.hbase.test;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
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.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.PageFilter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;
/**
* 基于新的API
* Hbase0.96版本
* 写的工具类
*
* @author qindongliang
* 大数据技术交流群: 376932160
*
* **/
public class HbaseCommons {
static Configuration conf=HBaseConfiguration.create();
static String tableName="";
public static void main(String[] args)throws Exception {
//String tableName="test";
//createTable(tableName, null);
}
/**
* 批量添加数据
* @param tableName 标名字
* @param rows rowkey行健的集合
* 本方法仅作示例,其他的内容需要看自己义务改变
*
* **/
public static void insertList(String tableName,String rows[])throws Exception{
HTable table=new HTable(conf, tableName);
ListPut list=new ArrayListPut();
for(String r:rows){
Put p=new Put(Bytes.toBytes(r));
//此处示例添加其他信息
//p.add(Bytes.toBytes("family"),Bytes.toBytes("column"), 1000, Bytes.toBytes("value"));
list.add(p);
}
table.put(list);//批量添加
table.close();//释放资源
}
/**
* 创建一个表
* @param tableName 表名字
* @param columnFamilys 列簇
*
* **/
public static void createTable(String tableName,String[] columnFamilys)throws Exception{
//admin 对象
HBaseAdmin admin=new HBaseAdmin(conf);
if(admin.tableExists(tableName)){
System.out.println("此表,已存在!");
}else{
//旧的写法
//HTableDescriptor tableDesc=new HTableDescriptor(tableName);
//新的api
HTableDescriptor tableDesc=new HTableDescriptor(TableName.valueOf(tableName));
for(String columnFamily:columnFamilys){
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
}
admin.createTable(tableDesc);
System.out.println("建表成功!");
}
admin.close();//关闭释放资源
}
/**
* 删除一个表
* @param tableName 删除的表名
* */
public static void deleteTable(String tableName)throws Exception{
HBaseAdmin admin=new HBaseAdmin(conf);
if(admin.tableExists(tableName)){
admin.disableTable(tableName);//禁用表
admin.deleteTable(tableName);//删除表
System.out.println("删除表成功!");
}else{
System.out.println("删除的表不存在!");
}
admin.close();
}
/**
* 插入一条数据
* @param tableName 表明
* @param columnFamily 列簇
* @param column 列
* @param value 值
*
* ***/
public static void insertOneRow(String tableName,String rowkey,String columnFamily,String column,String value)throws Exception{
HTable table=new HTable(conf, tableName);
Put put=new Put(Bytes.toBytes(rowkey));
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);//放入表
table.close();//释放资源
}
/**
* 删除一条数据
* @param tableName 表名
* @param row rowkey行键
*
* */
public static void deleteOneRow(String tableName,String row)throws Exception{
HTable table=new HTable(conf, tableName);
Delete delete=new Delete(Bytes.toBytes(row));
table.delete(delete);
table.close();
}
/**
* 删除多条数据
* @param tableName 表名
* @param rows 行健集合
*
* **/
public static void deleteList(String tableName,String rows[])throws Exception{
HTable table=new HTable(conf, tableName);
ListDelete list=new ArrayListDelete();
for(String row:rows){
Delete del=new Delete(Bytes.toBytes(row));
list.add(del);
}
table.delete(list);
table.close();//释放资源
}
/**
* 获取一条数据,根据rowkey
* @param tableName 表名
* @param row 行健
*
* **/
public static void getOneRow(String tableName,String row)throws Exception{
HTable table=new HTable(conf, tableName);
Get get=new Get(Bytes.toBytes(row));
Result result=table.get(get);
printRecoder(result);//打印记录
table.close();//释放资源
}
/**
* 查看某个表下的所有数据
*
* @param tableName 表名
* */
public static void showAll(String tableName)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
printRecoder(r);//打印记录
}
table.close();//释放资源
}
/**
* 查看某个表下的所有数据
*
* @param tableName 表名
* @param rowKey 行健
* */
public static void ScanPrefixByRowKey(String tableName,String rowKey)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
scan.setFilter(new PrefixFilter(Bytes.toBytes(rowKey)));
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
printRecoder(r);//打印记录
}
table.close();//释放资源
}
/**
* 查看某个表下的所有数据
*
* @param tableName 表名
* @param rowKey 行健扫描
* @param limit 限制返回数据量
* */
public static void ScanPrefixByRowKeyAndLimit(String tableName,String rowKey,long limit)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
scan.setFilter(new PrefixFilter(Bytes.toBytes(rowKey)));
scan.setFilter(new PageFilter(limit));
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
printRecoder(r);//打印记录
}
table.close();//释放资源
}
/**
* 根据rowkey扫描一段范围
* @param tableName 表名
* @param startRow 开始的行健
* @param stopRow 结束的行健
* **/
public void scanByStartAndStopRow(String tableName,String startRow,String stopRow)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
scan.setStartRow(Bytes.toBytes(startRow));
scan.setStopRow(Bytes.toBytes(stopRow));
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
printRecoder(r);
}
table.close();//释放资源
}
/**
* 扫描整个表里面具体的某个字段的值
* @param tableName 表名
* @param columnFalimy 列簇
* @param column 列
* **/
public static void getValueDetail(String tableName,String columnFalimy,String column)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
System.out.println("值: " +new String(r.getValue(Bytes.toBytes(columnFalimy), Bytes.toBytes(column))));
}
table.close();//释放资源
}
/**
* 打印一条记录的详情
*
* */
public static void printRecoder(Result result)throws Exception{
for(Cell cell:result.rawCells()){
System.out.print("行健: "+new String(CellUtil.cloneRow(cell)));
System.out.print("列簇: "+new String(CellUtil.cloneFamily(cell)));
System.out.print(" 列: "+new String(CellUtil.cloneQualifier(cell)));
System.out.print(" 值: "+new String(CellUtil.cloneValue(cell)));
System.out.println("时间戳: "+cell.getTimestamp());
}
}
}
java连接hbase,需要实现多表关联查询,比如a,b两表其中可以通过字段进行关联,请问如何join查询两个表
表 A(ID,NAME,AGE),表B(ID,AID,FILED)
SELECT * FROM A
JOIN B ON A.ID=B.AID
(其中表A里ID字段与表B里AID字段有关联)
hbase单机模式下,使用java API远程连接hbase的问题。
首先你应该看Master进程是否已经成功启动,检查下master的60010监控界面。这日志报的是连接拒绝 ,或者关闭防火墙
极有可能是你PC机网络无法连接到虚拟机里边,你可以从本机telnet下虚拟机上master的端口,看下能连上不
java怎样给hbase插入数据
hbase 是动态列的,直接加就可以了,不用事先定义的啊
例如:(代码没有调试过,具体可看hbase的例子)
Table table = connection.getTable(TableName.valueOf(表名));
Put put = new Put(Bytes.toBytes(主键字符串));
put.addColumn(FieldFamily, Bytes.toBytes(字段名1), Bytes.toBytes(插入的值1));
put.addColumn(FieldFamily, Bytes.toBytes(字段名2), Bytes.toBytes(插入的值2));
.........
table.put(put);
hbase连接java时,zookeeper总是连接不上,但是确实已经启动,每次连接,都报错
HBase的安装包里面有自带zookeeper的。很多系统部署也是直接启动上面的zookeeper。 本来也是没有问题的,想想吧,系统里也只有hbase在用zookeeper。先启动zookeeper,再将hbase起来就好了 ? 但是今天遇到了一个很蛋疼的问题。和同事争论了很久。 ? 因为我们是好多hbase集群共用一个zookeeper的,其中一个集群需要从hbase 0.90.二 升级到hbase 0.9二上,自然,包也要更新。但是其中一台regionserver上面同时也有跑zookeeper,而zookeeper还是用hbase 0.90.二 自带的zookeeper在跑。 ? 现在好了,升级一个regionserver,连着zookeeper也要受到牵连,看来必须要重启,不然,jar包替换掉,可能会影响到zk正在跑的经常。但是重启zk毕竟对正在连接这个zk的client端会有短暂的影响
java连接hbase的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java连接hbase数据库、java连接hbase的信息别忘了在本站进行查找喔。
发布于:2022-11-27,除非注明,否则均为
原创文章,转载请注明出处。