Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spark Salesforce Connector Tutorial Using JDBC

DZone's Guide to

Spark Salesforce Connector Tutorial Using JDBC

We have a quick look at leveraging Apache Spark to access data in Salesforce via its JDBC connector. Come see how it's done!

· Big Data Zone ·
Free Resource

The open source HPCC Systems platform is a proven, easy to use solution for managing data at scale. Visit our Easy Guide to learn more about this completely free platform, test drive some code in the online Playground, and get started today.

Apache Spark, the open source big data processing framework, was built for speed, ease and complex analytics. Learn how to access Salesforce data in Spark.

Spark has several comprehensive advantages to MapReduce technologies such as Hadoop and Storm. Spark has an advanced directed acyclic graph (DAG) pattern that supports cyclic data flows and also allows programmers to develop multi-step pipelines. Several tasks can be performed on the same data through in-memory data sharing across DAGs. Using Apache Spark, one can run up to 100 times faster in memory and that is one of the major reasons most organizations want to use Spark.

The tricky part is getting access to data stored in other applications to leverage the power of Spark. A common scenario we see is the development of sophisticated transformations in the Spark framework with cloud application data, such as Salesforce, Eloqua or Marketo. Many developers are turning to the Progress DataDirect Salesforce Spark Connector and DataSource API of Spark to integrate Salesforce data in Spark. Sai Krishna Bobba, a developer evangelist at DataDirect, created this quick tutorial below to help you get started with your connection:

Accessing Salesforce Data in Spark

spark-shell --jars /path_to_driver/sforce.jar
  • The shell will start running. At the scala prompt, establish a connection to Salesforce and read a table with SQL Context load function using the following command: val dataframe_salesforce = sqlContext.read.format("jdbc").option("url","jdbc:datadirect:sforce://login.salesforce.com;").option("driver","com.ddtek.jdbc.sforce.SForceDriver").option("dbtable","SFORCE.<table_name>").option("user","<Username>").option("password","<password>").option("securitytoken","<security_token>").load() 
  • Once you are successfully authenticated the scala shell will display the schema of the table that you requested.
  • For future SQL queries, you can register the data as a temp table using the following command:
dataframe_salesforce.registerTempTable("account")
  • Now you can use dataframe to run SQL queries and access the Salesforce data using the following command:
  • dataframe_salesforce.sqlContext.sql("select * from account").collect.foreach(println)

    You should be able to see your result as shown below:

    Apache Spark

    All Finished

    We hope this tutorial helped you access Salesforce data and process your datasets in Spark. This demonstration is not limited to Salesforce. In fact, you can use the Spark’s DataSource API with any of the DataDirect JDBC Spark connectors or DataDirect Cloud JDBC Spark connectors to connect and integrate to over 50+ datasources including SaaS, Relational and Big data sources.

    Please contact us if you have any questions and share your comments below.

    Managing data at scale doesn’t have to be hard. Find out how the completely free, open source HPCC Systems platform makes it easier to update, easier to program, easier to integrate data, and easier to manage clusters. Download and get started today.

    Topics:
    apache spark ,salesforce ,connector ,jdbc ,how-to ,analytics ,big data

    Published at DZone with permission of

    Opinions expressed by DZone contributors are their own.

    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}