| 42 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 43 | |
| 44 | public class TestColumnSizes implements TestConf { |
| 45 | private Path workDir = new Path( |
| 46 | Paths.get(System.getProperty("test.tmp.dir"), "orc-test-sizes").toString()); |
| 47 | private FileSystem fs; |
| 48 | |
| 49 | @BeforeEach |
| 50 | public void openFileSystem() throws Exception { |
| 51 | fs = FileSystem.getLocal(conf); |
| 52 | fs.mkdirs(workDir); |
| 53 | fs.deleteOnExit(workDir); |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | public void testSizes() throws Exception { |
| 58 | TypeDescription schema = TypeDescription.fromString("struct<x:int,y:string>"); |
| 59 | Map<String, Integer> fileToRowCountMap = new LinkedHashMap<>(); |
| 60 | fileToRowCountMap.put(workDir + File.separator + "test-sizes-1.orc", 10000); |
| 61 | fileToRowCountMap.put(workDir + File.separator + "test-sizes-2.orc", 20000); |
| 62 | for (Map.Entry<String, Integer> fileToRowCount : fileToRowCountMap.entrySet()) { |
| 63 | Writer writer = OrcFile.createWriter(new Path(fileToRowCount.getKey()), |
| 64 | OrcFile.writerOptions(conf) |
| 65 | .setSchema(schema)); |
| 66 | VectorizedRowBatch batch = schema.createRowBatch(); |
| 67 | LongColumnVector x = (LongColumnVector) batch.cols[0]; |
| 68 | BytesColumnVector y = (BytesColumnVector) batch.cols[1]; |
| 69 | for (int r = 0; r < fileToRowCount.getValue(); ++r) { |
| 70 | int row = batch.size++; |
| 71 | x.vector[row] = r; |
| 72 | byte[] buffer = ("byte-" + r).getBytes(); |
| 73 | y.setRef(row, buffer, 0, buffer.length); |
| 74 | if (batch.size == batch.getMaxSize()) { |
| 75 | writer.addRowBatch(batch); |
| 76 | batch.reset(); |
| 77 | } |
| 78 | } |
| 79 | if (batch.size != 0) { |
| 80 | writer.addRowBatch(batch); |
| 81 | } |
| 82 | writer.close(); |
| 83 | } |
| 84 | |
| 85 | PrintStream origOut = System.out; |
| 86 | ByteArrayOutputStream myOut = new ByteArrayOutputStream(); |
| 87 | // replace stdout and run command |
| 88 | System.setOut(new PrintStream(myOut, false, StandardCharsets.UTF_8)); |
| 89 | ColumnSizes.main(conf, new String[]{"--summary", workDir.toString()}); |
| 90 | System.out.flush(); |
| 91 | System.setOut(origOut); |
| 92 | String output = myOut.toString(StandardCharsets.UTF_8); |
| 93 | assertTrue(output.contains("Total Files: 2")); |
| 94 | assertTrue(output.contains("Total Rows: 30000")); |
| 95 | assertTrue(output.contains("Percent Bytes/Row Name")); |
| 96 | assertTrue(output.contains("_stripe_footer")); |
| 97 | } |
| 98 | } |