zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

使用THttpClient连接hbase异常

2023-06-13 09:11:26 时间

# 背景

- 客户在java代码中通过hbasethrift连接hbase异常

# 连接代码

- 一般使用TSocket,这里客户使用的是THttpClient,请求url使用的是http://

- 通过do_as设置了用户

    public HbaseClient(String host, int port, String doAsUser, int timeout, boolean useTransport) {
        this.host = host;
        this.port = port;
        this.doAsUser = doAsUser;
        this.timeout = timeout;
        this.useTransport = useTransport;
        String url = "http://" + this.host + ":" + this.port;
        // TSocket
        if (BooleanUtils.isTrue(this.useTransport)) {
            transport = new TSocket(this.host, this.port,timeout,timeout);
        }

        try {
            httpClient = new THttpClient(url);
        } catch (TTransportException e) {
            //log.error("HbaseClient ThttpClient failed!" + e.getMessage(), e);
            throw new RuntimeException(e);
        }
        httpClient.setCustomHeader(DO_AS, this.doAsUser);
        httpClient.setConnectTimeout(timeout);
        httpClient.setReadTimeout(timeout);
        httpClient.open();
        TProtocol protocol = new TBinaryProtocol(httpClient);
        client = new Hbase.Client(protocol);
        //log.info("HbaseClient create success!");
    }

# 解决方式

- 在hbase-site.xml新增如下配置

hbase.regionserver.thrift.http  true

hbase.thrift.support.proxyuser  true