Connect to a SelectDB instance via Arrow Flight SQL
Arrow Flight SQL is built on Apache Arrow's columnar memory format and gRPC, delivering significantly higher throughput for bulk reads than the MySQL protocol. You can enable Arrow Flight SQL on an ApsaraDB for SelectDB instance and connect from Python ADBC, Java ADBC, and JDBC clients.
Scenarios
-
Bulk reads: When pulling millions of rows from SelectDB into Pandas, Spark, and similar engines, the columnar Arrow format avoids the per-row serialize/deserialize cost of MySQL protocol and pushes much higher throughput.
-
BI and data science integrations: Python (ADBC), Java (ADBC, JDBC), and BI tools such as DBeaver speak arrow-flight-sql; results map directly to Pandas DataFrames or Arrow Tables in memory.
Prerequisites
-
The SelectDB instance is in the Running state, and the instance version is Cloud 4.0.5 or later.
-
The client can access the SelectDB instance over the internal network. The Arrow Flight SQL protocol requires the client to reach the Arrow Flight port (8050) of the BE nodes. This port is open only within the VPC and is not accessible over the public network. If the client is not in the same VPC, use a VPC peering connection, Cloud Enterprise Network (CEN), or Express Connect to connect networks across VPCs or between a VPC and an IDC.
-
Cluster direct access is enabled on the instance. Open it from the Network Information panel on the instance detail page; this opens the internal ports needed by Arrow Flight, Connectors, and similar components inside the VPC. Without it, the ADBC client raises
i/o timeoutwhile fetching results. For more information, see Enable cluster direct connection. -
The client IP is added to the instance whitelist. If the client connects from a different VPC or an IDC, add the CIDR block of the client network.
Get connection information
-
Log on to the ApsaraDB for SelectDB console.
-
Pick the region of the instance in the top-left selector.
-
Click the instance ID in the list to open its detail page.
-
In the Network Information panel, collect:
-
VPC address, for example
selectdb-cn-xxxx.selectdbfe.rds.aliyuncs.com. -
Arrow Flight SQL port, default
8070. Confirm withADMIN SHOW FRONTEND CONFIG LIKE '%arrow_flight%'.
-
Connect with the Python ADBC driver
ADBC (Arrow Database Connectivity) is the Apache Arrow project's standard database connectivity API. The Python ADBC driver returns Arrow Tables that convert to Pandas DataFrames with zero copy.
Install dependencies
Install both the manager and the FlightSQL driver:
pip install adbc_driver_manager adbc_driver_flightsql
Connect and query
The script below connects to SelectDB and converts the result to a Pandas DataFrame:
import adbc_driver_manager
import adbc_driver_flightsql.dbapi as flight_sql
# Username/password options live in adbc_driver_manager.DatabaseOptions
conn = flight_sql.connect(
uri="grpc://<vpc_address>:<arrow_flight_sql_port>",
db_kwargs={
adbc_driver_manager.DatabaseOptions.USERNAME.value: "<db_user>",
adbc_driver_manager.DatabaseOptions.PASSWORD.value: "<db_password>",
},
)
cursor = conn.cursor()
cursor.execute("SELECT * FROM demo.example_table LIMIT 1000")
# fetchallarrow() returns an Arrow Table; convert to Pandas zero-copy
arrow_table = cursor.fetchallarrow()
df = arrow_table.to_pandas()
print(f"got {len(df)} rows")
print(df.head())
cursor.close()
conn.close()
Username and password options sit on adbc_driver_manager.DatabaseOptions, not adbc_driver_flightsql.DatabaseOptions. The latter only carries FlightSQL-specific options (OAUTH, TLS, and so on), and importing from there raises AttributeError: USERNAME.
A note on DDL / DML / management statements
CREATE TABLE / INSERT / SET / SHOW VARIABLES and similar commands can be submitted with cursor.execute() and take effect server-side, but reading the StatusResult requires the ADBC client to reach an FE node and may surface as i/o timeout. Run these statements over the MySQL 9030 protocol (pymysql, JDBC mysql driver) and reserve Arrow Flight SQL for bulk result-set reads.
Connect via JDBC
SelectDB is compatible with the open-source flight-sql-jdbc-core driver shipped by Apache Arrow. It works in Java applications, Spark JDBC, and BI tools such as DBeaver.
Add the Maven dependency
Add the dependency in pom.xml. Define the version in properties for easier upgrades:
<properties>
<arrow.version>17.0.0</arrow.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.arrow</groupId>
<artifactId>flight-sql-jdbc-core</artifactId>
<version>${arrow.version}</version>
</dependency>
</dependencies>
On JDK 9 and later, you must add --add-opens=java.base/java.nio=ALL-UNNAMED to the JVM, otherwise startup fails with module java.base does not "opens java.nio" to unnamed module. Example: java --add-opens=java.base/java.nio=ALL-UNNAMED -cp ... MainClass. In IntelliJ, add the same flag to Run/Debug Configurations - VM options.
Connect and query
The JDBC URL scheme is jdbc:arrow-flight-sql. The API otherwise matches jdbc:mysql, and the returned ResultSet wraps an Arrow Table:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ArrowFlightSqlExample {
public static void main(String[] args) throws Exception {
String url = "jdbc:arrow-flight-sql://<vpc_address>:<arrow_flight_sql_port>"
+ "?useServerPrepStmts=false"
+ "&useSSL=false"
+ "&useEncryption=false";
String user = "<db_user>";
String password = "<db_password>";
Class.forName("org.apache.arrow.driver.jdbc.ArrowFlightJdbcDriver");
try (Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("SELECT * FROM demo.example_table LIMIT 100");
while (rs.next()) {
System.out.println(rs.getInt(1) + " | " + rs.getString(2));
}
rs.close();
}
}
}
Connect with the Java ADBC driver
If your downstream pipeline already uses Arrow columnar format and you do not need a JDBC ResultSet's row-store representation, use the Java ADBC driver. It exposes Arrow VectorSchemaRoot directly, skipping the row-to-column conversion.
Add the Maven dependency
<properties>
<adbc.version>0.15.0</adbc.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.arrow.adbc</groupId>
<artifactId>adbc-core</artifactId>
<version>${adbc.version}</version>
</dependency>
<dependency>
<groupId>org.apache.arrow.adbc</groupId>
<artifactId>adbc-driver-manager</artifactId>
<version>${adbc.version}</version>
</dependency>
<dependency>
<groupId>org.apache.arrow.adbc</groupId>
<artifactId>adbc-driver-flight-sql</artifactId>
<version>${adbc.version}</version>
</dependency>
</dependencies>
Connect and iterate batches
import org.apache.arrow.adbc.core.AdbcConnection;
import org.apache.arrow.adbc.core.AdbcDatabase;
import org.apache.arrow.adbc.core.AdbcDriver;
import org.apache.arrow.adbc.core.AdbcStatement;
import org.apache.arrow.adbc.driver.flightsql.FlightSqlDriver;
import org.apache.arrow.flight.Location;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.ipc.ArrowReader;
import java.util.HashMap;
import java.util.Map;
public class ArrowFlightAdbcExample {
public static void main(String[] args) throws Exception {
BufferAllocator allocator = new RootAllocator();
FlightSqlDriver driver = new FlightSqlDriver(allocator);
Map<String, Object> parameters = new HashMap<>();
AdbcDriver.PARAM_URI.set(parameters,
Location.forGrpcInsecure("<vpc_address>", <arrow_flight_sql_port>).getUri().toString());
AdbcDriver.PARAM_USERNAME.set(parameters, "<db_user>");
AdbcDriver.PARAM_PASSWORD.set(parameters, "<db_password>");
try (AdbcDatabase db = driver.open(parameters);
AdbcConnection connection = db.connect();
AdbcStatement stmt = connection.createStatement()) {
stmt.setSqlQuery("SELECT * FROM demo.example_table LIMIT 100");
try (AdbcStatement.QueryResult result = stmt.executeQuery();
ArrowReader reader = result.getReader()) {
while (reader.loadNextBatch()) {
VectorSchemaRoot batch = reader.getVectorSchemaRoot();
System.out.println("rows in batch: " + batch.getRowCount());
// hand the batch to Spark / Flink / a custom compute layer here
}
}
}
allocator.close();
}
}
How to choose among the three Java options
|
Option |
Returned form |
Best for |
|
flight-sql-jdbc-core (JDBC) |
JDBC ResultSet (row store) |
Existing row-oriented Java apps that want a faster transport; BI tools such as DBeaver and Tableau. |
|
Java ADBC FlightSqlDriver |
Arrow VectorSchemaRoot (columnar) |
Pipelines that already work in Arrow columnar form, or that feed Spark / Flink which can consume Arrow natively. |
|
jdbc:mysql (baseline) |
JDBC ResultSet (row store) |
Small result sets and DDL/DML where transport speed is not the bottleneck. |
FAQ
Q: How do I fix i/o timeout, dial tcp x.x.x.x:8050?
A: The BE IP returned to the client is unreachable. Check, in order:
-
The client can reach the SelectDB instance over the internal network. Arrow Flight does not work over the public network. If the client is in a different VPC or an IDC, the networks are connected through a VPC peering connection, Cloud Enterprise Network (CEN), or Express Connect.
-
Cluster direct access is enabled in the instance's Network Information panel. Without it, the BE port is not opened to clients.
-
The client IP is in the instance whitelist.
Q: Python raises AttributeError: USERNAME.
A: Use adbc_driver_manager.DatabaseOptions.USERNAME. The FlightSQL driver's own DatabaseOptions does not carry the username/password keys.
Q: Java fails to start with module java.base does not "opens java.nio".
A: Add --add-opens=java.base/java.nio=ALL-UNNAMED to the JVM start arguments.
Q: Arrow Flight is no faster than jdbc:mysql in my benchmark.
A: The Arrow Flight advantage appears under bulk transfer. The gap is small for tiny result sets (under 10K rows) or DDL. Run benchmarks at 100K rows or more.