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.Arrays;
30  import java.util.Collection;
31  import java.util.Collections;
32  import java.util.Iterator;
33  
34  import org.apache.giraph.conf.GiraphConfiguration;
35  import org.apache.giraph.graph.BasicComputation;
36  import org.apache.giraph.graph.Vertex;
37  import org.apache.giraph.io.formats.IdWithValueTextOutputFormat;
38  import org.apache.giraph.utils.InternalVertexRunner;
39  import org.apache.hadoop.io.DoubleWritable;
40  import org.apache.hadoop.io.FloatWritable;
41  import org.apache.hadoop.io.LongWritable;
42  import org.junit.Test;
43  import org.junit.Assert;
44  
45  /**
46   * Test class for Gora vertex input/output formats.
47   */
48  public class TestGoraVertexInputFormat {
49  
50    @Test
51    public void getEmptyDb() throws Exception {
52      Iterable<String>    results;
53      Iterator<String>    result;
54      GiraphConfiguration conf    = new GiraphConfiguration();
55      GIRAPH_GORA_DATASTORE_CLASS.
56      set(conf, "org.apache.gora.memory.store.MemStore");
57      GIRAPH_GORA_KEYS_FACTORY_CLASS.
58      set(conf,"org.apache.giraph.io.gora.utils.DefaultKeyFactory");
59      GIRAPH_GORA_KEY_CLASS.set(conf,"java.lang.String");
60      GIRAPH_GORA_PERSISTENT_CLASS.
61      set(conf,"org.apache.giraph.io.gora.generated.GVertex");
62      GIRAPH_GORA_START_KEY.set(conf,"1");
63      GIRAPH_GORA_END_KEY.set(conf,"10");
64      conf.set("io.serializations",
65          "org.apache.hadoop.io.serializer.WritableSerialization," +
66          "org.apache.hadoop.io.serializer.JavaSerialization");
67      conf.setComputationClass(EmptyComputation.class);
68      conf.setVertexInputFormatClass(GoraTestVertexInputFormat.class);
69      results = InternalVertexRunner.run(conf, new String[0], new String[0]);
70      Assert.assertNotNull(results);
71      result = results.iterator();
72      Assert.assertFalse(result.hasNext());
73    }
74  
75    @Test
76    public void getTestDb() throws Exception {
77      Iterable<String>    results;
78      GiraphConfiguration conf    = new GiraphConfiguration();
79      GIRAPH_GORA_DATASTORE_CLASS.
80      set(conf, "org.apache.gora.memory.store.MemStore");
81      GIRAPH_GORA_KEYS_FACTORY_CLASS.
82      set(conf,"org.apache.giraph.io.gora.utils.DefaultKeyFactory");
83      GIRAPH_GORA_KEY_CLASS.set(conf,"java.lang.String");
84      GIRAPH_GORA_PERSISTENT_CLASS.
85      set(conf,"org.apache.giraph.io.gora.generated.GVertex");
86      GIRAPH_GORA_START_KEY.set(conf,"1");
87      GIRAPH_GORA_END_KEY.set(conf,"100");
88      conf.set("io.serializations",
89          "org.apache.hadoop.io.serializer.WritableSerialization," +
90          "org.apache.hadoop.io.serializer.JavaSerialization");
91      conf.setComputationClass(EmptyComputation.class);
92      conf.setVertexInputFormatClass(GoraTestVertexInputFormat.class);
93      conf.setVertexOutputFormatClass(IdWithValueTextOutputFormat.class);
94      results = InternalVertexRunner.run(conf, new String[0], new String[0]);
95      Assert.assertNotNull(results);
96      Assert.assertEquals(3, ((ArrayList<?>)results).size());
97      Collections.sort((ArrayList)results);
98      String[] correct = new String[] {"1\t0.0", "10\t0.0", "100\t0.0"};
99      Arrays.sort(correct);
100     for (int i = 0; i < correct.length; i++) {
101       Assert.assertEquals(correct[i],
102           ((ArrayList<?>) results).get(i).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       vertex.voteToHalt();
119     }
120   }
121 }