无法读取存储在 GCS 存储桶中的 XML 文件 [英] Unable to read XML File stored in GCS Bucket

查看:42
本文介绍了无法读取存储在 GCS 存储桶中的 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试以最准确的方式遵循此文档:

package com.bitwise.cloud;导入 javax.xml.bind.annotation.XmlElement;导入 javax.xml.bind.annotation.XmlRootElement;导入 javax.xml.bind.annotation.XmlType;@XmlRootElement(name = "book")@XmlType(propOrder = {"author", "title","genre","price","description"})公共类 XMLFormatter {私人字符串作者;私人字符串标题;私人弦乐类型;私人字符串价格;私人字符串描述;公共 XMLFormatter() { }public XMLFormatter(字符串作者,字符串标题,字符串类型,字符串价格,字符串描述){this.author = 作者;this.title = 标题;this.genre = 流派;this.price = 价格;this.description = 描述;}@XmlElement公共无效setAuthor(字符串作者){this.author = 作者;}公共字符串 getAuthor() {返回作者;}@XmlElement公共无效设置标题(字符串标题){this.title = 标题;}公共字符串 getTitle() {返回标题;}@XmlElement公共无效setGenre(字符串流派){this.genre = 流派;}公共字符串 getGenre() {回归流派;}@XmlElement公共无效setPrice(字符串价格){this.price = 价格;}公共字符串 getPrice() {退货价格;}@XmlElementpublic void setDescription(字符串描述){this.description = 描述;}公共字符串 getDescription() {退货说明;}}

解决方案

XmlIO.Read PTransform 不支持提供多个记录元素(作者、标题、流派等).您必须提供一个根元素和一个记录元素,并且您的 XML 文档必须包含具有相同记录元素的记录.请参阅以下位置中给出的示例.

https://github.com/apache/beam/blob/master/sdks/java/io/xml/src/main/java/org/apache/beam/sdk/io/xml/XmlIO.java#L59

I have tried to follow this documentation in the most precise way I could:

https://beam.apache.org/documentation/sdks/javadoc/2.0.0/org/apache/beam/sdk/io/xml/XmlIO.html

Please find below my codes :

public static void main(String args[])
{

    DataflowPipelineOptions options=PipelineOptionsFactory.as(DataflowPipelineOptions.class);
     options.setTempLocation("gs://balajee_test/stagging");
     options.setProject("test-1-130106");

     Pipeline p=Pipeline.create(options);

     PCollection<XMLFormatter> record= p.apply(XmlIO.<XMLFormatter>read()
             .from("gs://balajee_test/sample_3.xml")
             .withRootElement("book")
             .withRecordElement("author")
             .withRecordElement("title")
             .withRecordElement("genre")
             .withRecordElement("price")
             .withRecordElement("description")
             .withRecordClass(XMLFormatter.class)
             );

     record.apply(ParDo.of(new DoFn<XMLFormatter,String>(){
                @ProcessElement

                public void processElement(ProcessContext c)
                {
                    System.out.println(c.element().getAuthor());    
                }
             }));

     p.run(); 
}   

I'm getting 'null' value for every XML component. Could you please review my code and suggest me the corrective course of action required?

package com.bitwise.cloud;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "book")
@XmlType(propOrder = {"author", "title","genre","price","description"})
public class XMLFormatter {
private String author;
private String title;
private String genre;
private String price;
private String description;

public XMLFormatter() { }

public XMLFormatter(String author, String title,String genre,String price,String description) {
this.author = author;
this.title = title;
this.genre = genre;
this.price = price;
this.description = description;
}

@XmlElement
public void setAuthor(String author) {
this.author = author;
}

public String getAuthor() {
return author;
}

@XmlElement
public void setTitle(String title) {
this.title = title;
}

public String getTitle() {
return title;
}

@XmlElement
public void setGenre(String genre) {
this.genre = genre;
}

public String getGenre() {
return genre;
}

@XmlElement
public void setPrice(String price) {
this.price = price;
}

public String getPrice() {
return price;
}


@XmlElement
public void setDescription(String description) {
this.description = description;
}

public String getDescription() {
return description;
}
}

解决方案

XmlIO.Read PTransform doesn't support providing multiple record elements (author, title, genre, etc). You have to provide a single root element and a record element and your XML document has to contain records that have the same record element. See the example given in the following location.

https://github.com/apache/beam/blob/master/sdks/java/io/xml/src/main/java/org/apache/beam/sdk/io/xml/XmlIO.java#L59

这篇关于无法读取存储在 GCS 存储桶中的 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆