2017-08-29 91 views
0

我正在尝试编写源文件存储在GCS中的文本文件。代码运行良好,但不是一个XML文件,而是生成多个XML文件。 (XML文件的数量似乎遵循源文本文件中存在的记录总数)。我在使用'DataflowRunner'时观察了这种情况。通过Apache Beam写入XML时生成多个文件

当我在本地运行相同的代码,然后生成两个文件。第一个包含所有具有适当元素的记录,第二个仅包含打开和关闭根元素。

有关这种意外行为发生的任何想法?请在下面找到我使用的代码片段:

PCollection<String>input_records=p.apply(TextIO.read().from("gs://balajee_test/xml_source.txt")); 

    PCollection<XMLFormatter> input_object= input_records.apply(ParDo.of(new DoFn<String,XMLFormatter>(){ 
     @ProcessElement 

     public void processElement(ProcessContext c) 
     { 
      String elements[]=c.element().toString().split(","); 

      c.output(new XMLFormatter(elements[0],elements[1],elements[2],elements[3],elements[4])); 

      System.out.println("Values to be written have been provided to constructor "); 

     } 
    })).setCoder(AvroCoder.of(XMLFormatter.class)); 

    input_object.apply(XmlIO.<XMLFormatter>write() 
       .withRecordClass(XMLFormatter.class) 
       .withRootElement("library") 
       .to("gs://balajee_test/book_output")); 

请让我知道在输出端一个XML文件(book_output.xml)的方式。

回答

0

XmlIO.write().to()被记录如下:

/** 
* Writes to files with the given path prefix. 
* 
* <p>Output files will have the name {@literal {filenamePrefix}-0000i-of-0000n.xml} where n is 
* the number of output bundles. 
*/ 

即预计它可能产生多个文件:例如如果跑步者选择将你的数据处理成3个任务(“捆绑”),你会得到3个文件。在某些情况下,某些部分可能会变空,但写入的总数据总是与预期数据相加。

要求IO只生成一个文件是一个合理的请求,如果你的数据不是特别大。它在TextIO和AvroIO中通过.withoutSharding()得到支持,但尚未在XmlIO中受支持。请随意file a JIRA与功能要求。

+0

https://issues.apache.org/jira/browse/BEAM-2826 –

+0

提交相同的JIRA请求。 –