使用XmlIo在Apache Beam中读取xml文件 [英] Reading an xml file in apache beam using XmlIo
问题描述
问题陈述:
i正在尝试使用直接运行器
在Beam中读取和打印xml文件的内容,这是代码段:
problem statement: i am trying to read and print contents of an xml file in beam using direct runner here is the code snippet:
public class BookStore{
public static void main (string args[]){
BookOptions options = PipelineOptionsFactory.fromArgs(args).withValidation().as(BookOptions .class);
Pipeline pipeline = Pipeline.create(options);
PCollection<Book> output = pipeline.apply(XmlIO.<Book>read().from("sample.xml")
.withRootElement("book")
.withRecordElement("name")
.withRecordClass(Book.class));
output.apply(ParDo.of(new DoFn<Book,String>(){
@ProcessElement
public void processElement(ProcessContext c)
{
System.out.println("xml data "+c.element().getname());
}
}));
pipeline.run();
}
}
我的pojo类:
@XmlRootElement(name = "book")
@XmlType(propOrder = {"name"})
public class Book{
private String name;
@XmlElement(name = "name")
public String getName ()
{
return name;
}
public void setName (String name)
{
this.name = name;
}
@Override
public String toString()
{
return "ClassPojo [name= "+name+"]";
}
}
我的sample.xml文件
my sample.xml file
<?xml version="1.0" encoding="UTF-8"?>
<book>
<name>Harrypotter</name>
</book>
当我使用直接运行程序执行上述代码时,我得到的名称输出为空
when i execute the above code using direct runner i am getting output of "name" as null
有人可以指导我吗?
有没有我可以参考的例子……?
is there any example i can refer into....?
推荐答案
您的XML文件与您在其中定义的 XmlIO
选项不对应您的管道-您需要具有一个包含记录(书)的根元素。解决方案之一可能是这样的:
Your XML file doesn't correspond to XmlIO
options that you define in your pipeline - you need to have a root element that includes your records (books). One of the solutions could be something like this:
PCollection<Book> output = pipeline.apply(
XmlIO.<Book>read().from("sample.xml")
.withRootElement("books")
.withRecordElement("book")
.withRecordClass(Book.class));
,XML文件应如下所示:
and XML file should look like this:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<name>Harrypotter</name>
</book>
</books>
这篇关于使用XmlIo在Apache Beam中读取xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!