Java - Apache Beam:使用 "UCS2-LE BOM" 编码从 GCS 读取文件

Java - Apache Beam: Read file from GCS with "UCS2-LE BOM" encoding

我想使用 TextIO 读取 UCS2-LE BOM 中的文件,但是它似乎不起作用。 有没有办法将 TextIO 与这种编码一起使用?或者是否有另一个库可以很好地处理这种类型的编码?

我的代码在 JAVA (Apache Beam)

PCollection<KV<String, String>> csvElements =
            pipeline.apply("Reads the input csv file", TextIO
                    .read()
                    .from(options.getPolledFile()))
                    .apply("Read File", ParDo.of(new DoFn<String, KV<String,String>>(){
                        @ProcessElement
                        public void processElement(ProcessContext c) throws UnsupportedEncodingException {
                            String element = c.element();

                            String elStr = new String(element.getBytes(),"UTF-16LE");
                            c.output(elStr);}}));

我找到了一个解决方案,在介质 post 中:Solution

我正在阅读的文件存储在 GCS 中,因此在 try 部分添加了行(与原始代码相比。)

file = "path to gas file";    
PCollection<String> readCollection = pipeline.apply(FileIO.match().filepattern(file))
                    .apply(FileIO.readMatches())
                    .apply(FlatMapElements
                            .into(strings())
                            .via((FileIO.ReadableFile f) -> {
                                List<String> result = new ArrayList<>();
                                try {
                                    ReadableByteChannel byteChannelParse = f.open();
                                    InputStream inputStream = Channels.newInputStream(byteChannelParse);
                                    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-16"));
                                    String line = br.readLine();
                                    while (line != null) {
                                        result.add(line);
                                        line = br.readLine();
                                    }
                                    br.close();
                                    inputStream.close();
                                }

                                catch (IOException e) {
                                    throw new RuntimeException("Error while reading", e);
                                }
                                return result;
                            }));

P.S:我没有添加带有凭据的行,因为我将它传递给了 IntelliJ 参数。