简体   繁体   中英

Reading an xml file in apache beam using XmlIo

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();
}
}

my pojo class:


@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+"]";
    }

}

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

can somebody guide me on this.

is there any example i can refer into....?

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));

and XML file should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
        <name>Harrypotter</name>
    </book>
</books>

Just adding runnable version from question for pipeline noobs, like myself, when using newer versions of beam-sdks-java-core (2.31.0)

import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.io.xml.XmlIO;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.transforms.DoFn;
import org.apache.beam.sdk.transforms.ParDo;

public class BookStore {
    public static void main(String[] args) {

        var pipeline = Pipeline.create(PipelineOptionsFactory.create());

        pipeline
                .apply(XmlIO.<Book>read().from("sample.xml")
                        .withRootElement("books")
                        .withRecordElement("book")
                        .withRecordClass(Book.class))
                .apply(ParDo.of(new DoFn<Book, String>() {

                    @ProcessElement
                    public void processElement(
                            @Element Book element, OutputReceiver<String> receiver) {

                        System.out.println("Xml data: " + element.getName());
                        receiver.output(element.getName());
                    }
                }));

        pipeline.run();
    }
}

Alternative solution:

import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.io.xml.XmlIO;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.transforms.MapElements;
import org.apache.beam.sdk.values.TypeDescriptors;

public class BookStore {
    public static void main(String[] args) {

        var pipeline = Pipeline.create(PipelineOptionsFactory.create());

        pipeline
                .apply(XmlIO.<Book>read().from("sample.xml")
                        .withRootElement("books")
                        .withRecordElement("book")
                        .withRecordClass(Book.class))
                .apply(MapElements.into(TypeDescriptors.strings())
                        .via((Book book) -> {
                            System.out.println("Xml data: " + book.getName());
                            return book.getName();
                        }));

        pipeline.run();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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