java(类似sql)和shell脚本问题,无法运行2个参数

ar7v8xwq  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(342)

我有一个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); }
     }
     }
 }

}

运行2.sh

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

我想在终端“./run.sh4550”中用shell脚本运行java,但我不能输入2个参数。如何修改shell脚本以启用此结果?

gfttwv5a

gfttwv5a1#

如果要向脚本传递多个参数,则变量的名称将递增。


# /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命令所做的一切)。注意 $2 在hadoop系列中。这是第二个脚本参数。把它移到需要的地方。

相关问题