The first graphx Program

Source: Internet
Author: User

Program function: Collect the location of the neighbor pointing to the vertex

/** Locate the region of the neighbor pointed to by each vertex */import Org. apache. spark. sparkcontextimport Org. apache. spark. sparkcontext. _ import Org. apache. spark. sparkconfimport Scala. collection. mutable. mapimport Org. apache. spark. _ import Org. apache. spark. graphx. _ import Org. apache. spark. RDD. rddobject testapp {def main (ARGs: array [String]) {Val conf = new sparkconf (). setappname ("testapp") Val SC = new sparkcontext (CONF) Val graph = graphloader. edgelistfile (SC, "/home/spark/graphx/data/followers.txt") // when an edge is loaded, the vertex Val users = SC appears on the edge. textfile ("/home/spark/graphx/data/users.txt "). map {line => Val fields = line. split (",") (fields (0 ). tolong, (fields (1), fields (2) // parse vertex data: ID (must be converted to long type), name, Region} Val mygraph = graph. apply (users, graph. edges) // re-graph. The vertex data is subject to users Val vertices = mygraph. mapreducetriplets [map [String, int] (// collect the region of the neighbor pointing to each point, triplet => iterator (triplet. srcid, map [String, int] (triplet. dstattr. _ 2-> 1), // map function one-way sends a message to the Source Vertex (A, B) with the directed edge) ==>{ // reduce function collects the message var mymap = map [String, int] () for (K, v) <-a) {If (B. contains (k) {var T = a (k) + B (k) mymap + = (k-> T )} else mymap + = (k-> A (k)} mymap // return the pooled result}) vertices. collect. foreach (A => Print (a + "\ n") // print the location of the collected neighbors }}


Users.txt vertex data: ID, name, Region

1,BarackObama,American2,ladygaga,American3,John,American4,xiaoming,Beijing6,Hanmeimei,Beijing7,Polly,American8,Tom,American


Followers.txt Edge Data: only source and target vertices are separated by spaces in the middle. Redundant columns are useless. For example, 2 1 Other has three columns of data, but graphx only reads the first two columns.

2 14 11 26 37 37 66 73 7

Result:

(4,Map(American -> 1))(6,Map(American -> 2))(2,Map(American -> 1))(1,Map(American -> 1))(3,Map(American -> 1))(7,Map(American -> 1))


Project directory structure:

./test.sbt./src./src/main./src/main/scala./src/main/scala/testApp.scala


Test. SBT content:

name := "test Project"version := "1.0"scalaVersion := "2.10.4"libraryDependencies += "org.apache.spark" %% "spark-core" % "1.0.1"libraryDependencies += "org.apache.spark" %% "spark-graphx" %"1.0.1"resolvers += "Akka Repository" at "http://repo.akka.io/releases/"



Execute the following command line for compilation:

sbt package



Execute the following command line to submit to the cluster (on the premise that the cluster is enabled). Here, my project is created in the spark installation directory under apps/testapp, so the following starts ../../

 ../../bin/spark-submit --class "testApp" --master local[4] target/scala-2.10/test-project_2.10-1.0.jar


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.