简体   繁体   English

如何从包含顶点和边的文本文件创建图形?

[英]How can I create a graph from Text File containing the vertex and edges?

I have created an RDD of two input files ie Edges and Node files.我创建了一个包含两个输入文件的 RDD,即 Edges 和 Node 文件。 While I use the Graph.fromEdge() method to create a graph, I get errors.当我使用 Graph.fromEdge() 方法创建图形时,出现错误。 Could someone please help me?有人可以帮我吗? The inputEdgesTextFile and inputNodesTextFile are taking the input text dataset. inputEdgesTextFile 和 inputNodesTextFile 正在获取输入文本数据集。 On the very last line of the code, I am getting error.在代码的最后一行,我收到了错误。 I am posting an error that I am getting in my code.我发布了我在代码中遇到的错误。 在此处输入图像描述

public static void main(String[] args) {

    SparkConf conf = new SparkConf().setMaster("local").setAppName("GraphFileReadClass");
    JavaSparkContext javaSparkContext = new JavaSparkContext(conf);
    ClassTag<String> stringTag = scala.reflect.ClassTag$.MODULE$.apply(String.class);
    ClassTag<String> intTag = scala.reflect.ClassTag$.MODULE$.apply(Integer.class);

    $eq$colon$eq<String, String> tpEquals = scala.Predef.$eq$colon$eq$.MODULE$.tpEquals();
    // Load an external Text File in Apache spark
    //The text files number of lines and each line consists these structure
    //SFEdge contains: | Edge_id integer | Source_Id integer | Destination_id integer | EdgeLength double |
    //SFNodes contains: | Node_id integer | Longitude double | Latitude double |
    
    
    JavaRDD<String> inputEdgesTextFile = javaSparkContext.textFile("./SFEdges.txt");
    JavaRDD<String> inputNodesTextFile = javaSparkContext.textFile("./SFNodes.txt");
    ArrayList<Tuple2<Integer, Integer>> nodes = new ArrayList<>();
    ArrayList<Edge<Double>> edges = new ArrayList<>();

    JavaRDD<NodesClass> nodesPart = inputNodesTextFile.mapPartitions(p -> {
        ArrayList<NodesClass> nodeList = new ArrayList<NodesClass>();
        int counter = 0;
        while (p.hasNext()) {
            String[] parts = p.next().split(" ");
            NodesClass node = new NodesClass();
            node.setNode_Id(Integer.parseInt(parts[0]));
            node.setLongitude(Double.parseDouble(parts[1]));
            node.setLatitude(Double.parseDouble(parts[2]));
            nodes.add(new Tuple2<Integer, Integer>(counter, Integer.parseInt(parts[0])));
            nodeList.add(node);
            counter++;

        }
        return nodeList.iterator();
    });
    JavaRDD<Tuple2<Integer, Integer>> nodesRDD = javaSparkContext.parallelize(nodes);
    nodesRDD.foreach(data -> System.out.print("Node details: " + data._1() + " " + data._2()));

    JavaRDD<EdgeNetwork> edgesPart = inputEdgesTextFile.mapPartitions(p -> {
        ArrayList<EdgeNetwork> edgeList = new ArrayList<EdgeNetwork>();
        while (p.hasNext()) {

            String[] parts = p.next().split(" ");
            EdgeNetwork edgeNet = new EdgeNetwork();
            edgeNet.setEdge_id(Integer.parseInt(parts[0]));
            edgeNet.setSource_id(Integer.parseInt(parts[1]));
            edgeNet.setDestination_id(Integer.parseInt(parts[2]));
            edgeNet.setEdge_length(Double.parseDouble(parts[3]));
            edges.add(new Edge<Double>(Long.parseLong(parts[1]), Long.parseLong(parts[2]),
                    Double.parseDouble(parts[3])));
            edgeList.add(edgeNet);

        }
        return edgeList.iterator();
    });
    JavaRDD<Edge<Double>> edgesRDD = javaSparkContext.parallelize(edges);

    Graph<String, Double> graph = Graph.fromEdges(edgesRDD.rdd(), " ", StorageLevel.MEMORY_ONLY(),
            StorageLevel.MEMORY_ONLY(), stringTag, stringTag);
    //The warning shows above this line for Graph<String,Double>
    //Maybe the RDD that I have created has some errors. Please suggest me

Graph.fromEdges looks in Scala like Graph.fromEdges看起来像Scala

def fromEdges[VD: ClassTag, ED: ClassTag](
    edges: RDD[Edge[ED]],
    defaultValue: VD,
    edgeStorageLevel: StorageLevel = StorageLevel.MEMORY_ONLY,
    vertexStorageLevel: StorageLevel = StorageLevel.MEMORY_ONLY): Graph[VD, ED] = {
...

The two class tags are translated in the Java-API as additional parameters at the end of the method.两个 class 标记在 Java-API 中被转换为方法末尾的附加参数。 VD is a string type here and ED is a double type, so the Java call should reflect these types, the second class tag should be Double:这里VD是string类型, ED是double类型,所以调用Java应该反映这些类型,第二个class标签应该是Double:

ClassTag<Double> doubleTag = scala.reflect.ClassTag$.MODULE$.apply(Double.class);

Graph.fromEdges(edgesRDD.rdd(), " ", StorageLevel.MEMORY_ONLY(),
        StorageLevel.MEMORY_ONLY(), stringTag, doubleTag);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将字符串“转换”为顶点以在 Java 中创建图形? - How can I 'convert' a string to a vertex to create a graph in Java? 如何创建具有相同权重和相同源顶点的多个边? - How can I create multiple edges with the same weight and same source vertex? 如何在单个遍历中从一个顶点创建多个边 - How to create multiple edges from one vertex in single traversal 来自边缘文件的图形 - Graph from file of edges 如何使用Java查找图的中心(顶点,该顶点与其他每个顶点相连,但边指向图的中心) - how to Find the center of graph (vertex, that is connected with every other vertex, but edges are directed to the center of graph) with java 如何在Graph中创建相对于距离从单个节点传播的边 - How to create edges propagating from a single node with respect to distance in a Graph 如何测试图中的节点是否连接(有边)? - How can I test whether a node is connected (with edges) in a graph? 如何在for循环中向图形添加边? - How can I add edges to my graph in a for loop? 如何遍历图的顶点,而不是每个顶点都有单独的add.vertex? - How can I loop through a graph's vertices instead of having a separate add.vertex for each vertex? 如何读取包含文本和图像数据的文件? - How can I read a file containing text and image data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM