[
https://issues.apache.org/jira/browse/FLINK-1726?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14377663#comment-14377663
]
ASF GitHub Bot commented on FLINK-1726:
---------------------------------------
Github user vasia commented on a diff in the pull request:
https://github.com/apache/flink/pull/505#discussion_r27017030
--- Diff:
flink-staging/flink-gelly/src/main/java/org/apache/flink/graph/example/SimpleCommunityDetectionExample.java
---
@@ -0,0 +1,159 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.graph.example;
+
+import org.apache.flink.api.common.ProgramDescription;
+import org.apache.flink.api.common.functions.MapFunction;
+import org.apache.flink.api.java.DataSet;
+import org.apache.flink.api.java.ExecutionEnvironment;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.api.java.tuple.Tuple3;
+import org.apache.flink.graph.Edge;
+import org.apache.flink.graph.Graph;
+import org.apache.flink.graph.Vertex;
+import org.apache.flink.graph.example.utils.SimpleCommunityDetectionData;
+import org.apache.flink.graph.library.SimpleCommunityDetection;
+
+/**
+ * This example shows how to use the {@link
org.apache.flink.graph.library.SimpleCommunityDetection}
+ * library method:
+ * <ul>
+ * <li> with the edge data set given as a parameter
+ * <li> with default data
+ * </ul>
+ */
+public class SimpleCommunityDetectionExample implements ProgramDescription
{
+
+ public static void main(String [] args) throws Exception {
+
+ if(!parseParameters(args)) {
+ return;
+ }
+
+ // set up the execution environment
+ ExecutionEnvironment env =
ExecutionEnvironment.getExecutionEnvironment();
+
+ // set up the graph
+ DataSet<Edge<Long, Double>> edges = getEdgesDataSet(env);
+ DataSet<Vertex<Long, Tuple2<Long, Double>>> vertices =
assignInitialVertexValues(edges);
+ Graph<Long, Tuple2<Long, Double>, Double> graph =
Graph.fromDataSet(vertices, edges, env);
+
+ // set up the program
+ Graph<Long, Tuple2<Long, Double>, Double> communityGraph =
+ graph.run(new
SimpleCommunityDetection(maxIterations));
+
+ // the result is in the form of <vertexId, communityId>, where
the communityId is the label
+ // which the vertex converged to
+ DataSet<Vertex<Long, Tuple2<Long, Double>>> communityVertices =
communityGraph.getVertices();
+
+ DataSet<Tuple2<Long, Long>> result = communityVertices
+ .map(new MapFunction<Vertex<Long, Tuple2<Long,
Double>>, Tuple2<Long, Long>>() {
+
+ @Override
+ public Tuple2<Long, Long>
map(Vertex<Long, Tuple2<Long, Double>> vertex) throws Exception {
+ return new Tuple2<Long,
Long>(vertex.f0, vertex.f1.f0);
+ }
+ });
+
+ // emit result
+ if (fileOutput) {
+ result.writeAsCsv(outputPath, "\n", ",");
+ } else {
+ result.print();
+ }
+
+ env.execute("Executing Simple Community Detection Example");
+ }
+
+ @Override
+ public String getDescription() {
+ return "Simple Community Detection Example";
+ }
+
+ //
*************************************************************************
+ // UTIL METHODS
+ //
*************************************************************************
+
+ private static boolean fileOutput = false;
+ private static String edgeInputPath = null;
+ private static String outputPath = null;
+ private static Integer maxIterations =
SimpleCommunityDetectionData.MAX_ITERATIONS;
+
+ private static boolean parseParameters(String [] args) {
+ if(args.length > 0) {
+ if(args.length != 3) {
+ System.err.println("Usage
SimpleCommunityDetection <edge path> <output path> " +
+ "<num iterations>");
+ return false;
+ }
+
+ fileOutput = true;
+ edgeInputPath = args[0];
+ outputPath = args[1];
+ maxIterations = Integer.parseInt(args[2]);
+
+ } else {
+ System.out.println("Executing SimpleCommunityDetection
example with default parameters and built-in default data.");
+ System.out.println("Provide parameters to read input
data from files.");
+ System.out.println("Usage SimpleCommunityDetection
<edge path> <output path> " +
+ "<num iterations>");
+ }
+
+ return true;
+ }
+
+ @SuppressWarnings("serial")
+ private static DataSet<Edge<Long, Double>>
getEdgesDataSet(ExecutionEnvironment env) {
+
+ if(fileOutput) {
+ return env.readCsvFile(edgeInputPath)
+ .ignoreComments("#")
+ .fieldDelimiter("\t")
+ .lineDelimiter("\n")
+ .types(Long.class, Long.class,
Double.class)
+ .map(new MapFunction<Tuple3<Long, Long,
Double>, Edge<Long, Double>>() {
--- End diff --
There is a `Tuple3ToEdgeMap` in the graph utils :-)
> Add Community Detection Library and Example
> -------------------------------------------
>
> Key: FLINK-1726
> URL: https://issues.apache.org/jira/browse/FLINK-1726
> Project: Flink
> Issue Type: Task
> Components: Gelly
> Affects Versions: 0.9
> Reporter: Andra Lungu
> Assignee: Andra Lungu
>
> Community detection paper: http://arxiv.org/pdf/0808.2633.pdf
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)