This section provides a walk-through of writing data access code to Spark SQL in JDBC.
Connecting from Code
See Establishing a Connection for the prerequisite information you need to deploy the driver and configure the connection to Spark SQL. Connecting from Code shows how to connect with the DriverManager or SparkSQLDataSource classes.
Use the Statement and PreparedStatement classes to execute SQL to Spark SQL:
- See Executing Statements to execute Statements and iterate over the returned ResultSets.
- See Using Prepared Statements to execute parameterized statements. The PreparedStatement class provides a means to efficiently execute queries more than once and to mitigate SQL injection attacks.
See Batch Processing to take advantage of the Spark SQL bulk processing functionality. The driver converts the JDBC batch commands into a single request to Spark SQL.
Executing Stored Procedures
You can execute stored procedures as parameterized statements (with the CallableStatement class) or SQL statements (with the EXECUTE syntax): see Calling Stored Procedures.
Instantiate pooled connections with SparkSQLDataSource objects: see Connection Pooling to create and configure the pool.