请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

能连接hbase打印出表名,但put时卡住

通过查看zookeeper,有hbase表的数据,在本地可以连上虚拟机中的hbase,能够获取表信息,但是在往hbase表中执行put时,卡住。hadoop版本2.7.6,hbase版本1.4.9。最终出现org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException

package com.tang.spark.utils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;

public class HbaseUtils {

private Connection conn = null;
private Configuration configuration = null;
private static HbaseUtils hbaseUtils = null;
private HbaseUtils() throws IOException {
    configuration = HBaseConfiguration.create();
    configuration.set("hbase.zookeeper.quorum","master:2181");
    configuration.set("habse.rootdir","hdfs://master:9000/hbase");
    conn = ConnectionFactory.createConnection(configuration);

// admin = new HBaseAdmin(configuration);
}

/**
 * 获取HbaseUtils实例
 * @return
 */
public static synchronized HbaseUtils getInstance() throws IOException {
    if (null == hbaseUtils){
        return new HbaseUtils();
    }
    return hbaseUtils;
}

/**
 *
 * @param tableName
 * @return
 */
public Table getTable(String tableName){
    Table hTable = null;
    try {
        hTable = conn.getTable(TableName.valueOf(tableName));
    }catch (Exception e){
        e.printStackTrace();
    }
    return hTable;
}

/**
 * 网hbase表中插入数据
 * @param tableName
 * @param rowKey
 * @param cf
 * @param colum
 * @param value
 */
public void put(String tableName,String rowKey,String cf,String colum,String value) throws IOException {

    Table table = getTable(tableName);
    Put put = new Put(rowKey.getBytes());
    put.addColumn(cf.getBytes(),colum.getBytes(),value.getBytes());
    table.put(put);
}

public void get(){

}

public static void main(String[] args) throws IOException {
    String tableName = "imooc_course_clickcount";
    String rowKey = "20190408_134";
    String cf = "info";
    String colum = "click_count";
    String value = "34";
    HbaseUtils instance = HbaseUtils.getInstance();
    System.out.println(instance.getTable(tableName).getName().getNameAsString());
    instance.put(tableName,rowKey,cf,colum,value);
    System.out.println();
    instance.conn.close();
}

}

正在回答 回答被采纳积分+3

2回答

提问者 qq_多少幅度_0 2019-04-08 20:05:49

在hbase的shell窗口可以执行put操作

0 回复 有任何疑惑可以回复我~
Michael_PK 2019-04-08 20:01:53

你HBase shell操作put正常不

0 回复 有任何疑惑可以回复我~
  • 提问者 qq_多少幅度_0 #1
    在hbase的shell窗口可以执行put操作
    回复 有任何疑惑可以回复我~ 2019-04-08 20:06:19
  • Michael_PK 回复 提问者 qq_多少幅度_0 #2
    那只有可能是hostname是否有问题,你代码里改成IP试试看行不行
    回复 有任何疑惑可以回复我~ 2019-04-08 20:25:16
  • 提问者 qq_多少幅度_0 回复 Michael_PK #3
    改了也不行,从新装了zookeeper也不行
    回复 有任何疑惑可以回复我~ 2019-04-08 20:43:06
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号