Getting Started with ScalarDB Analytics with Spark
This version of ScalarDB Analytics with Spark was in private preview. Please use version 3.14 or later instead.
This guide explains how to get started with ScalarDB Analytics with Spark.
Prerequisites
Before you can run queries with ScalarDB Analytics with Spark, you'll need to set up ScalarDB tables and install Apache Spark.
Set up ScalarDB tables
To use ScalarDB Analytics with Spark, you need at least one underlying database in ScalarDB to run analytical queries on. If you have your own underlying database set up in ScalarDB, you can skip this section and use your database instead.
If you don't have your own database set up yet, you can set up ScalarDB with a sample underlying database by following the instructions in Run Analytical Queries on Sample Data by Using ScalarDB Analytics with Spark.
Install Apache Spark
You also need a packaged release of Apache Spark. If you already have Spark installed, you can skip this section.
If you need Spark, you can download it from the Spark website. After downloading the compressed Spark file, you'll need to uncompress the file by running the following command, replacing X.X.X
with the version of Spark that you downloaded:
tar xf spark-X.X.X-bin-hadoop3.tgz
Then, enter the directory by running the following command, again replacing X.X.X
with the version of Spark that you downloaded:
cd spark-X.X.X-bin-hadoop3
Configure the Spark shell
The following explains how to perform interactive analysis by using the Spark shell.
Since ScalarDB Analytics with Spark is available on the Maven Central Repository, you can use it to enable ScalarDB Analytics with Spark in the Spark shell by using the --packages
option, replacing <SPARK_VERSION>_<SCALA_VERSION>:<SCALARDB_ANALYTICS_WITH_SPARK_VERSION>
with the versions that you're using.
./bin/spark-shell --packages com.scalar-labs:scalardb-analytics-spark-<SPARK_VERSION>_<SCALA_VERSION>:<SCALARDB_ANALYTICS_WITH_SPARK_VERSION>
ScalarDB Analytics with Spark offers different artifacts for various Spark and Scala versions, provided in the format scalardb-analytics-spark-<SPARK_VERSION>_<SCALA_VERSION>
. Make sure that you select the artifact matching the Spark and Scala versions you're using.
For reference, see Version Compatibility of ScalarDB Analytics with Spark.
Next, you'll need to configure the ScalarDB Analytics with Spark environment in the shell. ScalarDB Analytics with Spark provides a helper method for this purpose, which get everything set up to run analytical queries for you.
spark-shell> import com.scalar.db.analytics.spark.implicits._
spark-shell> spark.setupScalarDbAnalytics(
| // ScalarDB config file
| configPath = "/<PATH_TO_YOUR_SCALARDB_PROPERTIES>/config.properties",
| // Namespaces in ScalarDB to import
| namespaces = Set("<YOUR_NAMESPACE_NAME_1>", "<YOUR_NAMESPACE_NAME_2>"),
| // License information
| license = License.certPath("""{"your":"license", "key":"in", "json":"format"}""", "/<PATH_TO_YOUR_LICENSE>/cert.pem")
| )
Now, you can read data from the tables in the underlying databases of ScalarDB and run any arbitrary analytical queries through the Spark Dataset API. For example:
spark-shell> spark.sql("select * from <YOUR_NAMESPACE_NAME_1>.<YOUR_TABLE_NAME>").show()