简体   繁体   English

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

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

I want to read a file in UCS2-LE BOM using TextIO, however It doesn't seem to work.我想使用 TextIO 读取 UCS2-LE BOM 中的文件,但是它似乎不起作用。 Is there a way to use TextIO with this encoding ?有没有办法在这种编码中使用 TextIO ? Or is there another library that does well with this type of encoding ?或者是否有另一个库可以很好地处理这种类型的编码?

My code is in JAVA (Apache Beam)我的代码是 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);}}));

I found a solution, in a medium post : Solution我在一篇中等文章中找到了一个解决方案: 解决方案

The file I am reading is stored in GCS, hence the added lines in try part (compared to the original code.)我正在阅读的文件存储在 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;
                            }));

PS: I didn't add a line with credentials because I passed it into IntelliJ parameters. PS:我没有添加带有凭据的行,因为我将其传递给 IntelliJ 参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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