如何在使用bazel构建时传递参数

xsuvu9jc  于 2021-06-25  发布在  Flink
关注(0)|答案(1)|浏览(874)

如何在执行bazel构建时传递参数。实际上,我的程序将接受两个参数。一个是directory,另一个是target.csv。我需要获取目录下的文件并将其写入csv。

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collector;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.tuple.Tuple1;
import org.apache.flink.api.java.utils.ParameterTool;

public class ReadFiles {

  /**
   * @param args
   */
  public static void main(String[] args) throws Exception {

    // set up the execution environment
    try {
      final ParameterTool params = ParameterTool.fromArgs(args);
      final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

      env.setParallelism(1); // without this multiple files creating
      env.getConfig().setGlobalJobParameters(params);

      List<String> paths = new ArrayList<String>();
      File dir = new File(params.getRequired("input"));
      for (File f : dir.listFiles()) {
        if (f.isFile()) {
          paths.add(f.getName());
        }
      }
      DataSet<String> data = env.fromCollection(paths).rebalance();
      DataSet<Tuple1<String>> output = data.flatMap(new CSVSplitter());
      env.execute();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
// The operations are defined by specialized classes, here the Splitter class.

class CSVSplitter implements FlatMapFunction<String, Tuple1<String>> {

  public void flatMap(String value, Collector<Tuple1<String>> out) throws Exception {
    out.collect(new Tuple1<String>(value));
  }

}

以及我正在尝试的构建bazel的语法-

bazel build FlinkEx/com/practice:read_files

执行此操作后遇到问题。

Worker process sent response with exit code: 1.
error: wrong number of type arguments; required 3

我需要传递一些类似bazel build flinkex/com的参数/practice:read_files c:/c:/target.csv文件

ngynwnxp

ngynwnxp1#

你的意思是 bazel run ? 您可以通过使用 -- :

bazel run FlinkEx/com/practice:read_files -- c:/ c:/target.csv

或者可以直接运行二进制文件并“正常”传递参数:

bazel build FlinkEx/com/practice:read_files
bazel-bin/FlinkEx/com/practice:read_files c:/ c:/target.csv

(这是假设 bazel build 正在放置 read_filesbazel-bin . 如果在下面 bazel-genfiles ,只需从那里运行即可。)

相关问题