Java(类似于SQL)和Shell脚本运行2个参数时出现问题

时间:2019-04-23 08:37:26

标签: java shell hadoop

我有一个Java类Filter2,并且我想使用shell脚本run2.sh运行它。问题是我不知道如何在shell脚本中输入2个参数($ 1和$ 2)。

public class Filter2 {

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        conf.set("limit", otherArgs[0]);
    conf.set("limit2", otherArgs[1]);//added here

    Job job = new Job(conf, "Distributed Filter");
    job.setJarByClass(Filter2.class);
    job.setMapperClass(FilterMapper.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    job.setNumReduceTasks(0); // Set number of reducers to zero
    FileInputFormat.addInputPath(job, new Path(args[1]));
    FileOutputFormat.setOutputPath(job, new Path(args[2]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

 public static class FilterMapper
     extends Mapper<Object, Text, Text, IntWritable>{

     private final static IntWritable counter = new IntWritable(0);
     private Text word = new Text();
     private Integer total;

     private Integer limit;
     private Integer limit2;//added here
     public void map(Object key, Text value, Context context
             ) throws IOException, InterruptedException {
     StringTokenizer itr = new StringTokenizer(value.toString());

     limit = Integer.parseInt( context.getConfiguration().get("limit") );
     limit2 = Integer.parseInt( context.getConfiguration().get("limit2") );

     while (itr.hasMoreTokens()) {
         word.set(itr.nextToken());
             total = Integer.parseInt(itr.nextToken());//added here

         if ((total > limit)&&(total<limit2))//added here    
         { counter.set( total );
           context.write(word, counter); }
     }
     }
 }

}

run2.sh

$HADOOP_HOME/bin/hadoop jar Filter2.jar Filter2 $1 sales.txt /user/solution2/

我想在终端“ ./run.sh 45 50”中使用shell脚本运行Java,但是我无法输入2个输入参数。如何修改我的Shell脚本以启用此结果?

1 个答案:

答案 0 :(得分:0)

如果您要向脚本传递多个参数,则变量名只会增加。

#/bin/bash
echo "arg1: $1"
echo "arg2: $2"
$HADOOP_HOME/bin/hadoop jar Filter2.jar Filter2 $1 $2 sales.txt /user/solution2/

如果您使用以下命令调用此脚本

./run.sh 45 50

您将会看到

arg1: 45
arg2: 50
在控制台上

(加上您的hadoop命令执行的所有操作)。注意hadoop行中的$2。这是第二个脚本参数。将其移到需要的位置。