Example usage for org.apache.hadoop.io DoubleWritable DoubleWritable

List of usage examples for org.apache.hadoop.io DoubleWritable DoubleWritable

Introduction

In this page you can find the example usage for org.apache.hadoop.io DoubleWritable DoubleWritable.

Prototype

public DoubleWritable(double value) 

Source Link

Usage

From source file:org.apache.giraph.hive.common.HiveParsing.java

License:Apache License

/**
 * Parse edges as mappings of integer => double (id to weight)
 * @param record Hive record to parse//  w  w  w  .  j  a v a2 s .  c  o  m
 * @param columnIndex offset of column in row
 * @return edges
 */
@SuppressWarnings("unchecked")
public static Iterable<Edge<IntWritable, DoubleWritable>> parseIntDoubleEdges(HiveReadableRecord record,
        int columnIndex) {
    Object edgesObj = record.get(columnIndex);
    if (edgesObj == null) {
        return ImmutableList.of();
    }
    Map<Long, Double> readEdges = (Map<Long, Double>) edgesObj;
    List<Edge<IntWritable, DoubleWritable>> edges = Lists.newArrayListWithCapacity(readEdges.size());
    for (Map.Entry<Long, Double> entry : readEdges.entrySet()) {
        edges.add(EdgeFactory.create(new IntWritable(entry.getKey().intValue()),
                new DoubleWritable(entry.getValue())));
    }
    return edges;
}

From source file:org.apache.giraph.hive.common.HiveParsing.java

License:Apache License

/**
 * Parse edges as mappings of long => double (id to weight)
 * @param record Hive record to parse/*from  w w w.  ja  va 2s.  c o  m*/
 * @param columnIndex offset of column in row
 * @return edges
 */
@SuppressWarnings("unchecked")
public static Iterable<Edge<LongWritable, DoubleWritable>> parseLongDoubleEdges(HiveReadableRecord record,
        int columnIndex) {
    Object edgesObj = record.get(columnIndex);
    if (edgesObj == null) {
        return ImmutableList.of();
    }
    Map<Long, Double> readEdges = (Map<Long, Double>) edgesObj;
    List<Edge<LongWritable, DoubleWritable>> edges = Lists.newArrayListWithCapacity(readEdges.size());
    for (Map.Entry<Long, Double> entry : readEdges.entrySet()) {
        edges.add(EdgeFactory.create(new LongWritable(entry.getKey()), new DoubleWritable(entry.getValue())));
    }
    return edges;
}

From source file:org.apache.giraph.hive.computations.ComputationSumEdges.java

License:Apache License

@Override
public void compute(Vertex<IntWritable, DoubleWritable, DoubleWritable> vertex, Iterable<NullWritable> messages)
        throws IOException {
    double sum = 0;
    for (Edge<IntWritable, DoubleWritable> edge : vertex.getEdges()) {
        sum += edge.getValue().get();/*from  w w  w.j  a  va2  s . c om*/
    }
    vertex.setValue(new DoubleWritable(sum));
    vertex.voteToHalt();
}

From source file:org.apache.giraph.io.TestAdjacencyListTextVertexOutputFormat.java

License:Apache License

@Test
public void testVertexWithNoEdges() throws IOException, InterruptedException {
    TaskAttemptContext tac = mock(TaskAttemptContext.class);
    when(tac.getConfiguration()).thenReturn(conf);

    Vertex vertex = mock(Vertex.class);
    when(vertex.getId()).thenReturn(new Text("The Beautiful South"));
    when(vertex.getValue()).thenReturn(new DoubleWritable(32.2d));
    // Create empty iterable == no edges
    when(vertex.getEdges()).thenReturn(new ArrayList<Text>());

    RecordWriter<Text, Text> tw = mock(RecordWriter.class);
    AdjacencyListTextVertexWriter writer = createVertexWriter(tw);
    writer.setConf(conf);//from ww w  . java2s.  c  o  m
    writer.initialize(tac);
    writer.writeVertex(vertex);

    Text expected = new Text("The Beautiful South\t32.2");
    verify(tw).write(expected, null);
    verify(vertex, times(1)).getEdges();
}

From source file:org.apache.giraph.io.TestAdjacencyListTextVertexOutputFormat.java

License:Apache License

@Test
public void testVertexWithEdges() throws IOException, InterruptedException {
    TaskAttemptContext tac = mock(TaskAttemptContext.class);
    when(tac.getConfiguration()).thenReturn(conf);

    Vertex vertex = mock(Vertex.class);
    when(vertex.getId()).thenReturn(new Text("San Francisco"));
    when(vertex.getValue()).thenReturn(new DoubleWritable(0d));
    List<Edge<Text, DoubleWritable>> cities = Lists.newArrayList();
    Collections.addAll(cities, EdgeFactory.create(new Text("Los Angeles"), new DoubleWritable(347.16)),
            EdgeFactory.create(new Text("Phoenix"), new DoubleWritable(652.48)));

    when(vertex.getEdges()).thenReturn(cities);

    RecordWriter<Text, Text> tw = mock(RecordWriter.class);
    AdjacencyListTextVertexWriter writer = createVertexWriter(tw);
    writer.setConf(conf);/*  w  w w.j  a  v  a2  s  .c o  m*/
    writer.initialize(tac);
    writer.writeVertex(vertex);

    Text expected = new Text("San Francisco\t0.0\tLos Angeles\t347.16\t" + "Phoenix\t652.48");
    verify(tw).write(expected, null);
    verify(vertex, times(1)).getEdges();
}

From source file:org.apache.giraph.io.TestAdjacencyListTextVertexOutputFormat.java

License:Apache License

@Test
public void testWithDifferentDelimiter() throws IOException, InterruptedException {
    conf.set(AdjacencyListTextVertexOutputFormat.LINE_TOKENIZE_VALUE, ":::");
    TaskAttemptContext tac = mock(TaskAttemptContext.class);
    when(tac.getConfiguration()).thenReturn(conf);

    Vertex vertex = mock(Vertex.class);
    when(vertex.getId()).thenReturn(new Text("San Francisco"));
    when(vertex.getValue()).thenReturn(new DoubleWritable(0d));
    List<Edge<Text, DoubleWritable>> cities = Lists.newArrayList();
    Collections.addAll(cities, EdgeFactory.create(new Text("Los Angeles"), new DoubleWritable(347.16)),
            EdgeFactory.create(new Text("Phoenix"), new DoubleWritable(652.48)));

    when(vertex.getEdges()).thenReturn(cities);

    RecordWriter<Text, Text> tw = mock(RecordWriter.class);
    AdjacencyListTextVertexWriter writer = createVertexWriter(tw);
    writer.setConf(conf);//  ww  w.  j ava 2 s .c om
    writer.initialize(tac);
    writer.writeVertex(vertex);

    Text expected = new Text("San Francisco:::0.0:::Los Angeles:::347.16:::" + "Phoenix:::652.48");
    verify(tw).write(expected, null);
    verify(vertex, times(1)).getEdges();
}

From source file:org.apache.giraph.io.TestIdWithValueTextOutputFormat.java

License:Apache License

private void IdWithValueTestWorker(Text expected) throws IOException, InterruptedException {
    TaskAttemptContext tac = mock(TaskAttemptContext.class);
    when(tac.getConfiguration()).thenReturn(conf);

    Vertex vertex = mock(Vertex.class);
    when(vertex.getId()).thenReturn(new Text("Four Tops"));
    when(vertex.getValue()).thenReturn(new DoubleWritable(4d));

    // Create empty iterator == no edges
    when(vertex.getEdges()).thenReturn(new ArrayList<Text>());

    final RecordWriter<Text, Text> tw = mock(RecordWriter.class);
    IdWithValueVertexWriter writer = new IdWithValueVertexWriter() {
        @Override/*  w  w  w. j a va2  s . c  om*/
        protected RecordWriter<Text, Text> createLineRecordWriter(TaskAttemptContext context)
                throws IOException, InterruptedException {
            return tw;
        }
    };
    writer.setConf(conf);
    writer.initialize(tac);
    writer.writeVertex(vertex);

    verify(tw).write(expected, null);
    verify(vertex, times(0)).getEdges();
}

From source file:org.apache.giraph.io.TestLongDoubleDoubleAdjacencyListVertexInputFormat.java

License:Apache License

@Test
public void testHappyPath() throws Exception {
    String input = "42\t0.1\t99\t0.2\t2000\t0.3\t4000\t0.4";

    when(rr.getCurrentValue()).thenReturn(new Text(input));
    TextVertexReader vr = createVertexReader(rr);
    vr.setConf(conf);// w  w w . j a  v  a  2 s  . c o  m
    vr.initialize(null, tac);

    assertTrue("Should have been able to read vertex", vr.nextVertex());
    Vertex<LongWritable, DoubleWritable, DoubleWritable> vertex = vr.getCurrentVertex();
    assertValidVertex(conf, vertex, new LongWritable(42), new DoubleWritable(0.1),
            EdgeFactory.create(new LongWritable(99), new DoubleWritable(0.2)),
            EdgeFactory.create(new LongWritable(2000), new DoubleWritable(0.3)),
            EdgeFactory.create(new LongWritable(4000), new DoubleWritable(0.4)));
    assertEquals(vertex.getNumEdges(), 3);
}

From source file:org.apache.giraph.io.TestLongDoubleDoubleAdjacencyListVertexInputFormat.java

License:Apache License

@Test
public void testDifferentSeparators() throws Exception {
    String input = "12345:42.42:9999999:99.9";

    when(rr.getCurrentValue()).thenReturn(new Text(input));
    conf.set(AdjacencyListTextVertexInputFormat.LINE_TOKENIZE_VALUE, ":");
    TextVertexReader vr = createVertexReader(rr);
    vr.setConf(conf);//w ww.j  av a 2 s.  c o  m
    vr.initialize(null, tac);
    assertTrue("Should have been able to read vertex", vr.nextVertex());
    Vertex<LongWritable, DoubleWritable, DoubleWritable> vertex = vr.getCurrentVertex();
    assertValidVertex(conf, vertex, new LongWritable(12345), new DoubleWritable(42.42),
            EdgeFactory.create(new LongWritable(9999999), new DoubleWritable(99.9)));
    assertEquals(vertex.getNumEdges(), 1);
}

From source file:org.apache.giraph.io.TestTextDoubleDoubleAdjacencyListVertexInputFormat.java

License:Apache License

@Test
public void testHappyPath() throws Exception {
    String input = "Hi\t0\tCiao\t1.123\tBomdia\t2.234\tOla\t3.345";

    when(rr.getCurrentValue()).thenReturn(new Text(input));
    TextVertexReader vr = createVertexReader(rr);
    vr.setConf(conf);/*from   w w  w.j a v  a  2  s. c  om*/
    vr.initialize(null, tac);
    assertTrue("Should have been able to add a vertex", vr.nextVertex());
    Vertex<Text, DoubleWritable, DoubleWritable> vertex = vr.getCurrentVertex();
    assertValidVertex(conf, vertex, new Text("Hi"), new DoubleWritable(0),
            EdgeFactory.create(new Text("Ciao"), new DoubleWritable(1.123d)),
            EdgeFactory.create(new Text("Bomdia"), new DoubleWritable(2.234d)),
            EdgeFactory.create(new Text("Ola"), new DoubleWritable(3.345d)));
    assertEquals(vertex.getNumEdges(), 3);
}