Skip to content

Instantly share code, notes, and snippets.

@rjurney
Created March 25, 2025 17:10
Show Gist options
  • Save rjurney/61bb36265cf6eefdb9cf06b80be973b1 to your computer and use it in GitHub Desktop.
Save rjurney/61bb36265cf6eefdb9cf06b80be973b1 to your computer and use it in GitHub Desktop.
GraphFrames Pregel API - sum the ages of a node's neighbors
from graphframes.lib import AggregateMessages as AM
from graphframes.examples import Graphs
from pyspark.sql.functions import sum as sqlsum
g = Graphs(spark).friends() # Get example graph
# For each user, sum the ages of the adjacent users
msgToSrc = AM.dst["age"]
msgToDst = AM.src["age"]
agg = g.aggregateMessages(
sqlsum(AM.msg).alias("summedAges"),
sendToSrc=msgToSrc,
sendToDst=msgToDst)
agg.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment