Over a million developers have joined DZone.

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

Learn how you can maximize big data in the cloud with Apache Hadoop. Download this eBook now. Brought to you in partnership with Hortonworks.

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.

    Hortonworks DataFlow is an integrated platform that makes data ingestion fast, easy, and secure. Download the white paper now.  Brought to you in partnership with Hortonworks

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

    Published at DZone with permission of Nishanth Kadiyala, DZone MVB. See the original article here.

    Opinions expressed by DZone contributors are their own.

    The best of DZone straight to your inbox.

    SEE AN EXAMPLE
    Please provide a valid email address.

    Thanks for subscribing!

    Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
    Subscribe

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

    {{ parent.tldr }}

    {{ parent.urlSource.name }}