This project has retired. For details please refer to its Attic page.
TestGoraEdgeInputFormat xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * 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 and
16   * limitations under the License.
17   */
18  package org.apache.giraph.io.gora;
19  
20  import static org.apache.giraph.io.gora.constants.GiraphGoraConstants.GIRAPH_GORA_DATASTORE_CLASS;
21  import static org.apache.giraph.io.gora.constants.GiraphGoraConstants.GIRAPH_GORA_END_KEY;
22  import static org.apache.giraph.io.gora.constants.GiraphGoraConstants.GIRAPH_GORA_KEYS_FACTORY_CLASS;
23  import static org.apache.giraph.io.gora.constants.GiraphGoraConstants.GIRAPH_GORA_KEY_CLASS;
24  import static org.apache.giraph.io.gora.constants.GiraphGoraConstants.GIRAPH_GORA_PERSISTENT_CLASS;
25  import static org.apache.giraph.io.gora.constants.GiraphGoraConstants.GIRAPH_GORA_START_KEY;
26  
27  import java.io.IOException;
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.Iterator;
31  
32  import org.apache.giraph.conf.GiraphConfiguration;
33  import org.apache.giraph.graph.BasicComputation;
34  import org.apache.giraph.graph.Vertex;
35  import org.apache.giraph.io.formats.IdWithValueTextOutputFormat;
36  import org.apache.giraph.utils.InternalVertexRunner;
37  import org.apache.hadoop.io.DoubleWritable;
38  import org.apache.hadoop.io.FloatWritable;
39  import org.apache.hadoop.io.LongWritable;
40  import org.junit.Test;
41  import org.junit.Assert;
42  
43  /**
44   * Test class for Gora vertex input/output formats.
45   */
46  public class TestGoraEdgeInputFormat {
47  
48    @Test
49    public void getEmptyDb() throws Exception {
50      Iterable<String>    results;
51      Iterator<String>    result;
52      GiraphConfiguration conf    = new GiraphConfiguration();
53      GIRAPH_GORA_DATASTORE_CLASS.
54      set(conf, "org.apache.gora.memory.store.MemStore");
55      GIRAPH_GORA_KEYS_FACTORY_CLASS.
56      set(conf,"org.apache.giraph.io.gora.utils.DefaultKeyFactory");
57      GIRAPH_GORA_KEY_CLASS.set(conf,"java.lang.String");
58      GIRAPH_GORA_PERSISTENT_CLASS.
59      set(conf,"org.apache.giraph.io.gora.generated.GEdge");
60      GIRAPH_GORA_START_KEY.set(conf,"1");
61      GIRAPH_GORA_END_KEY.set(conf,"3");
62      conf.set("io.serializations",
63          "org.apache.hadoop.io.serializer.JavaSerialization," +
64          "org.apache.hadoop.io.serializer.WritableSerialization");
65      conf.setComputationClass(EmptyComputation.class);
66      conf.setEdgeInputFormatClass(GoraGEdgeEdgeInputFormat.class);
67      results = InternalVertexRunner.run(conf, new String[0], new String[0]);
68      Assert.assertNotNull(results);
69      result = results.iterator();
70      Assert.assertFalse(result.hasNext());
71    }
72  
73    @Test
74    public void getTestDb() throws Exception {
75      Iterable<String>    results;
76      GiraphConfiguration conf    = new GiraphConfiguration();
77      GIRAPH_GORA_DATASTORE_CLASS.
78      set(conf, "org.apache.gora.memory.store.MemStore");
79      GIRAPH_GORA_KEYS_FACTORY_CLASS.
80      set(conf,"org.apache.giraph.io.gora.utils.DefaultKeyFactory");
81      GIRAPH_GORA_KEY_CLASS.set(conf,"java.lang.String");
82      GIRAPH_GORA_PERSISTENT_CLASS.
83      set(conf,"org.apache.giraph.io.gora.generated.GEdge");
84      GIRAPH_GORA_START_KEY.set(conf,"1");
85      GIRAPH_GORA_END_KEY.set(conf,"4");
86      conf.set("io.serializations",
87          "org.apache.hadoop.io.serializer.WritableSerialization," +
88          "org.apache.hadoop.io.serializer.JavaSerialization");
89      conf.setComputationClass(EmptyComputation.class);
90      conf.setEdgeInputFormatClass(GoraTestEdgeInputFormat.class);
91      conf.setVertexOutputFormatClass(IdWithValueTextOutputFormat.class);
92      results = InternalVertexRunner.run(conf, new String[0], new String[0]);
93      Assert.assertNotNull(results);
94      Assert.assertEquals(3, ((ArrayList<?>)results).size());
95      if (results instanceof Collection<?>
96      & (((Collection<?>)results).size() == 2)) {
97        Assert.assertEquals("33\t0.0",
98            ((ArrayList<?>)results).get(0).toString());
99        Assert.assertEquals("22\t0.0",
100           ((ArrayList<?>)results).get(1).toString());
101       Assert.assertEquals("11\t0.0",
102           ((ArrayList<?>)results).get(2).toString());
103     }
104   }
105 
106   /*
107   Test compute method that sends each edge a notification of its parents.
108   The test set only has a 1-1 parent-to-child ratio for this unit test.
109    */
110   public static class EmptyComputation
111     extends BasicComputation<LongWritable, DoubleWritable,
112     FloatWritable, LongWritable> {
113 
114     @Override
115     public void compute(
116         Vertex<LongWritable, DoubleWritable, FloatWritable> vertex,
117         Iterable<LongWritable> messages) throws IOException {
118       Assert.assertNotNull(vertex);
119       vertex.voteToHalt();
120     }
121   }
122 }