PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/contrib/src/test/org/apache/hadoop/hive/contrib/mr/TestGenericMR.java

#
Java | 137 lines | 91 code | 24 blank | 22 comment | 4 complexity | 94eed74efbcfdb08aeb4b4c0e8593d87 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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.hadoop.hive.contrib.mr;
  19. import java.io.StringReader;
  20. import java.io.StringWriter;
  21. import java.util.Iterator;
  22. import java.util.NoSuchElementException;
  23. import junit.framework.TestCase;
  24. /**
  25. * TestGenericMR.
  26. *
  27. */
  28. public final class TestGenericMR extends TestCase {
  29. public void testReduceTooFar() throws Exception {
  30. try {
  31. new GenericMR().reduce(new StringReader("a\tb\tc"), new StringWriter(),
  32. new Reducer() {
  33. public void reduce(String key, Iterator<String[]> records,
  34. Output output) throws Exception {
  35. while (true) {
  36. records.next();
  37. }
  38. }
  39. });
  40. } catch (final NoSuchElementException nsee) {
  41. // expected
  42. return;
  43. }
  44. fail("Expected NoSuchElementException");
  45. }
  46. public void testEmptyMap() throws Exception {
  47. final StringWriter out = new StringWriter();
  48. new GenericMR().map(new StringReader(""), out, identityMapper());
  49. assertEquals(0, out.toString().length());
  50. }
  51. public void testIdentityMap() throws Exception {
  52. final String in = "a\tb\nc\td";
  53. final StringWriter out = new StringWriter();
  54. new GenericMR().map(new StringReader(in), out, identityMapper());
  55. assertEquals(in + "\n", out.toString());
  56. }
  57. public void testKVSplitMap() throws Exception {
  58. final String in = "k1=v1,k2=v2\nk1=v2,k2=v3";
  59. final String expected = "k1\tv1\nk2\tv2\nk1\tv2\nk2\tv3\n";
  60. final StringWriter out = new StringWriter();
  61. new GenericMR().map(new StringReader(in), out, new Mapper() {
  62. public void map(String[] record, Output output) throws Exception {
  63. for (final String kvs : record[0].split(",")) {
  64. final String[] kv = kvs.split("=");
  65. output.collect(new String[] {kv[0], kv[1]});
  66. }
  67. }
  68. });
  69. assertEquals(expected, out.toString());
  70. }
  71. public void testIdentityReduce() throws Exception {
  72. final String in = "a\tb\nc\td";
  73. final StringWriter out = new StringWriter();
  74. new GenericMR().reduce(new StringReader(in), out, identityReducer());
  75. assertEquals(in + "\n", out.toString());
  76. }
  77. public void testWordCountReduce() throws Exception {
  78. final String in = "hello\t1\nhello\t2\nokay\t4\nokay\t6\nokay\t2";
  79. final StringWriter out = new StringWriter();
  80. new GenericMR().reduce(new StringReader(in), out, new Reducer() {
  81. @Override
  82. public void reduce(String key, Iterator<String[]> records, Output output)
  83. throws Exception {
  84. int count = 0;
  85. while (records.hasNext()) {
  86. count += Integer.parseInt(records.next()[1]);
  87. }
  88. output.collect(new String[] {key, String.valueOf(count)});
  89. }
  90. });
  91. final String expected = "hello\t3\nokay\t12\n";
  92. assertEquals(expected, out.toString());
  93. }
  94. private Mapper identityMapper() {
  95. return new Mapper() {
  96. @Override
  97. public void map(String[] record, Output output) throws Exception {
  98. output.collect(record);
  99. }
  100. };
  101. }
  102. private Reducer identityReducer() {
  103. return new Reducer() {
  104. @Override
  105. public void reduce(String key, Iterator<String[]> records, Output output)
  106. throws Exception {
  107. while (records.hasNext()) {
  108. output.collect(records.next());
  109. }
  110. }
  111. };
  112. }
  113. }