Java-ApacheBeam:从GCS读取文件时使用;UCS2-LE物料清单“;编码
我想使用TextIO读取UCS2-LE BOM中的文件,但它似乎不起作用。 有没有一种方法可以将TextIO与这种编码结合使用?或者是否有另一个库可以很好地使用这种编码 我的代码是JAVA(Apache Beam)Java-ApacheBeam:从GCS读取文件时使用;UCS2-LE物料清单“;编码,java,encoding,google-cloud-platform,google-cloud-dataflow,apache-beam,Java,Encoding,Google Cloud Platform,Google Cloud Dataflow,Apache Beam,我想使用TextIO读取UCS2-LE BOM中的文件,但它似乎不起作用。 有没有一种方法可以将TextIO与这种编码结合使用?或者是否有另一个库可以很好地使用这种编码 我的代码是JAVA(Apache Beam) PCollection csvElements= apply(“读取输入csv文件”),TextIO .读() .from(options.getPolledFile())) .apply(“读取文件”,ParDo.of(new DoFn(){ @过程元素 public void p
PCollection csvElements=
apply(“读取输入csv文件”),TextIO
.读()
.from(options.getPolledFile()))
.apply(“读取文件”,ParDo.of(new DoFn(){
@过程元素
public void processElement(ProcessContext c)引发UnsupportedEncodingException{
字符串元素=c.element();
String elStr=新字符串(element.getBytes(),“UTF-16LE”);
c、 输出(elStr);}}});
我在一篇中篇文章中找到了一个解决方案:
我正在读取的文件存储在GCS中,因此try部分中添加了行(与原始代码相比)
file=“气体文件的路径”;
PCollection readCollection=pipeline.apply(FileIO.match().filepattern(文件))
.apply(FileIO.readMatches())
.应用(FlatMapElements)
.into(字符串())
.via((FileIO.ReadableFile f)->{
列表结果=新建ArrayList();
试一试{
ReadableByteChannel ByTechAnneParse=f.open();
InputStream InputStream=Channels.newInputStream(byteChannelParse);
BufferedReader br=新的BufferedReader(新的InputStreamReader(inputStream,“UTF-16”));
String line=br.readLine();
while(行!=null){
结果。添加(行);
line=br.readLine();
}
br.close();
inputStream.close();
}
捕获(IOE异常){
抛出新的RuntimeException(“读取时出错”,e);
}
返回结果;
}));
p.S:我没有添加带有凭据的行,因为我将其传递到IntelliJ参数中。Hi@Oumab10!请你分享一下错误信息好吗?我没有发现任何限制matter@MethkalKhalawi我没有得到一个错误,但是当我打印元素时,编码不正确,我会得到奇怪的字符。
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);}}));
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;
}));