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.io.gora;
1920import java.io.IOException;
21import java.util.Map;
22import java.util.Set;
2324import org.apache.giraph.edge.Edge;
25import org.apache.giraph.edge.EdgeFactory;
26import org.apache.giraph.graph.Vertex;
27import org.apache.giraph.io.gora.generated.GVertex;
28import org.apache.hadoop.io.DoubleWritable;
29import org.apache.hadoop.io.FloatWritable;
30import org.apache.hadoop.io.LongWritable;
31import org.apache.hadoop.mapreduce.InputSplit;
32import org.apache.hadoop.mapreduce.TaskAttemptContext;
3334/**35 * Implementation of a specific reader for a generated data bean.36 */37publicclassGoraTestVertexInputFormat38extends GoraVertexInputFormat<LongWritable, DoubleWritable,
39 FloatWritable> {
4041/**42 * DEfault constructor43 */44publicGoraTestVertexInputFormat() {
45 }
4647/**48 * Creates specific vertex reader to be used inside Hadoop.49 * @param split split to be read.50 * @param context JobContext to be used.51 * @return GoraVertexReader Vertex reader to be used by Hadoop.52 */53 @Override
54public GoraVertexReader createVertexReader(
55 InputSplit split, TaskAttemptContext context) throws IOException {
56 putArtificialData();
57returnnewGoraGVertexVertexReader();
58 }
5960/**61 * Writes data into the data store in order to test it out.62 */63 @SuppressWarnings("unchecked")
64privatestaticvoid putArtificialData() {
65 getDataStore().put("1", createVertex("1", null));
66 getDataStore().put("10", createVertex("10", null));
67 getDataStore().put("100", createVertex("100", null));
68 getDataStore().flush();
69 }
7071/**72 * Creates a vertex using an id and a set of edges.73 * @param id Vertex id.74 * @param edges Set of edges.75 * @return GVertex created.76 */77publicstatic GVertex createVertex(String id, Map<String, String> edges) {
78 GVertex newVrtx = new GVertex();
79 newVrtx.setVertexId(id);
80if (edges != null) {
81for (String edgeId : edges.keySet())
82 newVrtx.getEdges().put(edgeId, edges.get(edgeId));
83 }
84return newVrtx;
85 }
8687/**88 * Gora vertex reader89 */90protectedclassGoraGVertexVertexReaderextends GoraVertexReader {
9192/**93 * Transforms a GoraObject into a Vertex object.94 * @param goraObject Object from Gora to be translated.95 * @return Vertex Result from transforming the gora object.96 */97 @Override
98protected Vertex<LongWritable, DoubleWritable, FloatWritable>
99 transformVertex(Object goraObject) {
100 Vertex<LongWritable, DoubleWritable, FloatWritable> vertex;
101/* create the actual vertex */102 vertex = getConf().createVertex();
103 GVertex tmpGVertex = (GVertex) goraObject;
104105 LongWritable vrtxId = new LongWritable(
106 Long.parseLong(tmpGVertex.getVertexId().toString()));
107 DoubleWritable vrtxValue = new DoubleWritable(tmpGVertex.getVertexValue());
108 vertex.initialize(vrtxId, vrtxValue);
109if (tmpGVertex.getEdges() != null && !tmpGVertex.getEdges().isEmpty()) {
110 Set<CharSequence> keyIt = tmpGVertex.getEdges().keySet();
111for (CharSequence key : keyIt) {
112 String keyVal = key.toString();
113 String valVal = tmpGVertex.getEdges().get(key).toString();
114 Edge<LongWritable, FloatWritable> edge;
115if (!keyVal.contains("vertexId")) {
116 edge = EdgeFactory.create(
117new LongWritable(Long.parseLong(keyVal)),
118new FloatWritable(Float.parseFloat(valVal)));
119 vertex.addEdge(edge);
120 }
121 }
122 }
123return vertex;
124 }
125 }
126 }