简体   繁体   中英

Syntax error insert in Apache Beam code

I am writing Apache beam code for testing. Please refer the code below. I created sample SimpleFunction and apply code and trying to compile.

    public static void main(String[] args) throws IOException {
        System.out.println("Test Log");

        PipelineOptions options = PipelineOptionsFactory.create();
        //options.setRunner(SparkRunner.class);
        options.setRunner(SparkRunner.class);

        Pipeline p = Pipeline.create(options);

        p.apply(FileIO.match().filepattern("hdfs://path/to/*.gz"))
            // withCompression can be omitted - by default compression is detected from the filename.
            .apply(FileIO.readMatches())
            .apply(MapElements
                // uses imports from TypeDescriptors
                .via(
                    new SimpleFunction <ReadableFile, KV<String,String>>() {

                        private static final long serialVersionUID = -7867677L;

                        @SuppressWarnings("unused")
                        public KV<String,String> createKV(ReadableFile f) {
                            String temp = null;
                            try{
                            temp = f.readFullyAsUTF8String();
                            }catch(IOException e){

                            }
                            return KV.of(f.getMetadata().resourceId().toString(), temp);
                        }

                        @Override
                        public String apply(ReadableFile element, KV<String, String> input) {
                            StringBuilder str = new StringBuilder();
                            return str.toString();
                        }
                    }

            ))
            .apply(FileIO.write());
        p.run();
    }

but compiler is throwing syntax error at public String apply(ReadableFile

I tried but not successful to fix this, Could someone guide me?

SimpleFunction<InputT, OutputT> takes a value of InputT and returns the value of OutputT . The signature of apply in this case is OutputT apply(InputT input); , see here .

In your case for your types the SimpleFunction will have to look like this:

new SimpleFunction <ReadableFile, KV<String,String>>() {
   ...
   @Override
   public KV<String,String> apply(ReadableFile input) {
      ...
   }
}

For example see how it is used here .

In your case you need more logic around readMatches() , see here for example how it is applied to parse Avros, and this is the implementation details of the PTransform from that code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM