2017-01-30 3 views
1

Графовые рамки имеют приятный пример для мотивов с сохранением состояния. Как я могу явно вернуть счетчики? Как видите, вывод содержит только вершины и друзей, но не числа.spark graphframes stateful motif

Как я могу изменить его, чтобы не (только) иметь доступ к краям, но также доступ к меткам вершин?

when(relationship === "friend", cnt + 1).otherwise(cnt) 

I.e. как я мог бы улучшить подсчет сосчитать

  • друзей каждой вершины с возрастом> 30
  • процентное содержание, friendsGreater30/AllFriends

    val g = examples.Graphs.friends // get example graph 
    
    // Find chains of 4 vertices. 
    val chain4 = g.find("(a)-[ab]->(b); (b)-[bc]->(c); (c)-[cd]->(d)") 
    
    // Query on sequence, with state (cnt) 
    // (a) Define method for updating state given the next element of the motif. 
    def sumFriends(cnt: Column, relationship: Column): Column = { 
        when(relationship === "friend", cnt + 1).otherwise(cnt) 
    } 
    // (b) Use sequence operation to apply method to sequence of elements in motif. 
    //  In this case, the elements are the 3 edges. 
    val condition = Seq("ab", "bc", "cd"). 
        foldLeft(lit(0))((cnt, e) => sumFriends(cnt, col(e)("relationship"))) 
    // (c) Apply filter to DataFrame. 
    val chainWith2Friends2 = chain4.where(condition >= 2) 
    

    http://graphframes.github.io/user-guide.html

    chainWith2Friends2.show() 
    

, которые будут выводить

+-------------+------------+-------------+------------+-------------+------------+--------------+ 
|   a|   ab|   b|   bc|   c|   cd|    d| 
+-------------+------------+-------------+------------+-------------+------------+--------------+ 
|[e,Esther,32]|[e,d,friend]| [d,David,29]|[d,a,friend]| [a,Alice,34]|[a,e,friend]| [e,Esther,32]| 
|[e,Esther,32]|[e,d,friend]| [d,David,29]|[d,a,friend]| [a,Alice,34]|[a,b,friend]| [b,Bob,36]| 
| [d,David,29]|[d,a,friend]| [a,Alice,34]|[a,e,friend]|[e,Esther,32]|[e,d,friend]| [d,David,29]| 
| [d,David,29]|[d,a,friend]| [a,Alice,34]|[a,e,friend]|[e,Esther,32]|[e,f,follow]| [f,Fanny,36]| 
| [d,David,29]|[d,a,friend]| [a,Alice,34]|[a,b,friend]| [b,Bob,36]|[b,c,follow]|[c,Charlie,30]| 
| [a,Alice,34]|[a,e,friend]|[e,Esther,32]|[e,d,friend]| [d,David,29]|[d,a,friend]| [a,Alice,34]| 
+-------------+------------+-------------+------------+-------------+------------+--------------+ 

ответ

0

Обратите внимание, что sumFriends возвращает столбец, так condition столбец. Вот почему вы можете получить к нему доступ в операторе where без кавычек. Таким образом, все, что вам нужно сделать, это добавить этот столбец в ваш фреймворк. После выполнения кода, я могу запустить

chain4.withColumn("condition",condition).select("condition").show 

+---------+ 
|condition| 
+---------+ 
| 1| 
| 0| 
| 0| 
| 0| 
| 0| 
| 3| 
| 3| 
| 3| 
| 2| 
| 2| 
| 3| 
| 1| 
+---------+ 

вы также можете использовать chain4.select(condition)

Надеются, что это помогает

Смежные вопросы