XML Databases, anyone?

Status
Not open for further replies.

Apteris

Ars Tribunus Angusticlavius
9,591
Subscriptor
I'm wondering if anyone here has experience working with a native XML database, and at the same time I'm looking for a sanity check on my idea. There are many threads like this, but this one is mine.

One of my tasks at work is to convert a set of M XML files into a set of N CSV files. We get the M XML files from a component not under our control, we have to output N CSV files to another component not (very much) under our control. The mapping from source to output is not 1-to-1; its full complexity is yet to be determined.

So, not a difficult task, but still one that requires consideration. My first thought was pretty much the same as this one:

[url=http:/civis/viewtopic.php?p=23678412#p23678412:2innxfoo said:
Ravensroke[/url]":2innxfoo]1. Make a database with tables that sort of match your xml structure.
2. Parse XML into java objects
3. Bulk load data into database with the database framework of your choice (plain JDBC, JDBC templates, Hibernate)
4. Use sql queries to query your data
5. Profit.
But that's a lot of work. It's error prone and verbose. And why would I manually create a new format to store data that already has an implied format, namely its XML structure? Crazy talk.

My second thought was to just run a SAX parser over the XML files, parse XML elements one at a time and write to output as appropriate. But it turns out that would be inconvenient too, because in writing out the CSVs I have to sometimes reference parent or child elements. I may even have to reference other XML files. So I would in essence be manually creating an in-memory model of my data, that I'd then be writing to disk.

And so after a bit of Googling, it dawned on me: I already have a data format. I just need to store it efficiently and query it. Enter the native XML databases, and XQuery. The top three Google hits, apart from the Wikipedia entry, are BaseX, eXist-db and Oracle XML DB.

I started with BaseX and have been pretty pleased with the results so far. I fed it 100 MB of data and it parsed it and indexed it in no time flat. All my queries return in less than one second. (Tomorrow I'll be testing it with some larger files to see how it copes.) Most importantly, I can now effortlessly run queries of the form

Code:
for $thing in /collection//thing return ($thing/@ID, $thing/text(), $thing/../@parentID)

A thing of beauty. It should work well, after all, this is what XQuery was designed for. But while the idea seems good--this is where you fine folk yell at me if I'm wrong--the implementation fills me with concern. BaseX is apparently actively developed by just one person, and while I'm sure they're competent and have nothing but good intentions, it's still just one person. Its native bindings to Java are a joke. That would be OK, since technically I should be using the XQuery API for Java anyway. Except, the XQJ API for BaseX is also developed by what appears to be a small team, at http://xqj.net/. What do you know, they also develop the XQJ API for eXist-db and Sedna; guess I have no choice but to trust them.

Over on the other side of the fence, eXist-db are apparently "preparing the 2.0 release" and so can't be bothered with small things like non-broken links to their JavaDoc. Meanwhile, Oracle go into the opposite direction and provide 100 pages of documentation on how to run a simple XQuery. I guess too much is better than too little; of course, one needs to ask, is such a heavyweight solution even necessary?

So. Anyone know which of these possible solutions is the best? Any recommendations?
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
Oracle, even in express edition, is probably super overkill unless your datasets are massive. On top of that, you have all the ugly licensing issues to deal with in production. So it would not be my first choice at all. It does have tons of functionality though.

If you don't need to persist the data between invocations, then processing the files in Python using lxml or ElementTree may not be a terrible approach. It would at least reduces the effort of building an in-memory model compared to Java, but it obviously depends on your data.
 
XML doesn't get indexed, you can't query it with SQL. You're trying to fit a round peg into a square hole. Do it right, use the right tools for the job (RDBMS) and you'll make your life easier. Parsing well defined xml documents and loading them into an RDBMS is incredibly well documented and understood (and robust when done properly). Exporting the data from a database is incredibly easy as well because you have possibly the best tool in the world (SQL) to query your data and aggregate it into the desired form. Editing is easy because again, you can use SQL to do it.

I just don't understand your resistance to parsing some XML and writing a few lines of SQL. XML is a great tool for exchanging data between applications. Its pretty horrible at actually working with the data once you have it. (Which is why you want to parse it into usable form).

Side note: I have to deal with importing/exporting XML all day long, and using XML as a live data store for an application is the real crazy talk here.
 

Joel_B

Ars Praefectus
4,652
Subscriptor
Apteris":1v2qxiwz said:
[url=http:/civis/viewtopic.php?p=23678412#p23678412:1v2qxiwz said:
Ravensroke[/url]":1v2qxiwz]1. Make a database with tables that sort of match your xml structure.
2. Parse XML into java objects
3. Bulk load data into database with the database framework of your choice (plain JDBC, JDBC templates, Hibernate)
4. Use sql queries to query your data
5. Profit.
But that's a lot of work. It's error prone and verbose. And why would I manually create a new format to store data that already has an implied format, namely its XML structure? Crazy talk.

This is the easiest way to do it. Seriously. It's what everyone else does.
 

Apteris

Ars Tribunus Angusticlavius
9,591
Subscriptor
Ravensroke":1pngtfd2 said:
XML doesn't get indexed
It does if you use an XML database.

you can't query it with SQL.
But I can query it with XQuery.

I just don't understand your resistance to parsing some XML and writing a few lines of SQL. XML is a great tool for exchanging data between applications. Its pretty horrible at actually working with the data once you have it. (Which is why you want to parse it into usable form).
Because I shouldn't need to. The data is already in usable form, I just need a way to use it, and I have found one. In fact, without a clear need for an SQL database, I'd argue that it's converting to a relational DB that is the actual fitting of a square peg into a round hole.

Side note: I have to deal with importing/exporting XML all day long, and using XML as a live data store for an application is the real crazy talk here.
I hope you didn't take my OP the wrong way. I quoted you mostly to reference the other thread, and I wasn't saying the idea was crazy talk in general, merely in my case. I'm not using XML as a live data store, it's very much an intermediary format; it just so happens I need to process it a bit.
 

Joel_B

Ars Praefectus
4,652
Subscriptor
Apteris":34neuxrj said:
A thing of beauty. It should work well, after all, this is what XQuery was designed for. But while the idea seems good--this is where you fine folk yell at me if I'm wrong--the implementation fills me with concern. BaseX is apparently actively developed by just one person, and while I'm sure they're competent and have nothing but good intentions, it's still just one person. Its native bindings to Java are a joke. That would be OK, since technically I should be using the XQuery API for Java anyway. Except, the XQJ API for BaseX is also developed by what appears to be a small team, at http://xqj.net/. What do you know, they also develop the XQJ API for eXist-db and Sedna; guess I have no choice but to trust them.

Over on the other side of the fence, eXist-db are apparently "preparing the 2.0 release" and so can't be bothered with small things like non-broken links to their JavaDoc. Meanwhile, Oracle go into the opposite direction and provide 100 pages of documentation on how to run a simple XQuery. I guess too much is better than too little; of course, one needs to ask, is such a heavyweight solution even necessary?

So. Anyone know which of these possible solutions is the best? Any recommendations?

I looked at XML Databases for a project a while ago. The names you mention ring a bell. The main problem is that you still need to conver the results to something that people can use in a UI. Except doing it with an XML Database means that you need to do it at the UI level. Then you have problems if someone up-stream changes the XML Spec. Then you have to change the UI to account for that.

But then you also have the added problem that some XML will be in the old spec and some in the new spec so you have to have both bits of the UI code available.

It's much, much, much easier to parse everything to your own format and stick it in a database.
 

Joel_B

Ars Praefectus
4,652
Subscriptor
Apteris":3ip0hhda said:
If someone upstream changes the XML structure of their outputs, I expect I would be forced to adapt whichever solution I choose, be it XML or relational.

It's unlikely anyone will need a GUI for this data, though I do acknowledge your concern.

Ok, I thought at some point you mentioned a GUI for this. My mistake. Still think its easier to stick in a db.
 
D

Deleted member 43669

Guest
I think someone here mentioned HyperJAXB. It's JAXB + Hibernate.

If it were done right, it might be possible to autogenerate a reasonable SQL schema from an .xsd and persist XML to that schema effortlessly.

Haven't tried it, though :p

About using XML databases, if this is not critical and you have time on your side, why not? Once upon a time I was very interested in XQuery, it certainly looked great, but it seems to have vanished from the face of Earth.
 
I once was involved in a project that magically evolved into what you propose doing: running queries against the XML files and outputting new files. I frantically tried to stop it and instead create an intermediate database to store the information in and then build new files from that, but I didn't succeed.

It is with no doubt the most reeking pile of shit I have even managed to create. I sincerely believe that those who took over after I left want to (with all rights!) hire goons and take "care" of me.

Seriously, don't do it.
 

Pontiphex

Ars Legatus Legionis
13,182
Subscriptor
SQL Server has a native XML column type. You can even define it with a schema and do indexing.

I've used that many times as the first step of a data manipulation pipeline that starts with data in XML:

  • Stuff data into SQL Server table with XML column.
  • Use XQuery to pull out the critical data needed to make the decision for which processing step to take next.
  • Processing handler takes XML data and does something, potentially querying additional XML data if related.
 
Apteris":2pw0w49j said:
Could you go in a bit more detail into why the idea didn't work out?
A combination of things. A big issue was speed. The lookups and transformation of data against the XML files ended up taking a vast amount of time. IO was a big factor since the files were pretty big. It also ended up being hard to grasp and maintain the code due to lots and lots of different XML files being read and written. Now, my case had a bunch of muenchian grouping and other things needed to be done which was written in xslt, which of course added to the complexity.

If I were to do it again, I'd dump as much data as possible into an SQL database and do queries against that. Especially if the overall layout were the same as that time (a "base" XML file with up to twenty other files needed for lookup values and other things).
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
What database were you using? How big were the files?

However, this:
Janx Spirit":1ghbey9p said:
Especially if the overall layout were the same as that time (a "base" XML file with up to twenty other files needed for lookup values and other things).
Implied the need for some sort of normalization originally, at which point an XML database is probably no longer a good fit, for good or ill.
 

Apteris

Ars Tribunus Angusticlavius
9,591
Subscriptor
For the record. I tested performance with a big file (1 million elements, 350MB file size) and it (the performance) was good. Around 20 seconds to add the file to the database and index it, and seconds-range queries on the data. Which, seeing as it's an intermediary format and not a live system, is quite good. But ultimately my supervisor decided against the solution, going with a plain-ol' SQL database. Which is just as well, since the maturity of the XML APIs didn't feel me with hope and joy, and this

LordHunter317":29nmbnpp said:
the need for some sort of normalization originally, at which point an XML database is probably no longer a good fit, for good or ill
was damn-near prophetic. Some normalization may indeed be necessary.
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
Do note that creating a hybrid is possible with the XML support in most relational databases. You can store fragments of XML in columns, which may be useful for some applications. All RDBMS have some level of support, but I believe Oracle is still the hands-down winner in terms of features, and possibly performance. It obviously depends on what you're doing, though.

XML databases are great if you want to query inside of one document. They're less useful if you're trying to query across documents (some full-text features aside).
 

fitten

Ars Legatus Legionis
55,635
Subscriptor++
Pontiphex":3fjsxgo1 said:
SQL Server has a native XML column type. You can even define it with a schema and do indexing.

I've used that many times as the first step of a data manipulation pipeline that starts with data in XML:

  • Stuff data into SQL Server table with XML column.
  • Use XQuery to pull out the critical data needed to make the decision for which processing step to take next.
  • Processing handler takes XML data and does something, potentially querying additional XML data if related.

Yes, but it can be slow (particularly on inserts). I worked on data manipulation pipelines that used XML as well. We ended up having a few issues that we needed to solve... mostly because of performance and the number of records being processed. Cardinalities of how records map to sub-pieces/parts is important, but with our problem(s), we actually ended up with a hybrid system where we 'promoted' certain XML data into proper columns (actually, all this was done as an optimization per custom pipeline that we wrote) for better/faster queries. We kept the original XML fragment in the record as well and could query directly into it as well. The query code in the product took incoming queries (both read and writes) which were expressed in XML queries and used promoted columns in preference to the XML queries when promoted columns were available (also writing/updating the promoted column values on inserts/updates). Obviously, this was specific to the problems we were solving... having a fragment that could satisfy a query with multiple results wouldn't work with what we did, although the ideas could be extended to make a system that could do so. To be faster, we even had the concept of 'dirty' records where we'd modify the promoted columns and mark the record as dirty so that if a query read the XML column, the promoted values would overwrite the values in the XML fragment inside the data access code but before being returned to the caller. The next UPDATE of the record would resolve the dirty and the XML and promoted values would then be consistent.

This type system may be overkill for the OP, though.
 
Status
Not open for further replies.