This project has retired. For details please refer to its Attic page.
SinkFileValidator xref
View Javadoc

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  
19  package org.apache.hadoop.chukwa.datacollection.test;
20  
21  
22  import java.io.IOException;
23  import java.net.URI;
24  import java.net.URISyntaxException;
25  import java.nio.charset.Charset;
26  
27  import org.apache.hadoop.chukwa.ChukwaArchiveKey;
28  import org.apache.hadoop.chukwa.ChunkImpl;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.fs.FileSystem;
31  import org.apache.hadoop.fs.Path;
32  import org.apache.hadoop.io.SequenceFile;
33  import org.apache.hadoop.io.Writable;
34  
35  public class SinkFileValidator {
36  
37    public static void main(String[] args) {
38      String fsURL = "hdfs://localhost:9000";
39      String fname;
40      if (args.length < 1) {
41        System.out
42            .println("usage:  SinkFileValidator <filename> [filesystem URI] ");
43        return;
44      }
45      fname = args[0];
46      if (args.length > 1)
47        fsURL = args[1];
48  
49      Configuration conf = new Configuration();
50      try {
51        FileSystem fs;
52        if (fsURL.equals("local"))
53          fs = FileSystem.getLocal(conf);
54        else
55          fs = FileSystem.get(new URI(fsURL), conf);
56        SequenceFile.Reader r = new SequenceFile.Reader(fs, new Path(fname), conf);
57        System.out.println("key class name is " + r.getKeyClassName());
58        System.out.println("value class name is " + r.getValueClassName());
59  
60        ChukwaArchiveKey key = new ChukwaArchiveKey();
61        ChunkImpl evt = ChunkImpl.getBlankChunk();
62        int events = 0;
63        while (r.next(key, evt) && (events < 5)) {
64          if (!Writable.class.isAssignableFrom(key.getClass()))
65            System.out.println("warning: keys aren't writable");
66  
67          if (!Writable.class.isAssignableFrom(evt.getClass()))
68            System.out.println("warning: values aren't writable");
69  
70          if (evt.getData().length > 1000) {
71            System.out.println("got event; data: "
72                + new String(evt.getData(), 0, 1000, Charset.forName("UTF-8")));
73            System.out.println("....[truncating]");
74          } else
75            System.out.println("got event; data: " + new String(evt.getData(), Charset.forName("UTF-8")));
76          events++;
77        }
78        System.out.println("file looks OK!");
79      } catch (IOException e) {
80        e.printStackTrace();
81      } catch (URISyntaxException e) {
82        e.printStackTrace();
83      }
84  
85    }
86  
87  }