1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.chukwa.util;
19
20 import junit.framework.TestCase;
21 import java.util.*;
22 import java.io.*;
23 import org.apache.hadoop.chukwa.ChukwaArchiveKey;
24 import org.apache.hadoop.chukwa.ChunkImpl;
25 import org.apache.hadoop.chukwa.datacollection.DataFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.fs.FSDataOutputStream;
28 import org.apache.hadoop.fs.FileSystem;
29 import org.apache.hadoop.fs.Path;
30 import org.apache.hadoop.io.SequenceFile;
31
32 public class TestDumpChunks extends TestCase {
33
34 public static void writeSeqFile(Configuration conf, FileSystem fileSys, Path dest,
35 List<ChunkImpl> chunks) throws IOException {
36 FSDataOutputStream out = fileSys.create(dest);
37
38 Calendar calendar = Calendar.getInstance();
39 SequenceFile.Writer seqFileWriter = SequenceFile.createWriter(conf, out,
40 ChukwaArchiveKey.class, ChunkImpl.class,
41 SequenceFile.CompressionType.NONE, null);
42
43 for (ChunkImpl chunk: chunks) {
44 ChukwaArchiveKey archiveKey = new ChukwaArchiveKey();
45
46 calendar.set(Calendar.SECOND, 0);
47 calendar.set(Calendar.MILLISECOND, 0);
48 archiveKey.setTimePartition(calendar.getTimeInMillis());
49
50 archiveKey.setDataType(chunk.getDataType());
51 archiveKey.setStreamName(chunk.getStreamName());
52 archiveKey.setSeqId(chunk.getSeqID());
53 seqFileWriter.append(archiveKey, chunk);
54 }
55 seqFileWriter.close();
56 out.close();
57 }
58
59 public void testFilePatternMatching() throws IOException, java.net.URISyntaxException {
60
61 File tempDir = new File(System.getProperty("test.build.data", "/tmp"));
62
63 File tmpFile = File.createTempFile("dumpchunkTest", ".seq", tempDir);
64 tmpFile.deleteOnExit();
65
66 Configuration conf = new Configuration();
67 Path path = new Path(tmpFile.getAbsolutePath());
68 List<ChunkImpl> chunks = new ArrayList<ChunkImpl>();
69 byte[] dat = "test".getBytes();
70
71 ChunkImpl c = new ChunkImpl("Data", "aname", dat.length, dat, null);
72 chunks.add(c);
73
74 dat = "ing".getBytes();
75 c = new ChunkImpl("Data", "aname", dat.length+4, dat, null);
76 chunks.add(c);
77
78 writeSeqFile(conf, FileSystem.getLocal(conf), path, chunks);
79
80 String[] args = new String[] {"datatype=Data",path.toString()};
81 ByteArrayOutputStream capture = new ByteArrayOutputStream();
82 DumpChunks.dump(args, conf,new PrintStream(capture));
83
84 assertTrue(new String(capture.toByteArray()).startsWith("testing\n---"));
85
86
87 }
88
89 }