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

org.apache.hama.graph.VertexInputReader Maven / Gradle / Ivy

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.hama.graph;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hama.bsp.BSPPeer;
import org.apache.hama.bsp.Partitioner;
import org.apache.hama.bsp.PartitioningRunner.RecordConverter;
import org.apache.hama.commons.util.KeyValuePair;

/**
 * A reader to read Hama's input files and parses a vertex out of it.
 * 
 * 
 * @param  the input format's KEYIN type.
 * @param  the input format's VALUE_IN type.
 * @param  the vertex id type.
 * @param  the Edge cost object type.
 * @param  the Vertex value/message object type.
 */
@SuppressWarnings("rawtypes")
public abstract class VertexInputReader
    implements RecordConverter {

  @SuppressWarnings("unchecked")
  @Override
  public void setup(Configuration conf) {
    // initialize the usual vertex structures for read/write methods
    GraphJobRunner. initClasses(conf);
  }

  private final KeyValuePair outputRecord = new KeyValuePair();

  /**
   * Parses a given key and value into the given vertex. If returned true, the
   * given vertex is considered finished and a new instance will be given in the
   * next call.
   */
  public abstract boolean parseVertex(KEYIN key, VALUEIN value,
      Vertex vertex) throws Exception;

  @SuppressWarnings("unchecked")
  @Override
  public KeyValuePair convertRecord(
      KeyValuePair inputRecord, Configuration conf)
      throws IOException {
    Class> vertexClass = (Class>) conf
        .getClass(GraphJob.VERTEX_CLASS_ATTR, Vertex.class);
    boolean vertexCreation;
    Vertex vertex = GraphJobRunner
        . newVertexInstance(vertexClass);
    try {
      vertexCreation = parseVertex((KEYIN) inputRecord.getKey(),
          (VALUEIN) inputRecord.getValue(), vertex);
    } catch (Exception e) {
      vertexCreation = false;
    }
    if (!vertexCreation) {
      throw new IOException(
          "Error parsing vertex. Please check your vertex input reader.");
    }

    outputRecord.setKey(vertex.getVertexID());
    outputRecord.setValue(vertex);
    return outputRecord;
  }

  @SuppressWarnings("unchecked")
  @Override
  public int getPartitionId(KeyValuePair inputRecord,
      Partitioner partitioner, Configuration conf, BSPPeer peer, int numTasks) {
    Vertex vertex = (Vertex) outputRecord.getValue();

    return Math.abs(partitioner.getPartition(vertex.getVertexID(),
        vertex.getValue(), numTasks));
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy