Query data from an OpenSearch Retrieval Engine Edition instance using the Java Database Connectivity (JDBC) SDK. This approach lets Java applications issue SQL-style queries against your search index without using a proprietary client library.
Prerequisites
Before you begin, ensure that you have:
An OpenSearch Retrieval Engine Edition instance
The instance API endpoint, username, and password — found in the API Endpoint section of the Instance Details page
How it works
All queries go through a Ha3Driver connection configured with your instance credentials. Submit queries using either PreparedStatement (for parameterized queries) or Statement (for direct SQL strings). Both return a Ha3ResultSet with an error-checking method.
Query data
Set up the connection
Register the Ha3 driver and open a connection using DriverManager:
import java.sql.*;
import java.util.Properties;
import com.aliyun.ha3engine.jdbc.Ha3ResultSet;
import com.aliyun.ha3engine.jdbc.common.exception.ErrorInfo;
public class Search {
private static Connection connection;
public void initConnection() throws ClassNotFoundException, SQLException {
String driverClass = "com.aliyun.ha3engine.jdbc.Ha3Driver"; // 1
Properties props = new Properties();
props.setProperty("serviceName", "<api-endpoint>"); // 2
props.setProperty("username", "<username>"); // 3
props.setProperty("password", "<password>"); // 4
props.setProperty("enableDynamicParams", "true"); // 5
props.setProperty("enableDetailLog", "true"); // 6
Class.forName(driverClass);
connection = DriverManager.getConnection("jdbc:ha3://", props);
}
}The fully qualified class name of the Ha3 JDBC driver.
The API endpoint of your instance, for example
ha-cn-i7*****605.public.ha.aliyuncs.com. Find it in the API Endpoint section of the Instance Details page.The username for your instance. Find it in the same API Endpoint section.
The password for your instance. To change it, use the API Endpoint section.
Enables dynamic parameters for
PreparedStatementqueries.Enables detailed logging for this connection.
Replace the placeholders with your actual values:
| Placeholder | Description | Where to find it |
|---|---|---|
<api-endpoint> | API endpoint of the instance | API Endpoint section, Instance Details page |
<username> | Username for the instance | API Endpoint section, Instance Details page |
<password> | Password for the instance | API Endpoint section, Instance Details page |
Use PreparedStatement for parameterized queries
Use PreparedStatement when query parameters are determined at runtime:
public void search1() throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement(
"select * from `test1` where `id` in " +
"(select `id` from `test1` where `count` > ? and `id` > ?)");
preparedStatement.setInt(1, 14); // count > 14
preparedStatement.setInt(2, 1); // id > 1
Ha3ResultSet resultSet = (Ha3ResultSet) preparedStatement.executeQuery();
ErrorInfo errorInfo = resultSet.getErrorInfo();
if (errorInfo != null && errorInfo.getErrorCode() != 0) {
System.out.println("Error code: " + errorInfo.getErrorCode());
System.out.println("Error: " + errorInfo.getError());
System.out.println("Message: " + errorInfo.getMessage());
} else {
while (resultSet.next()) {
System.out.println(resultSet.getInt("id"));
System.out.println(resultSet.getString("title"));
System.out.println(resultSet.getString("body"));
System.out.println(resultSet.getInt("count"));
System.out.println(resultSet.getDouble("price"));
}
}
}Use Statement for direct SQL queries
Use Statement when the full SQL string is known at compile time:
public void search2() throws SQLException {
Statement statement = connection.createStatement();
Ha3ResultSet resultSet = (Ha3ResultSet) statement.executeQuery(
"select `id`,`title`,`body` from `test1` " +
"where `id` in (select `id` from `test1` where `count` > 9)");
ErrorInfo errorInfo = resultSet.getErrorInfo();
if (errorInfo != null && errorInfo.getErrorCode() != 0) {
System.out.println("Error code: " + errorInfo.getErrorCode());
System.out.println("Error: " + errorInfo.getError());
System.out.println("Message: " + errorInfo.getMessage());
} else {
while (resultSet.next()) {
System.out.println(resultSet.getLong("id"));
System.out.println(resultSet.getString("title"));
System.out.println(resultSet.getString("body"));
}
}
}Error handling
Both query methods use the same error-checking pattern. After calling executeQuery(), check Ha3ResultSet.getErrorInfo() before iterating results:
If
getErrorInfo()returnsnullor an error code of0, the query succeeded — iterateresultSet.next()to read results.If the error code is non-zero, read
getError()andgetMessage()to diagnose the issue.