Using Apache Spark to Query a Remote Authenticated MongoDB Server

Using Apache Spark to Query a Remote Authenticated MongoDB Server

<strong>Apache Spark is one of the most popular open source tools for big data. Learn how to use it to ingest data from a remote MongoDB server.</strong>

Apache Spark is one of the most popular open source tools for big data. Learn how to use it to ingest data from a remote MongoDB server.

Cloudera Data Flow, the answer to all your real-time streaming data problems. Manage your data from edge to enterprise with a no-code approach to developing sophisticated streaming applications easily. Learn more today.

1. Download and Extract Spark
$ wget http://apache.spinellicreations.com/spark/spark-2.4.0/spark-2.4.0-bin-hadoop2.7.tgz
$ tar -xf spark-2.4.0-bin-hadoop2.7.tgz
$ cd spark-2.4.0-bin-hadoop2.7

Create a spark-defaults.conf file by copying spark-defaults.conf.template in conf/.

Add the below line to the conf file.

spark.debug.maxToStringFields=1000
2. Connect to Mongo via a Remote Server

We use the MongoDB Spark Connector.

First, make sure the Mongo instance in the remote server has the bindIp set to the appropriate value and the correct local IP (not just localhost). Use the authentication root and password below to indicate the credentials of your authenticated Mongo database. 192.168.1.32 is your remote server’s private IP (i.e., the server where Mongo is running). We are reading the oplog.rs collection in the local database. Change these accordingly. Similarly, we are writing the outputs to the database, sparkoutput.

spark-2.4.0-bin-hadoop2.7]$ ./bin/pyspark --conf "spark.mongodb.input.uri=mongodb://root:[email protected]:27017/local.oplog.rs?readPreference=primaryPreferred" --conf "spark.mongodb.output.uri=mongodb://root:[email protected]:27017/sparkoutput" --packages org.mongodb.spark:mongo-spark-connector_2.11:2.4.0
Python 2.7.5 (default, Oct 30 2018, 23:45:53)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

Ivy Default Cache set to: /home/pkathi2/.ivy2/cache

The jars for the packages stored in: /home/pkathi2/.ivy2/jars

:: loading settings :: url = jar:file:/home/pkathi2/spark-2.4.0-bin-hadoop2.7/jars/ivy-2.4.0.jar!/org/apache/ivy/core/settings/ivysettings.xml

org.mongodb.spark#mongo-spark-connector_2.11 added as a dependency

:: resolving dependencies :: org.apache.spark#spark-submit-parent-33a37e02-1a24-498d-9217-e7025eeebd10;1.0

confs: [default]

found org.mongodb.spark#mongo-spark-connector_2.11;2.4.0 in central

found org.mongodb#mongo-java-driver;3.9.0 in central

:: resolution report :: resolve 256ms :: artifacts dl 5ms

:: modules in use:

org.mongodb#mongo-java-driver;3.9.0 from central in [default]

org.mongodb.spark#mongo-spark-connector_2.11;2.4.0 from central in [default]


| | modules || artifacts |

| conf | number| search|dwnlded|evicted|| number|dwnlded|


| default | 2 | 0 | 0 | 0 || 2 | 0 |


:: retrieving :: org.apache.spark#spark-submit-parent-33a37e02-1a24-498d-9217-e7025eeebd10

confs: [default]

0 artifacts copied, 2 already retrieved (0kB/6ms)

WARN NativeCodeLoader: This message means the systme is unable to load native-hadoop library for your platform… using built-in Java classes where applicable.

Set the default log level to “WARN”.

To adjust logging level, use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).

Welcome to


/ / ___ ___/ /

\ / _ / _ `/ __/ '/

/__ / .__/_,// /_/_\ version 2.4.0

/_/

Using Python version 2.7.5

SparkSession is available as ‘spark’.

>>> from pyspark.sql import SparkSession

>>> my_spark = SparkSession
... .builder
... .appName("myApp")
... .config("spark.mongodb.input.uri", "mongodb://root:[email protected]:27017/local.oplog.rs?authSource=admin")
... .config("spark.mongodb.output.uri", "mongodb://root:[email protected]:27017/sparkoutput?authSource=admin")
... .getOrCreate()

Make sure you are using the correct authentication source (i.e., where you authenticate yourself in the Mongo server).

3. Perform Queries on the Mongo Collection

Now you can perform queries on your remote Mongo collection through the Spark instance. For example, the below query finds the schema from the collection.

>>> df = spark.read.format("com.mongodb.spark.sql.DefaultSource").load()
>>> df.printSchema()
root
|-- h: long (nullable = true)
|-- ns: string (nullable = true)
|-- o: struct (nullable = true)
| |-- $set: struct (nullable = true)
| | |-- lastUse: timestamp (nullable = true)
| |-- $v: integer (nullable = true)

Originally published by Pradeeban Kathiravelu at https://dzone.com

Learn more

☞ The Complete Developers Guide to MongoDB

☞ Master MongoDB, the NOSQL leader with Clarity and Confidence

☞ MongoDB, NoSQL & Node: Mongoose, Azure & Database Management

☞ Build a ChatApp with: (Nodejs,Socketio, Expressjs ,MongoDB)

☞ GraphQL: Learning GraphQL with Node.Js

☞ Apache Spark 2.0 with Scala - Hands On with Big Data!

☞ Taming Big Data with Apache Spark and Python - Hands On!

☞ Apache Spark with Scala - Learn Spark from a Big Data Guru

☞ Apache Spark Hands on Specialization for Big Data Analytics

Apache Spark Tutorial - Apache Spark Full Course - Learn Apache Spark

Apache Spark Tutorial - Apache Spark Full Course - Learn Apache Spark

This video will help you understand and learn Apache Spark in detail. This Spark tutorial is ideal for both beginners as well as professionals who want to master Apache Spark concepts.

This video will help you understand and learn Apache Spark in detail. This Spark tutorial is ideal for both beginners as well as professionals who want to master Apache Spark concepts. Below are the topics covered in this Spark tutorial for beginners:

2:44 Introduction to Apache Spark

3:49 What is Spark?

5:34 Spark Eco-System

7:44 Why RDD?

16:44 RDD Operations

18:59 Yahoo Use-Case

21:09 Apache Spark Architecture

24:24 RDD

26:59 Spark Architecture

31:09 Demo

39:54 Spark RDD

41:09 Spark Applications

41:59 Need For RDDs

43:34 What are RDDs?

44:24 Sources of RDDs

45:04 Features of RDDs

46:39 Creation of RDDs

50:19 Operations Performed On RDDs

50:49 Narrow Transformations

51:04 Wide Transformations

51:29 Actions

51:44 RDDs Using Spark Pokemon Use-Case

1:05:19 Spark DataFrame

1:06:54 What is a DataFrame?

1:08:24 Why Do We Need Dataframes?

1:09:54 Features of DataFrames

1:11:09 Sources Of DataFrames

1:11:34 Creation Of DataFrame

1:24:44 Spark SQL

1:25:14 Why Spark SQL?

1:27:09 Spark SQL Advantages Over Hive

1:31:54 Spark SQL Success Story

1:33:24 Spark SQL Features

1:37:15 Spark SQL Architecture

1:39:40 Spark SQL Libraries

1:42:15 Querying Using Spark SQL

1:45:50 Adding Schema To RDDs

1:55:05 Hive Tables

1:57:50 Use Case: Stock Market Analysis with Spark SQL

2:16:50 Spark Streaming

2:18:10 What is Streaming?

2:25:46 Spark Streaming Overview

2:27:56 Spark Streaming workflow

2:31:21 Streaming Fundamentals

2:33:36 DStream

2:38:56 Input DStreams

2:40:11 Transformations on DStreams

2:43:06 DStreams Window

2:47:11 Caching/Persistence

2:48:11 Accumulators

2:49:06 Broadcast Variables

2:49:56 Checkpoints

2:51:11 Use-Case Twitter Sentiment Analysis

3:00:26 Spark MLlib

3:00:31 MLlib Techniques

3:01:46 Demo

3:11:51 Use Case: Earthquake Detection Using Spark

3:24:01 Visualizing Result

3:25:11 Spark GraphX

3:26:01 Basics of Graph

3:27:56 Types of Graph

3:38:56 GraphX

3:40:42 Property Graph

3:48:37 Creating & Transforming Property Graph

3:56:17 Graph Builder

4:02:22 Vertex RDD

4:07:07 Edge RDD

4:11:37 Graph Operators

4:24:37 GraphX Demo

4:34:24 Graph Algorithms

4:34:40 PageRank

4:38:29 Connected Components

4:40:39 Triangle Counting

4:44:09 Spark GraphX Demo

4;57:54 MapReduce vs Spark

5:13:03 Kafka with Spark Streaming

5:23:38 Messaging System

5:21:15 Kafka Components

2:23:45 Kafka Cluster

5:24:15 Demo

5:48:56 Kafka Spark Streaming Demo

6:17:16 PySpark Tutorial

6:21:26 PySpark Installation

6:47:06 Spark Interview Questions

Hadoop vs Spark | Hadoop MapReduce vs Spark

Hadoop vs Spark | Hadoop MapReduce vs Spark

🔥Intellipaat Big Data Hadoop Course: https://intellipaat.com/big-data-hadoop-training/ In this video on Hadoop vs Spark you will understand about the top Big...

In this video on Hadoop vs Spark you will understand about the top Big Data solutions used in the IT industry, and which one should you use for better performance. So in this Hadoop MapReduce vs Spark comparison some important parameters have been taken into consideration to tell you the difference between Hadoop and Spark also which one is preferred over the other in certain aspects in detail.

Why Hadoop is important

Big data hadoop is one of the best technological advances that is finding increased applications for big data and in a lot of industry domains. Data is being generated hugely in each and every industry domain and to process and distribute effectively hadoop is being deployed everywhere and in every industry.

Spark MLlib tutorial | Machine Learning On Spark | Apache Spark Tutorial

Spark MLlib tutorial | Machine Learning On Spark | Apache Spark Tutorial

This video on Spark MLlib Tutorial will help you learn about Spark's machine learning library. You will understand the different types of machine learning algorithms - supervised, unsupervised, and reinforcement learning.

Spark MLlib tutorial | Machine Learning On Spark | Apache Spark Tutorial

This video on Spark MLlib Tutorial will help you learn about Spark's machine learning library. You will understand the different types of machine learning algorithms - supervised, unsupervised, and reinforcement learning.

Then, you will get an idea about the various tools that Spark's MLlib component provides. You will see the different data types and some fundamental statistical analysis that you can perform using MLlib.

Finally, you will understand about classification and regression algorithms and implement it using linear and logistic regression. Now, let's get started and learn Spark MLlib.

Below topics are explained in this Spark MLlib tutorial:

  1. What is Spark MLlib? 00:42
  2. What is Machine Learning? 02:27
  3. Machine Learning Algorithms 04:51
  4. Spark MLlib Tools 09:14
  5. Spark MLlib Data Types 09:55
  6. Machine Learning Pipelines 22:18
  7. Clasification & Regression 24:13
  8. Spark MLlib Use Case Demo 31:51