java 连接hbase_java 连接hbase

package com.weshare.bigdata;

import org.apache.hadoop.hbase.Cell;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.client.*;

import org.apache.hadoop.hbase.util.Bytes;

public class hbaseLink {

static org.apache.hadoop.conf.Configuration configuration=null;

private org.apache.hadoop.hbase.client.Connection connection=null;

private Table table=null;

public void init() throws Exception{

configuration=HBaseConfiguration.create();

configuration.set("hbase.zookeeper.quorum", "服务器地址");

configuration.set("hbase.zookeeper.property.clientPort", "服务器端口");// zookeeper端口

connection=ConnectionFactory.createConnection(configuration);

// try {

// table = connection.getTable(TableName.valueOf("hbase表名"));

// ResultScanner rs = table.getScanner(new Scan());

// int num=0;

// for (Result r : rs) {

// Byte b= r.getRow(scan);

// System.out.println("获得到rowkey:" + new String(r.getRow()));

// num++;

//

// }System.out.print(num);

// }catch(Exception e){

// e.printStackTrace();

// }

}

public void queryall(String tableName) throws Exception{

table = connection.getTable(TableName.valueOf(tableName));

ResultScanner rs = table.getScanner(new Scan());

int num=0;

for (Result r : rs) {

//System.out.println("获得到rowkey:" + new String(r.getRow()));

num++;

}

System.out.print(num);

}

public void checkColName(String tableName,String rowkey,String exceptedColName) throws Exception{

table = connection.getTable(TableName.valueOf(tableName));//建立hbase表链接

Get scan = new Get(rowkey.getBytes());//获取rokey,将其转义成byte字节

Result r=table.get(scan);//使用转义的rowkey查询表,将其查询结果返回给Result

System.out.println("1");

if(r.rawCells().equals("")||r.rawCells()==null||r.rawCells().length==0){

System.out.println("3");

}

for (Cell keyValue : r.rawCells()) {

System.out.println("2");

String colName = Bytes.toString(keyValue.getQualifierArray(),keyValue.getQualifierOffset(),keyValue.getQualifierLength());

if(!colName.equals(exceptedColName)){

System.out.println("校验成功,不存在colName:"+exceptedColName+",实际colName:" + colName);

}else if(colName.isEmpty()){

System.out.println("校验成功,查询无colName");

}else{

System.out.println("校验失败");

}

System.out.println("12:"+keyValue.getFamilyLength());

}

// Get coln=scan.addFamily(rowkey.getBytes());

// NavigableMap nm=scan.getFamilyMap(rowkey.getBytes());

// System.out.println(coln.toString());

}

public static void main(String[] args) throws Exception{

hbaseLink hl=new hbaseLink();

hl.init();

hl.queryall("tablename");

// hl.checkColName("table","rowkey","column");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值