java代码模板

版本比较

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
 * 版本的比较
 * <p/>
 * User : jiaguo.tian
 * Date: 12-9-13
 * Time: 下午3:26
 */
public final class VersionUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(VersionUtil.class);
 
    /**
     * 比较版本
     * version1 < version2 返回 -1;
     * version1 =  version2 返回 0;
     * version1 > version2 返回 1;
     *
     * @param version1
     * @param version2
     * @return version1 < version2 返回 -1;
     *         version1 =  version2 返回 0;
     *         version1 > version2 返回 1;
     */
    public static final int compareVersion(String version1, String version2) {
        if (StringUtils.isEmpty(version1) || StringUtils.isEmpty(version2)) {
            LOGGER.info(String.format("version1 or version2 is null.version1:%s,version2:%s", version1, version2));
            return 0;
        }
        StringTokenizer tTokenizer = new StringTokenizer(version1, ".");
        StringTokenizer aTokenizer = new StringTokenizer(version2, ".");
        while (tTokenizer.hasMoreTokens() && aTokenizer.hasMoreTokens()) {
            String tToken = tTokenizer.nextToken();
            String aToken = aTokenizer.nextToken();
            int t = Integer.parseInt(tToken);
            int a = Integer.parseInt(aToken);
            if (t > a) {
                return 1;
            } else if (t < a) {
                return -1;
            }
        }
        return 0;
    }
 
    private VersionUtil() {/**/}
}

带命令行的cascading执行代码,使用develop1时执行本地cascading测试,develop2时执行的是本地hadoop测试,执行production时是线上跑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import cascading.flow.Flow;
import cascading.flow.FlowConnector;
import cascading.flow.FlowDef;
import cascading.flow.hadoop.HadoopFlowConnector;
import cascading.flow.local.LocalFlowConnector;
import cascading.pipe.Every;
import cascading.pipe.GroupBy;
import cascading.pipe.Pipe;
import cascading.pipe.assembly.Retain;
import cascading.property.AppProps;
import cascading.scheme.hadoop.TextDelimited;
import cascading.tap.SinkMode;
import cascading.tap.Tap;
import cascading.tap.hadoop.GlobHfs;
import cascading.tap.hadoop.Hfs;
import cascading.tap.local.FileTap;
import cascading.tuple.Fields;
import org.apache.commons.cli.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
 * User: jiaguotian
 * Date: 13-07-28
 * Time: 22:50
 */
public class CascadingTemplateApp {
    private static final Logger LOGGER = LoggerFactory.getLogger(BannerGetAdIntervalApp.class);
    private static final String MODE_PRODUCTION = "production";
    private static final String MODE_DEVELOP_V1 = "develop1";
    private static final String MODE_DEVELOP_V2 = "develop2";
    private static final String DEFAULT_CHARSET = "UTF-8";
    private static final String DELIMITER = "|";
 
    public static void main(String[] args) throws org.apache.commons.cli.ParseException, ParseException, IOException {
        Options options = initOptions();
        CommandLine cmd = parseCommandLine(options, args);
 
        String inputRootPath = null;
        String outputRootPath = null;
        String mode = cmd.getOptionValue(PARAM_MODE, MODE_PRODUCTION);
        if (cmd.hasOption(PARAM_INPUT_ROOT_PATH)) {
            inputRootPath = cmd.getOptionValue(PARAM_INPUT_ROOT_PATH).trim();
        } else {
            printHelpAndExit(options);
        }
        if (cmd.hasOption(PARAM_OUTPUT_ROOT_PATH)) {
            outputRootPath = cmd.getOptionValue(PARAM_OUTPUT_ROOT_PATH).trim();
        } else {
            printHelpAndExit(options);
        }
 
        Fields fields = Fields.size(36);
        fields = fields.rename(new Fields(3), new Fields(FIELD_3));
        fields = fields.rename(new Fields(1), new Fields(FIELD_1));
        fields = fields.rename(new Fields(5), new Fields(FIELD_5));
 
        Properties properties = new Properties();
        properties.put("mapred.map.tasks.speculative.execution", "false");
        properties.put("mapred.resuce.tasks.speculative.execution", "false");
        properties.put("mapreduce.job.complete.cancel.delegation.tokens", "false");
        // properties.put("mapred.child.java.opts", jvmOpts);
        // properties.put("mapred.reduce.tasks", reduceNum);
        // properties.put("mapred.compress.map.output", "true");
        // properties.put("mapred.map.output.compression.codec", GzipCodec.class.getName());
        // properties.put("mapred.output.compress", "true");
        // properties.put("mapred.output.compression.codec", GzipCodec.class.getName());
        AppProps.setApplicationJarClass(properties, CascadingTemplateApp.class);
        AppProps.setApplicationName(properties, CascadingTemplateApp.class.getName());
        // ***********************************************************************************************************************
        FlowConnector flowConnector;
        if (MODE_DEVELOP_V2.equalsIgnoreCase(mode)) {
            flowConnector = new HadoopFlowConnector(properties);
        } else if (MODE_DEVELOP_V1.equalsIgnoreCase(mode)) {
            flowConnector = new LocalFlowConnector(properties);
        } else {
            flowConnector = new HadoopFlowConnector(properties);
        }
        Tap inputTap;
        Tap outTap;
        if (MODE_DEVELOP_V2.equalsIgnoreCase(mode)) {
            String inputPath = inputRootPath + File.separator + "*.txt";
            String outputPath = outputRootPath + File.separator + "out";
            inputTap = new GlobHfs(new TextDelimited(fields, null, false, false, DELIMITER, false, null, null, true, DEFAULT_CHARSET), inputPath);
            outTap = new Hfs(new TextDelimited(true, DELIMITER), outputPath, SinkMode.REPLACE);
        } else if (MODE_DEVELOP_V1.equalsIgnoreCase(mode)) {
            String inputPath = inputRootPath;
            String outputPath = outputRootPath;
            inputTap = new FileTap(new cascading.scheme.local.TextDelimited(fields, false, false, DEFAULT_CHARSET, false, null, null, true, TextDelimited.DEFAULT_CHARSET), inputPath);
            outTap = new FileTap(new cascading.scheme.local.TextDelimited(true, DELIMITER), outputPath, SinkMode.REPLACE);
        } else {
            String inputPath = *****;
            String outputPath = *****;
            inputTap = new GlobHfs(new TextDelimited(fields, null, false, false, DELIMITER, false, null, null, true, TextDelimited.DEFAULT_CHARSET), inputPath);
            outTap = new Hfs(new TextDelimited(false, DELIMITER), outputPath, SinkMode.REPLACE);
        }
 
 
        FlowDef flowDef = FlowDef.flowDef();
        flowDef.setName(CascadingTemplateApp.class.getSimpleName());
 
        Pipe inputPipe = new Pipe("inputPipe");
        flowDef.addSource(inputPipe, inputTap);
        // *******************************************
        Pipe result = new Pipe("result", inputPipe);
        result = new Retain(result, new Fields(FIELD_3, FIELD_1, FIELD_5));
        // 其它处理
        // *******************************************
        flowDef.addTailSink(result, outTap);
        // ***********************************************************************************************************************
        Flow connect = flowConnector.connect(flowDef);
        connect.complete();
    }
 
    private static void printHelpAndExit(Options options) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(BannerGetAdIntervalApp.class.getName(), options);
        System.exit(0);
    }
 
    private static CommandLine parseCommandLine(Options options, String[] args) throws org.apache.commons.cli.ParseException {
        CommandLineParser parser = new PosixParser();
        CommandLine cmd = parser.parse(options, args);
        if (cmd.hasOption("h") || cmd.getOptions().length == 0) {
            printHelpAndExit(options);
        }
        return cmd;
    }
 
    private static Options initOptions() {
        Options options = new Options();
        // 必须
        options.addOption(OptionBuilder.withArgName(PARAM_INPUT_ROOT_PATH).withLongOpt(PARAM_INPUT_ROOT_PATH)
                .hasArg().withDescription("inputRootPath can not be null.[hdfs://xxxxxxxx:8020/xxxx/xxxxx]")
                .create("ir"));
        options.addOption(OptionBuilder.withArgName(PARAM_OUTPUT_ROOT_PATH).withLongOpt(PARAM_OUTPUT_ROOT_PATH)
                .hasArg().withDescription("outputRootPath can not be null.[hdfs://xxxxxxx:8020/xxxx/xxxxx/xxxxx] ")
                .create("or"));
        return options;
    }
}

命令行参数执行程序

maven库

1
2
3
4
5
<dependency>
    <groupId>commons-cli</groupId>
    <artifactId>commons-cli</artifactId>
    <version>1.2</version>
</dependency>

java程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public static void main(String[] args) throws org.apache.commons.cli.ParseException, ParseException, IOException {
    Options options = initOptions();
    CommandLine cmd = parseCommandLine(options, args);
 
    // 解析参数
    Date day = null;
    String compress = null;
    if (cmd.hasOption(PARAM_DAY)) {
        day = new SimpleDateFormat("yyyyMMdd").parse(cmd.getOptionValue(PARAM_DAY).trim());
    } else {
        printHelpAndExit(options);
    }
    if (cmd.hasOption(PARAM_COMPRESS)) {
        compress = cmd.getOptionValue(PARAM_COMPRESS).trim();
    } else {
        compress = Constants.GZIP;
    }
 
    // 正常的业务逻辑
 
}
 
private static Options initOptions() {
    Options options = new Options();
    // 必须
    options.addOption(OptionBuilder.withArgName(PARAM_DAY).withLongOpt(PARAM_DAY)
            .hasArg().withDescription("day can not be null.[yyyyMMdd]")
            .create("d"));
    options.addOption(OptionBuilder.withArgName(PARAM_COMPRESS).withLongOpt(PARAM_COMPRESS)
            .hasArg().withDescription("compress default is GZIP")
            .create("c"));
    return options;
}
 
private static CommandLine parseCommandLine(Options options, String[] args) throws org.apache.commons.cli.ParseException {
    CommandLineParser parser = new PosixParser();
    CommandLine cmd = parser.parse(options, args);
    if (cmd.hasOption("h") || cmd.getOptions().length == 0) {
        printHelpAndExit(options);
    }
    return cmd;
}
 
private static void printHelpAndExit(Options options) {
    HelpFormatter formatter = new HelpFormatter();
    formatter.printHelp(BannerFraudDetectionApp.class.getName(), options);
    System.exit(0);
}

Leave a Reply

Your email address will not be published. Required fields are marked *