下面是通过边构造图的一个简单的例子:
package graphx;
import java.util.ArrayList;
import java.util.List;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.graphx.Edge;
import org.apache.spark.graphx.Graph;
import org.apache.spark.storage.StorageLevel;
import scala.reflect.ClassTag;
public class GraphxLearning {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SparkConf conf = new SparkConf().setAppName("Graphx Learning").setMaster("local");
JavaSparkContext sc = new JavaSparkContext(conf);
ClassTag<String> stringTag = scala.reflect.ClassTag$.MODULE$.apply(String.class);
List<Edge<String>> edges = new ArrayList<>();
edges.add(new Edge<String>(1L, 2L, "Friend1"));
edges.add(new Edge<String>(1L, 4L, "Friend2"));
edges.add(new Edge<String>(2L, 4L, "Friend3"));
edges.add(new Edge<String>(3L, 1L, "Friend4"));
edges.add(new Edge<String>(3L, 4L, "Friend5"));
JavaRDD<Edge<String>> edgesRDD = sc.parallelize(edges);
Graph<String, String> graph = Graph.fromEdges(edgesRDD.rdd(), "", StorageLevel.MEMORY_ONLY(), StorageLevel.MEMORY_ONLY(), stringTag, stringTag);
List<Edge<String>> e = graph.edges().toJavaRDD().collect();
System.out.println(e);
}
}
参考资料
[1]
API 接口文档
[2]
https://www.programcreek.com/java-api-examples/index.php?api=org.apache.spark.graphx.Graph
版权声明:本文为qq_27466159原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。