Python sax to lxml for 80+GB XML

后端 未结 3 1935
后悔当初
后悔当初 2020-12-04 22:43

How would you read an XML file using sax and convert it to a lxml etree.iterparse element?

To provide an overview of the problem, I have built an XML ingestion tool

3条回答
  •  囚心锁ツ
    2020-12-04 23:28

    iterparse is an iterative parser. It will emit Element objects and events and incrementally build the entire Element tree as it parses, so eventually it will have the whole tree in memory.

    However, it is easy to have a bounded memory behavior: delete elements you don't need anymore as you parse them.

    The typical "giant xml" workload is a single root element with a large number of child elements which represent records. I assume this is the kind of XML structure you are working with?

    Usually it is enough to use clear() to empty out the element you are processing. Your memory usage will grow a little but it's not very much. If you have a really huge file, then even the empty Element objects will consume too much and in this case you must also delete previously-seen Element objects. Note that you cannot safely delete the current element. The lxml.etree.iterparse documentation describes this technique.

    In this case, you will process a record every time a is found, then you will delete all previous record elements.

    Below is an example using an infinitely-long XML document. It will print the process's memory usage as it parses. Note that the memory usage is stable and does not continue growing.

    from lxml import etree
    import resource
    
    class InfiniteXML (object):
        def __init__(self):
            self._root = True
        def read(self, len=None):
            if self._root:
                self._root=False
                return "\n"
            else:
                return """\n\ttext value\n\n"""
    
    def parse(fp):
        context = etree.iterparse(fp, events=('end',))
        for action, elem in context:
            if elem.tag=='record':
                # processing goes here
                pass
    
            #memory usage
            print resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    
            # cleanup
            # first empty children from current element
                # This is not absolutely necessary if you are also deleting siblings,
                # but it will allow you to free memory earlier.
            elem.clear()
            # second, delete previous siblings (records)
            while elem.getprevious() is not None:
                del elem.getparent()[0]
            # make sure you have no references to Element objects outside the loop
    
    parse(InfiniteXML())
    

提交回复
热议问题