1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18package org.apache.giraph.jython;
1920import org.apache.giraph.comm.messages.InMemoryMessageStoreFactory;
21import org.apache.giraph.comm.messages.MessageStoreFactory;
22import org.apache.giraph.conf.GiraphConfiguration;
23import org.apache.giraph.conf.GiraphConstants;
24import org.apache.giraph.conf.GiraphTypes;
25import org.apache.giraph.edge.ByteArrayEdges;
26import org.apache.giraph.graph.Language;
27import org.apache.giraph.io.formats.IdWithValueTextOutputFormat;
28import org.apache.giraph.io.formats.IntNullTextEdgeInputFormat;
29import org.apache.giraph.scripting.DeployType;
30import org.apache.giraph.scripting.ScriptLoader;
31import org.apache.giraph.utils.InternalVertexRunner;
32import org.apache.hadoop.io.IntWritable;
33import org.apache.hadoop.io.NullWritable;
34import org.junit.Test;
3536import com.google.common.collect.Maps;
3738import java.util.Map;
3940importstatic org.junit.Assert.assertEquals;
4142publicclassTestJythonComputation {
4344 @Test
45publicvoid testCountEdgesInMemoryMessageStoreFactory() throws Exception {
46 testCountEdges(InMemoryMessageStoreFactory.class);
47 }
4849publicvoid testCountEdges(Class<? extends MessageStoreFactory>
50 messageStoreFactoryClass) throws Exception {
51 String[] edges = new String[] {
52"1 2",
53"2 3",
54"2 4",
55"4 1"56 };
5758 GiraphConfiguration conf = new GiraphConfiguration();
59 GiraphTypes types = new GiraphTypes(IntWritable.class, IntWritable.class,
60 NullWritable.class, NullWritable.class, NullWritable.class);
61 types.writeIfUnset(conf);
62 ScriptLoader.setScriptsToLoad(conf,
63"org/apache/giraph/jython/count-edges.py",
64 DeployType.RESOURCE, Language.JYTHON);
65 JythonUtils.init(conf, "CountEdges");
66 conf.setOutEdgesClass(ByteArrayEdges.class);
67 conf.setEdgeInputFormatClass(IntNullTextEdgeInputFormat.class);
68 conf.setVertexOutputFormatClass(IdWithValueTextOutputFormat.class);
69 GiraphConstants.MESSAGE_STORE_FACTORY_CLASS.set(conf,
70 messageStoreFactoryClass);
71 Iterable<String> results = InternalVertexRunner.run(conf, null, edges);
7273 Map<Integer, Integer> values = parseResults(results);
7475// Check that all vertices with outgoing edges have been created76 assertEquals(3, values.size());
77// Check the number of edges for each vertex78 assertEquals(1, (int) values.get(1));
79 assertEquals(2, (int) values.get(2));
80 assertEquals(1, (int) values.get(4));
81 }
8283privatestatic Map<Integer, Integer> parseResults(Iterable<String> results) {
84 Map<Integer, Integer> values = Maps.newHashMap();
85for (String line : results) {
86 String[] tokens = line.split("\\s+");
87int id = Integer.valueOf(tokens[0]);
88int value = Integer.valueOf(tokens[1]);
89 values.put(id, value);
90 }
91return values;
92 }
93 }