gpt4 book ai didi

java - Hive、JDBC、TTransportException : SASL authentication not complete

转载 作者:行者123 更新时间:2023-12-02 02:01:33 32 4
gpt4 key购买 nike

我连接到 Hive 并从表行中获取数据的 ID。当我连接到配置单元、发送请求并获得响应时,问题不会发生。但是当我从 ResultSet 获取 id 时,出现异常: org.apache.thrift.transport.TTransportException: SASL 身份验证未完成。为什么会出现这种异常以及需要采取哪些措施来避免这种异常?抱歉我的英语不好。

这是我的子类,用于创建 hive 连接并发送请求:

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;
}
}
}

}

这是我连接到配置单元的代码:

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;
}

最佳答案

就我而言,此异常的原因是在关闭语句之前关闭了连接。所以建议您检查一下是否正确保持了连接。

这是我的代码,希望它能给你一些启发:

代码错误,关闭语句前先关闭连接:

    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.
}
}

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

    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();
}
}

关于java - Hive、JDBC、TTransportException : SASL authentication not complete,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51503299/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com