All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.stratosphere.spargel.java.examples.SpargelConnectedComponents Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 * Copyright (C) 2010-2013 by the Stratosphere project (http://stratosphere.eu)
 *
 * Licensed 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 eu.stratosphere.spargel.java.examples;

import eu.stratosphere.api.java.DataSet;
import eu.stratosphere.api.java.ExecutionEnvironment;
import eu.stratosphere.api.java.functions.MapFunction;
import eu.stratosphere.api.java.tuple.Tuple2;
import eu.stratosphere.spargel.java.MessageIterator;
import eu.stratosphere.spargel.java.MessagingFunction;
import eu.stratosphere.spargel.java.VertexCentricIteration;
import eu.stratosphere.spargel.java.VertexUpdateFunction;
import eu.stratosphere.types.NullValue;

@SuppressWarnings({"serial", "unchecked"})
public class SpargelConnectedComponents {

	public static void main(String[] args) throws Exception {
		ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
		
		DataSet vertexIds = env.generateSequence(0, 10);
		DataSet> edges = env.fromElements(new Tuple2(0L, 2L), new Tuple2(2L, 4L), new Tuple2(4L, 8L),
															new Tuple2(1L, 5L), new Tuple2(3L, 7L), new Tuple2(3L, 9L));
		
		DataSet> initialVertices = vertexIds.map(new IdAssigner());
		
		DataSet> result = initialVertices.runOperation(VertexCentricIteration.withPlainEdges(edges, new CCUpdater(), new CCMessager(), 100));
		
		result.print();
		env.execute("Spargel Connected Components");
	}
	
	public static final class CCUpdater extends VertexUpdateFunction {
		@Override
		public void updateVertex(Long vertexKey, Long vertexValue, MessageIterator inMessages) {
			long min = Long.MAX_VALUE;
			for (long msg : inMessages) {
				min = Math.min(min, msg);
			}
			if (min < vertexValue) {
				setNewVertexValue(min);
			}
		}
	}
	
	public static final class CCMessager extends MessagingFunction {
		@Override
		public void sendMessages(Long vertexId, Long componentId) {
			sendMessageToAllNeighbors(componentId);
		}
	}
	
	/**
	 * A map function that takes a Long value and creates a 2-tuple out of it:
	 * 
(Long value) -> (value, value)
*/ public static final class IdAssigner extends MapFunction> { @Override public Tuple2 map(Long value) { return new Tuple2(value, value); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy