hadoop_tools: Head.java

File Head.java, 2.8 KB (added by liaojiaohe, 14 years ago)
Line 
1/*
2 * Copyright 1997-2012
3 * http://www.pconline.com.cn
4 */
5package cn.pconline.hadooptools;
6
7import java.io.IOException;
8import java.util.StringTokenizer;
9import org.apache.commons.compress.archivers.ArchiveException;
10import org.apache.commons.logging.Log;
11import org.apache.commons.logging.LogFactory;
12import org.apache.hadoop.conf.Configuration;
13import org.apache.hadoop.fs.FSDataInputStream;
14import org.apache.hadoop.fs.FileSystem;
15import org.apache.hadoop.fs.Path;
16import org.apache.hadoop.io.compress.CompressionInputStream;
17import org.apache.hadoop.io.compress.GzipCodec;
18import org.apache.hadoop.util.GenericOptionsParser;
19
20/**
21 *
22 * @author larry <larryliao@pconline.com.cn>
23 */
24public class Head {
25
26    private static final Log LOG =
27            LogFactory.getLog(Head.class.getName());
28    static final String NAME = "Head";
29    static int MaxLineNumber = 10;
30
31    public static void main(String[] args) throws IOException, ArchiveException {
32        Configuration conf = new Configuration();
33        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
34
35        if (otherArgs.length < 1) {
36            System.err.println("ERROR: Wrong number of parameters: " + args.length);
37            System.err.println("Usage: Head -conf <configuration file> -D <property=value>"
38                    + " <filePath> [lineNumber]");
39
40            System.exit(-1);
41        }
42
43        FileSystem hdfs = FileSystem.get(conf); //获埗HDFS文件系统的对象
44
45        Path inputDir = new Path(otherArgs[0]);
46        if (otherArgs.length > 1) {
47            MaxLineNumber = Integer.parseInt(otherArgs[1]) > 0 ? Integer.parseInt(otherArgs[1]) : 10;
48        }
49
50        byte[] buffer = new byte[4096];
51        FSDataInputStream inputStream = hdfs.open(inputDir);
52
53        GzipCodec codec = new GzipCodec();
54        codec.setConf(conf);
55        CompressionInputStream input = codec.createInputStream(inputStream);
56
57        int lineNumber = 0;
58        String lastLine = null;
59        input.read(buffer);
60        while (lineNumber < MaxLineNumber && buffer != null) {
61            String[] out = new String(buffer, "UTF-8").split("\n");
62            for (int i = 0; i < out.length; i++) {
63                if (i == out.length - 1) {
64                    lastLine = out[i];
65                } else {
66                    if (i == 0 && lastLine != null) {
67                        System.out.println(lastLine + out[i]);
68                    } else {
69                        System.out.println(out[i]);
70                    }
71                    lineNumber++;
72                }
73
74                if (lineNumber >= MaxLineNumber) {
75                    break;
76                }
77            }
78            if (lineNumber < MaxLineNumber) {
79                input.read(buffer);
80            }
81        }
82
83        hdfs.close();
84    }
85}