Working with Datasets from JDBC Data Sources (and PostgreSQL)
Start spark-shell
with the JDBC driver for the database you want to use. In our case, it is PostgreSQL JDBC Driver.
Note
|
Download the jar for PostgreSQL JDBC Driver 42.1.1 directly from the Maven repository. |
Tip
|
Execute the command to have the jar downloaded into
The entire path to the driver file is then like You should see the following while
|
Start ./bin/spark-shell
with –driver-class-path command line option and the driver jar.
1 2 3 4 5 |
SPARK_PRINT_LAUNCH_COMMAND=1 ./bin/spark-shell --driver-class-path /Users/jacek/.ivy2/jars/org.postgresql_postgresql-42.1.1.jar |
It will give you the proper setup for accessing PostgreSQL using the JDBC driver.
Execute the following to access projects
table in sparkdb
.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// that gives an one-partition Dataset val opts = Map( "url" -> "jdbc:postgresql:sparkdb", "dbtable" -> "projects") val df = spark. read. format("jdbc"). options(opts). load |
Note
|
Use user and password options to specify the credentials if needed.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
// Note the number of partition (aka numPartitions) scala> df.explain == Physical Plan == *Scan JDBCRelation(projects) [numPartitions=1] [id#0,name#1,website#2] ReadSchema: struct<id:int,name:string,website:string> scala> df.show(truncate = false) +---+------------+-----------------------+ |id |name |website | +---+------------+-----------------------+ |1 |Apache Spark|http://spark.apache.org| |2 |Apache Hive |http://hive.apache.org | |3 |Apache Kafka|http://kafka.apache.org| |4 |Apache Flink|http://flink.apache.org| +---+------------+-----------------------+ // use jdbc method with predicates to define partitions import java.util.Properties val df4parts = spark. read. jdbc( url = "jdbc:postgresql:sparkdb", table = "projects", predicates = Array("id=1", "id=2", "id=3", "id=4"), connectionProperties = new Properties()) // Note the number of partitions (aka numPartitions) scala> df4parts.explain == Physical Plan == *Scan JDBCRelation(projects) [numPartitions=4] [id#16,name#17,website#18] ReadSchema: struct<id:int,name:string,website:string> scala> df4parts.show(truncate = false) +---+------------+-----------------------+ |id |name |website | +---+------------+-----------------------+ |1 |Apache Spark|http://spark.apache.org| |2 |Apache Hive |http://hive.apache.org | |3 |Apache Kafka|http://kafka.apache.org| |4 |Apache Flink|http://flink.apache.org| +---+------------+-----------------------+ |
Troubleshooting
If things can go wrong, they sooner or later go wrong. Here is a list of possible issues and their solutions.
java.sql.SQLException: No suitable driver
Ensure that the JDBC driver sits on the CLASSPATH. Use –driver-class-path as described above (--packages
or --jars
do not work).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
scala> val df = spark. | read. | format("jdbc"). | options(opts). | load java.sql.SQLException: No suitable driver at java.sql.DriverManager.getDriver(DriverManager.java:315) at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions$$anonfun$7.apply(JDBCOptions.scala:84) at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions$$anonfun$7.apply(JDBCOptions.scala:84) at scala.Option.getOrElse(Option.scala:121) at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions.<init>(JDBCOptions.scala:83) at org.apache.spark.sql.execution.datasources.jdbc.JDBCOptions.<init>(JDBCOptions.scala:34) at org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider.createRelation(JdbcRelationProvider.scala:32) at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:301) at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:190) at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:158) ... 52 elided |
PostgreSQL Setup
Note
|
I’m on Mac OS X so YMMV (aka Your Mileage May Vary). |
Use the sections to have a properly configured PostgreSQL database.
Installation
Install PostgreSQL as described in…TK
Caution
|
This page serves as a cheatsheet for the author so he does not have to search Internet to find the installation steps. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
$ initdb /usr/local/var/postgres -E utf8 The files belonging to this database system will be owned by user "jacek". This user must also own the server process. The database cluster will be initialized with locale "pl_pl.utf-8". initdb: could not find suitable text search configuration for locale "pl_pl.utf-8" The default text search configuration will be set to "simple". Data page checksums are disabled. creating directory /usr/local/var/postgres ... ok creating subdirectories ... ok selecting default max_connections ... 100 selecting default shared_buffers ... 128MB selecting dynamic shared memory implementation ... posix creating configuration files ... ok creating template1 database in /usr/local/var/postgres/base/1 ... ok initializing pg_authid ... ok initializing dependencies ... ok creating system views ... ok loading system objects' descriptions ... ok creating collations ... ok creating conversions ... ok creating dictionaries ... ok setting privileges on built-in objects ... ok creating information schema ... ok loading PL/pgSQL server-side language ... ok vacuuming database template1 ... ok copying template1 to template0 ... ok copying template1 to postgres ... ok syncing data to disk ... ok WARNING: enabling "trust" authentication for local connections You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb. Success. You can now start the database server using: pg_ctl -D /usr/local/var/postgres -l logfile start |
Starting Database Server
Note
|
Consult 17.3. Starting the Database Server in the official documentation. |
Tip
|
Enable
Add |
Start the database server using pg_ctl
.
1 2 3 4 5 6 |
$ pg_ctl -D /usr/local/var/postgres -l logfile start server starting |
Alternatively, you can run the database server using postgres
.
1 2 3 4 5 |
$ postgres -D /usr/local/var/postgres |
Accessing Database
Use psql sparkdb
to access the database.
1 2 3 4 5 6 7 8 9 |
$ psql sparkdb psql (9.6.2) Type "help" for help. sparkdb=# |
Execute SELECT version()
to know the version of the database server you have connected to.
1 2 3 4 5 6 7 8 9 |
sparkdb=# SELECT version(); version -------------------------------------------------------------------------------------------------------------- PostgreSQL 9.6.2 on x86_64-apple-darwin14.5.0, compiled by Apple LLVM version 7.0.2 (clang-700.1.81), 64-bit (1 row) |
Use \h
for help and \q
to leave a session.
Creating Table
Create a table using CREATE TABLE
command.
1 2 3 4 5 6 7 8 9 |
CREATE TABLE projects ( id SERIAL PRIMARY KEY, name text, website text ); |
Insert rows to initialize the table with data.
1 2 3 4 5 6 7 8 |
INSERT INTO projects (name, website) VALUES ('Apache Spark', 'http://spark.apache.org'); INSERT INTO projects (name, website) VALUES ('Apache Hive', 'http://hive.apache.org'); INSERT INTO projects VALUES (DEFAULT, 'Apache Kafka', 'http://kafka.apache.org'); INSERT INTO projects VALUES (DEFAULT, 'Apache Flink', 'http://flink.apache.org'); |
Execute select * from projects;
to ensure that you have the following records in projects
table:
1 2 3 4 5 6 7 8 9 10 11 12 |
sparkdb=# select * from projects; id | name | website ----+--------------+------------------------- 1 | Apache Spark | http://spark.apache.org 2 | Apache Hive | http://hive.apache.org 3 | Apache Kafka | http://kafka.apache.org 4 | Apache Flink | http://flink.apache.org (4 rows) |
Stopping Database Server
1 2 3 4 5 |
pg_ctl -D /usr/local/var/postgres stop |