溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java 連接hbase

發(fā)布時間:2020-07-15 21:18:24 來源:網(wǎng)絡(luò) 閱讀:1899 作者:水滴的歷程 欄目:軟件技術(shù)

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", "服務(wù)器地址");
    configuration.set("hbase.zookeeper.property.clientPort", "服務(wù)器端口");// 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,將其轉(zhuǎn)義成byte字節(jié)
    Result r=table.get(scan);//使用轉(zhuǎn)義的rowkey查詢表,將其查詢結(jié)果返回給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");
}
}

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI