Graphing Citizen Data With ETL
How to set up an effective data modeling plan with graphs.
Join the DZone community and get the full member experience.Join For Free
in this post i want to talk about the challenges we faced during our graph journey and how we overcame them. the outline of this post is as follows.
- why do we need etl and sync from sql to a graph
- benefits of taking citizen data into a graph
- challenges in data modeling, export and import
- change data capture
- our approach
why do we need etl and sync from sql to a graphthe digital universe is doubling in size every two years and expected to reach 44 zettabytes by 2020 .
with such a vast amount of data, the patterns and connections in data become as important as the data itself. to be able to discover and use those patterns and connections for business purposes, information technology veterans increasingly use graph databases . although they are reluctant to alter their online transaction processing systems, they can’t resist the appeal created by the power of graph computing, especially due to the fact that neo4j has reduced previously complex and scary graph science to such simple and smart solutions (yeah cypher rocks!).
we have been dealing with census data for more than 15 years. it was 2-3 years ago when we hit the graph database concept and neo4j, while we were looking for possible solutions for some big data analysis requirements. in fact we had spent some time on key-value and column-family databases till one day we realized that we were trying to construct a graph using other nosql databases. after that, it did not take long to come upon the native graph solution: neo4j.
the benefits of taking citizen data into a graphshortly after initial poc practices, it became quite clear that representing citizen data as a graph database was the perfect solution for the following scenarios among many others:
- providing comprehensive and online real-time ancestral trees for citizens
- determining heirs and calculating heritage shares
- identifying old/helpless citizens without any relatives living nearby
- constructing domestic / international migration routes, investigating causes and discussing consequences
for instance, it would require four join operations on tables with more than 100 million rows to reach a close relative such as a cousin of a person, which would require a lot more effort for the developer and consume a lot more system resources compared to neo4j. after realizing that great potential in graph databases, it was no longer possible for us to even consider a citizen database not making use of a graph database.
if you already have a small or well-designed sql data, it is relatively easy to construct a property-graph model and make the initial data load. however, defining the graph data model and etl process for our data, which was first designed 25 years ago according to legislative regulations that dates back to 19th century and have been subject to frequent updates, had its own story.
first let’s look at some of the challenges in data modeling.
challenges in data modeling
- mutual relations will be an early subject of discussion especially if humans are a part of your conceptual domain. we had initially designed the spouse relationship as mutual. so there were two spouse relationships, one from wife to husband and one from husband to wife. although that looked good on the screen at first, it became a real drawback when we began to perform analysis depending on human relations. they caused circular relations misleading our analysis. we ended up just creating one relation for each couple. you may also define two different relationship types like wife and husband if your queries require so, but we would advise you to think carefully about it.
birthdate info is represented with three separate nullable fields in census database because of old records which don’t have exact birthdate information. in addition, it is a valuable information that we often use as a criteria in queries. so we designed it as a “time series tree” such that
(day)-[day_of]->(month)-[month_of]->[year]starting from year 1000. a
personnode can be connected to a
daynode if it has a complete birthdate information, connected to a
monthnode if day information is missing and connected to a
yearnode if the year of birth is all we know.
- another problem arose when we began expanding person nodes after loading birthdate data. there are thousands of people born on the same day causing dense nodes that can possibly slow down traversals across these nodes (depending on queries). as discussed in the learning neo4j (by rik van bruggen , packt publishing) book you can apply a fan-out strategy for that. we have to stress that you should maintain fan-out levels (meta nodes) as data changes to not introduce new dense nodes.
- you can’t always extract relationships directly from existing explicit or implicit table relations. one example is the marriages that ended with a divorce, which we want to define as “exspouse”. unfortunately you have to check out the marriage registrations table for marriage records, eliminate current wife and then distinguish widows as well. this is an extraordinary mapping from sql to graph that you have to manage in both initial data load and data synchronization.
challenges in exporting and loading data
time spent to export your relational data:
when you have billions of records for numerous tables, it takes really long to extract that data. we managed to shorten it by parallelizing the export process. it is possible to do it in several ways:
- if you have such a table, possibly it is already stored in partitions. so make use of it and export partitions in parallel.
- another good practice is to divide into ranges by primary keys or unique indexes.
- if none of the above are applicable, dividing by non-unique indexes would also do work.
time spent to import:
you successfully exported your massive data, and it is now time to import. depending on your data, it can also take long to import it in with the
load csvcommand. if you are making an initial load, using “batch import” instead of
load csvwould save a lot of your time. it is also possible to use them in conjunction; let the batch import do the heavy work and load csv to handle the fine work.
- character encoding: during both export and import you need to check the tools used, exported files and the environment for regional settings. it is very disheartening to start over when you realize such a problem at the end.
- special characters: you may have to replace them during export like replacing ” (double quotes) with ‘ (single quote). text fields may also include separator characters that you will have to take care of.
null values in source data:
if you are using
load csv, the following statement will make cypher ignore attributes with values:
set n.fieldx = case trim(row.fieldx) when "" then null else row.fieldx end
for batch import you can simply use the
- absence of unique identifiers: for batch import you will have to provide a unique id for each node.
keeping it up to data (change data capture)keeping your rdbms and graph database synchronized is challenging. i will remark just some important matters here.
change data capture (cdc) mechanisms heavily depend on your relational database product. below are possible solutions applicable to popular rdbms.
- a common solution is to create triggers to track data manipulations in tables you are interested in. those triggers may populate a single delta table with necessary information such as table name, change type, changed data, timestamp, etc. you have to manage transactions and preserve order of transactions to guarantee the integrity of both sql and graph.
- if you are not good with triggers and are ready to make some investment, it is a better choice to use a cdc solution (oracle goldengate, sql server integration services, ibm infosphere change data capture, etc.) that is compatible with your rdbms product.
- whatever cdc mechanism you choose, it is a good practice to establish a uniform cdc data structure to use as the input of the component that carries out data transformation and load.
apart from the cdc mechanism, handling data changes may be tricky depending on your data structure. here are some examples:
- nodes that do not originate directly from a record are a consideration. for instance, in census data there is not a particular table for a family record. it is implicitly represented by family ids assigned to citizens. so a new family is introduced to the database when a foreigner becomes a citizen, and a new family id is assigned. we have to create a new family node to add that person to the graph.
- the birth date issue mentioned above is also a challenge in cdc. you have to employ a mechanism to find the exact meta node to connect and also create a new one if it reaches the threshold you determined.
- another interesting case is when your source data has a versioned structure. your solution depends on whether you want versioned data in the graph as well or want to aggregate them to a single node. in our case we needed to aggregate versions to a single node. so for every insertion or update we analyze whether it is really an insert, an update or a delete and act accordingly.
our approachobviously, there are a number of issues to consider for employing a graph database while using a relational database as the master data source. we concluded that building a generic etl solution that is specialized in transforming and continuously synchronizing rdbms data to a graph database would be the perfect solution to those issues.
our design approach is to define the etl process as a declaration of a transformation model that is independent of its execution, rather than defining business processes.
the system consists of the following components:
a visual design tool that enables you to:
- design a graph data model
- define the transformation model from sql to graph
- capture those models as a dsl
an etl execution engine that:
- executes initial load
executes the etl model
- provided as dsl
- developed using the api provided
- continuously synchronizes data from sql to graph
here is the high-level diagram for our etl solution:
if you want to learn more about our etl solution please join us graphconnect europe. we will be giving a lightning talk at 12:10 p.m. that takes a deeper dive into our etl solution.
i would like to thank my colleagues can, bilgesu, başar, volkan and our v.p. ali for their devotion to our product and precious contribution to this post.
Published at DZone with permission of İrfan Nuri Karaca, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.