Exemplo n.º 1
0
  public static void main(String... args) throws Exception {
    // Read an Atom Feed... this part isn't required.. the mapred stuff
    // works on any activity stream source, this just gives us some
    // interesting input material
    Abdera abdera = Abdera.getInstance();
    URL url = new URL("http://planet.intertwingly.net/atom.xml");
    Parser parser = abdera.getParser();
    ParserOptions options = parser.makeDefaultParserOptions().charset("UTF-8").get();
    Document<Feed> doc = abdera.getParser().parse(url.openStream(), url.toString(), options);
    Feed feed = doc.getRoot();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    feed.writeTo("activity", out);

    // Convert it to an Activity Stream
    String r = new String(out.toByteArray(), "UTF-8");
    Collection<Activity> col = IO.get().readCollection(new StringReader(r));

    // Prepare the input data.. here's where the interesting bit starts...
    // this first step indexes the collection of activities into a Iterable
    // of Pair objects. A Pair object is essentially a tuple with two elements,
    // called first() and second(). The first() is used as the key in the
    // Map function, while second() is used as the value. In this particular
    // case, we're using a null key on the input...
    PairBuilder<Void, Activity> gen =
        Pair.<Void, Activity>make().index(MoreFunctions.<Activity>alwaysVoid(), col.getItems());

    // The Function ff is asynchronous... we apply it, then call get on
    // the returned Future to wait for the result. The mapreduce operation
    // occurs in a different thread and sets the value of the Future
    // when it is complete... once it does, we iterate through the collection
    // of Pairs it kicks out.. which in this case, is a listing of actors
    // in the stream sorted by number of activities each.
    for (Pair<Integer, Iterable<String>> entry : ff.apply(gen).get())
      System.out.println(entry.first() + "=" + entry.second());
  }
Exemplo n.º 2
0
  @Test
  public void testPair() {
    Pair<String, String> pair = Pair.of("A", "B");
    assertEquals("A", pair.first());
    assertEquals("B", pair.second());

    Map<String, String> map = new HashMap<String, String>();
    map.put("A", "B");
    map.put("B", "C");
    Iterable<Pair<String, String>> pairs = Pair.from(map);
    assertEquals(2, Iterables.size(pairs));
    assertEquals("A", Iterables.get(pairs, 0).first());
    assertEquals("B", Iterables.get(pairs, 0).second());
    assertEquals("B", Iterables.get(pairs, 1).first());
    assertEquals("C", Iterables.get(pairs, 1).second());
  }