Fundamental operations on graphs

In this recipe, we will learn how to create graphs and do basic operations on them.

Getting ready

As a starting example, we will have three vertices, each representing the city center of three cities in California—Santa Clara, Fremont, and San Francisco. The following is the distance between these cities:

Source

Destination

Distance (miles)

Santa Clara, CA

Fremont, CA

20

Fremont, CA

San Francisco, CA

44

San Francisco, CA

Santa Clara, CA

53

How to do it…

  1. Import the GraphX-related classes:
    scala> import org.apache.spark.graphx._
    scala> import org.apache.spark.rdd.RDD
    
  2. Load the vertex data in an array:
    scala> val vertices = Array((1L, ("Santa Clara","CA")),(2L, ("Fremont","CA")),(3L, ("San Francisco","CA"))) ...

Get Spark Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.