Using the Connector Support for the Spark Streaming API¶
The Snowflake Connector for Spark’s support for the Spark Streaming API allows you to load data from Spark into tables in Snowflake in relatively small, frequent batches without manually executing a separate COPY command for each batch.
Before Reading This Topic¶
This topic assumes that you are already familiar with:
Spark and the Spark Streaming API.
A programming language that works with the Spark Streaming API (e.g. Scala, Java, or Python).
The Snowflake Connector for Spark and how it loads data in general.
Relevant Snowflake concepts, such as named internal stages for data files.
In addition, although not required, familiarity with Snowpipe might be helpful for a deeper understanding of how streaming works in the connector, particularly when troubleshooting issues.
Overview¶
Data in other systems is usually loaded in large batches at widely-spaced intervals (e.g. once per day). However, users often need to load data much more frequently.
Both Snowflake and Spark allow frequent loads (e.g. once per minute) of data files. We refer to this formally as « micro-batching » data and informally as « streaming » data.
Spark supports streaming via the Spark Streaming API. When the Streaming API is used, the system groups and processes records in small batches. When used in conjunction with the Snowflake Connector for Spark, these small batches are incrementally loaded into Snowflake as they are produced.
Spark Streaming API¶
The Spark Streaming API includes a function dataFrame.writeStream() that allows Spark to write to a stream that
serves as input for another system, such as Snowflake. When Spark uses the Snowflake streaming connector functions,
Spark writes to a special streaming dataFrame that in turn writes to a Snowflake internal stage (shared transfer point)
for data.
For more details, see:
Snowpipe¶
After the data is written to the Snowflake stage, the connector uses Snowpipe to read from the staged data files and load the data into a Snowflake table.
For more details about Snowpipe, see Implementation Details (in this topic) and Snowpipe.
Billing Considerations¶
Because the Snowflake Connector for Spark uses Snowpipe when streaming input, and because Snowpipe time is billable, you will see Snowpipe charges on your account for streamed input.
Streaming Mode¶
This topic focuses on the implementation of Spark Streaming API support in the Snowflake Connector for Spark. For the purposes of this topic, we refer to this as streaming mode for the connector.
In streaming mode, the connector works with a Spark job (i.e. application) that runs continuously; the job calls functions in Spark, which in turn call methods in the connector.
The key difference between streaming mode and non-streaming mode is:
In non-streaming mode, the process that transfers data stops after transferring each file.
In streaming mode, the process runs until explicitly stopped, continually looking for new files that contain data to transfer.
Note
Support for streaming mode is included in version 2.4.9 (and higher) of the Snowflake Connector for Spark.
The connector is a single .jar file that is implemented as a Spark plugin. It contains methods for both
streaming and non-streaming data transfers.
Prerequisites for Streaming¶
To use streaming mode in the connector, you should have the following:
Version of Spark that supports the Spark Streaming API.
Supported version of the Snowflake JDBC driver.
Version 2.4.9 (or higher) of the Snowflake Connector for Spark. For more details about installing the connector, see Installation et configuration du connecteur Spark.
Java ingestion SDK. For more details, see Data loading preparation using the Snowpipe REST API.
A table in Snowflake into which you can write the data. In addition, you must have the appropriate privileges (e.g. INSERT) on this table.
Privileges to create named internal stages.
Privileges to create Snowflake pipes.
In addition, you need a Spark application program that runs « continuously » and writes to a stream via the
dataFrame.writeStream() function. A sample program is included in this topic. We frequently use the term
« Spark job » to refer to this Spark application program.
Usage Notes¶
Streaming mode supports loading data from Spark into Snowflake only (i.e. it does not support loading data from Snowflake into Spark).
In streaming mode:
The typical latency of the connector is a few minutes.
If you specify a named internal stage and the stage does not already exist, then the connector automatically creates the stage.
The Snowflake Connector for Spark controls the interface to Snowpipe. You do not need to manually start (or stop) Snowpipe.
The connector cleans up data files in the stage after Snowpipe confirms that the data was loaded. If Snowpipe does not confirm that loading was successful, then the files will remain in the stage so that Snowpipe can try to load the data again. The files will remain until one of the following occurs:
Snowpipe succeeds in loading the data.
The file is manually removed.
The stage is dropped.
Restrictions that apply when using Snowpipe without streaming also apply when using Snowpipe with streaming. For example, Snowpipe does not guarantee that data will be loaded into tables in the same order as the files arrived in the stage. For more details about Snowpipe, see Snowpipe.
To avoid conflicts in loading and cleaning up files, the stage used by the streaming connector should not be used with any other streaming job or used for loading other files. Files in this stage will be created and removed automatically and should not be modified by the user. Users should not manually add or remove files from the Spark stage during streaming.
An additional advantage of streaming mode is that it allows a Spark application to send data asynchronously, rather than being blocked during writes to Snowflake.
Note
Streaming mode does not currently support streaming data directly from Databricks or Qubole. However, the connector still works in non-streaming mode with both Qubole and Databricks.
Support might be added in future versions of the connector.
Configuring the Connector Parameters for Streaming¶
To configure the connector, you must specify several parameters. Most of these parameters are the same parameters that you need to specify for non-streaming mode, but some are specific to streaming mode.
General Parameters¶
These parameters are common to both the streaming and non-streaming modes for the connector:
Parameter |
Description |
Example Value |
Comments |
|---|---|---|---|
|
Full hostname for your Snowflake account (provided by Snowflake). |
|
This value differs depending on your region. |
|
Name of the database that contains the destination table. |
|
|
|
Name of the schema that contains the destination table. |
|
|
|
Name of the Snowflake warehouse to use to provide the processing resources. |
|
|
|
Name of your Snowflake account. This parameter is obsolete. |
N/A |
Instead, specify your account name as part of your hostname in the |
|
Snowflake user login name. |
|
|
|
Destination table (i.e. table into which data will be loaded) |
|
|
|
Specifies whether to compress staging files ( |
|
Set this to |
Streaming Parameters¶
These parameters are required specifically for streaming mode:
Parameter |
Description |
Example Value |
Comments |
|---|---|---|---|
|
Private key (in PEM format) for key pair authentication. |
For instructions, see Using Key Pair Authentication & Key Rotation in this topic. |
|
|
Named internal stage from which Snowpipe reads the data files. |
|
This must be a unique name. No more than one pipe should be reading from this stage, and no more than one connector should write to this stage. |
Using Key Pair Authentication & Key Rotation¶
Snowflake supports using key pair authentication and key pair rotation.
Key pair authentication is an authentication option for supported Snowflake clients.
Key pair rotation provides enhanced key pair security.
After completing the initial configuration for Authentification par paire de clés et rotation de paires de clés, complete the following step.
Send an unencrypted copy of the private key using the
pem_private_keyconnection option.
Attention
For security reasons, rather than hard-coding this key in your application, you should set the parameter dynamically after reading the key from a secure source.
If the key is encrypted, then decrypt it and send the decrypted version.
Using Streaming with the Connector¶
These steps assume that you have already met the requirements in the Prerequisites for Streaming section.
Create (or copy and modify) a Spark job (typically in Scala, Java, or Python) that executes a
dataFrame.writeStream()call. For an example, see the sample code in this topic.In the Spark job, set the parameters that enable the connector to communicate with Snowflake and Snowpipe. The sample program that we’ve provided shows the required parameters. These parameters are also described in Configuring the Connector Parameters for Streaming (in this topic).
Start running the Spark job.
If you need to start the data source, or connect the data source to Spark, then do so.
Wait a few minutes (assuming that the Spark stream produces new output approximately every minute).
Verify that the data was loaded into the table. For example, use SnowSQL to query the table into which the data was loaded. If you don’t see the data, try again in a few minutes.
Starting and Stopping a Streaming Load Operation¶
Streaming starts automatically when you run your Spark job.
To stop streaming, stop your Spark job.
If files are still being loaded, the connector and Snowpipe might continue running temporarily and the command to stop streaming might not complete immediately.
As part of the procedure for stopping the streaming, the connector will shut down the Snowpipe pipe. You do not need to manually stop the pipe.
If the Spark job stops unexpectedly, then in most cases you can correct the problem on the Spark side and resume running the Spark job. In most such cases, the data staging area will still exist, so after you restart the Spark job, data will resume streaming into Snowflake. If not, please check the Troubleshooting section.
Implementation Details¶
This section is optional reading. You can use streaming mode without understanding much of its internals. However, if a problem occurs and you need to troubleshoot it, you might find this information helpful.
As described earlier, the dataFrame.writeStream() method works with the Snowflake Connector for Spark to write information
to a unique Snowflake data staging area (« stage »). This should be an internal stage, not an external stage.
The connector then interfaces with a Snowpipe pipe that ingests the data.
The staging area is monitored for new files, typically at an interval of approximately once per minute. When a new file appears, Snowpipe reads the file and loads the data into the destination table.
As each data file is loaded into the destination table, Snowflake updates a companion « context » file (in JSON format) that contains information such as the status of the load. Normally, you do not need to look at this file. If the data file is loaded successfully, Snowflake automatically cleans up both the data file and the corresponding context file shortly after the load (typically within a few minutes). However, if a problem occurs during loading, the context file and the data file are preserved. Either you or Snowflake Support can use the context file to help determine the cause of the problem.
Because the data file was also preserved, Snowpipe can try again to load the data after any problems have been solved. If some, but not all, data from a file is loaded before a problem occurs, and then the load is resumed after the problem has been fixed, Snowflake uses the context file to resume loading where it left off (i.e. to avoid loading the same data twice).
For more information about troubleshooting, see Troubleshooting (in this topic).
When you finish streaming data (e.g. if you no longer need data from a particular source, such as a set of « Internet of Things » devices), you can stop streaming the data. If you stop streaming data, Snowflake finishes loading any data that it has started to load and then shuts down the pipe. The shutdown process might take a few minutes, depending upon how much data is left to be loaded.
Important
You can access the data file and context file if you need to troubleshoot; however, you should not modify the context file or manually add or remove a context file under normal circumstances. You also should not add other files to the stage.
Sample Scala Program¶
This sample Scala program works with the streaming mode for the Snowflake Connector for Spark. This sample is based heavily on an
example provided in the Spark Streaming API documentation. To focus on streaming mode of the connector rather than on Spark data
processing, this example uses a socket as the data source from which readStream() reads.
Troubleshooting¶
If your data is not loaded into the table within a reasonable amount of time (typically several minutes or less if the data source produces new data approximately every minute and produces only relatively small amounts of data for each batch), then please contact your Snowflake representative for this preview.
Note that if you resume loading after stopping loading, you should use the same stage name when you resume. Otherwise, any data files left in the stage will not be processed.