hive、jdbc、ttransportexception:sasl身份验证未完成

svmlkihl  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(636)

我连接到配置单元并从表的行中获取数据的id。问题不会发生,当我连接到配置单元时,发送请求并获得响应。但是当我从resultset得到id时,我得到了一个异常:org.apache.thrift.transport.ttransportexception:sasl身份验证未完成。为什么会出现这种例外情况,需要做些什么来避免这种情况?对不起,我英语不好。
创建配置单元连接并发送请求是我的附属类:

public class HiveDataSearcher implements AutoCloseable {
private static final String hiveDriverName = "org.apache.hive.jdbc.HiveDriver";

static {
    try {
        Class.forName(hiveDriverName);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}

private Connection hiveConnection;

private String tableName;
private String whereBody;

public HiveDataSearcher(String url, String login, String password) {
    try {
        hiveConnection = DriverManager.getConnection(url, login, password);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }

    this.tableName = "";
    this.whereBody = "";
}

public HiveDataSearcher(Connection hiveConnection) {
    Objects.requireNonNull(hiveConnection, "hiveConnection");

    this.hiveConnection = hiveConnection;

    this.tableName = "";
    this.whereBody = "";
}

public String getTableName() {
    return tableName;
}

public HiveDataSearcher setTableName(String tableName) {
    Objects.requireNonNull(tableName, "tableName");

    this.tableName = tableName;

    return this;
}

public String getWhereBody() {
    return whereBody;
}

public HiveDataSearcher setWhereBody(String whereBody) {
    Objects.requireNonNull(whereBody, "whereBody");

    this.whereBody = whereBody;

    return this;
}

public ResultSet select(String ... selectParams) {
    return select(Arrays.asList(selectParams));
}

public ResultSet select(Iterable<String> selectParams) {
    String request = prepareRequest(selectParams);
    ResultSet response;

    try {
        response = hiveConnection
                .createStatement()
                .executeQuery(request);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }

    return response;
}

private String prepareRequest(Iterable<String> selectParams) {
    return new StringBuilder()
            .append("select").append(' ').append(selectParamsToHiveFormat(selectParams)).append(' ')
            .append("from").append(' ').append(tableName).append(' ')
            .append("where").append(' ').append(whereBody)
            .toString();
}

private String selectParamsToHiveFormat(Iterable<String> selectParams) {
    StringBuilder formattedSelectParams = new StringBuilder();

    for (String selectedParam : selectParams) {
        formattedSelectParams.append('\'').append(selectedParam).append('\'').append(',');
    }

    if (formattedSelectParams.length() == 0) {
        formattedSelectParams.append('*');
    } else {
        formattedSelectParams.deleteCharAt(formattedSelectParams.length() - 1);
    }

    return formattedSelectParams.toString();
}

public void close() {
    if (hiveConnection != null) {
        try {
            hiveConnection.close();
        } catch (SQLException e) {
            //nothing to do, just close connection
        } finally {
            hiveConnection = null;
        }
    }
}

}
这是我连接到hive的代码:

private static final String HIVE_URL = <hive url>;
private static final String HIVE_LOGIN = <hive login>;
private static final String HIVE_PASSWORD = <hive password>;

private static final String[] SEARCH_FIELDS = new String[] {"rowkey"};

private List<String> getIdsFromHive(String tableName, String whereBody) {
    ResultSet hiveResponse;

    try (HiveDataSearcher searcher = new HiveDataSearcher(HIVE_URL, HIVE_LOGIN, HIVE_PASSWORD)) {
        hiveResponse = searcher
                .setTableName(tableName)
                .setWhereBody(whereBody)
                .select(SEARCH_FIELDS);
    }

    List<String> ids = new ArrayList<>();

    try {
        while (hiveResponse.next()) { // in this place throw TTransportException
            ids.add(hiveResponse.getString(SEARCH_FIELDS[0]));
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }

    return ids;
}
ddhy6vgd

ddhy6vgd1#

在我的例子中,这个异常的原因是在关闭语句之前关闭了连接。所以我建议你检查一下你是否正确地维护了连接。
这是我的代码,希望能给你一些启发:
错误代码,请在关闭语句之前关闭连接:

Connection connection = null;
    Statement statement = null;
    try {
        connection = HIVEUTILS.getConnection();
        statement = connection.createStatement();
        statement.execute("DROP TABLE IF EXISTS tbl1");
        statement.execute("CREATE TABLE `tbl1` (`id` int)");
        statement.execute("INSERT INTO tbl1 VALUES(1)");
    }finally {
        if (connection != null){
            connection.close();
        }
        if (statement != null){
            statement.close(); // exception occur here.
        }
    }

正确的关闭顺序是:close resultset(如果有)->close语句->close连接。

Connection connection = null;
    Statement statement = null;
    try {
        connection = HIVEUTILS.getConnection();
        statement = connection.createStatement();
        statement.execute("DROP TABLE IF EXISTS tbl1");
        statement.execute("CREATE TABLE `tbl1` (`id` int)");
        statement.execute("INSERT INTO tbl1 VALUES(1)");
    }finally {
        if (statement != null){
            statement.close(); // change the order
        }
        if (connection != null){
            connection.close();
        }
    }

相关问题