SQL to JSON Data Modeling With Hackolade
Learn how IntegrIT's Hackolade can help you do SQL to JSON data modeling as well as generate static documentation about the model..
Join the DZone community and get the full member experience.Join For Free
sql to json data modeling is something i touched on in the first part of my moving from sql server to couchbase series . since that blog post, some new tooling has come to my attention from hackolade , who has recently added first-class couchbase support to their tool.
in this post, i’m going to review the very simple modeling exercise i did by hand and show how integrit’s hackolade can help.
i’m using the same sql schema that i used in the previous blog post series; you can find it on github (in the sqlserverdataaccess/scripts folder) .
review: sql to json data modeling
first, let’s review the main way to represent relations in a relational database is via a key/foreign key relationship between tables.
when looking at modeling in json, there are two main ways to represent relationships:
- referential : concepts are given their own documents, but reference other document(s) using document keys.
- denormalization : instead of splitting data between documents using keys, group the concepts into a single document.
i started with a relational model of shopping carts and social media users.
in my example, i said that a shopping cart-to-shopping cart items relationship in a relational database would probably be better represented in json by a single shopping cart document (which contains items). this is the “denormalization” path. then, i suggested that a social media user-to-social media user update relationship would be best represented in json with a referential relationship: updates live in their own documents, separate from the user.
this was an entirely manual process. for that simple example, it was not difficult. but with larger models, it would be helpful to have some tooling to assist in the sql to json data modeling. it won’t be completely automatic: there’s still some art to it, but the tooling can do a lot of the work for us.
starting with a sql server ddl
this next part assumes you’ve already run the sql scripts to create the five tables: shoppingcartitems, shoppingcart, friendbookusers, friendbookupdates, and friendbookusersfriends. (feel free to try this on your own databases, of course).
the first step is to create a ddl script of your schema. you can do this with sql server management studio.
first, right-click on the database you want. then, go to tasks then generate scripts . next, you will see a wizard. you can pretty much just click next on each step, but if you’ve never done this before you may want to read the instructions of each step so you understand what’s going on.
finally, you will have a sql file generated at the path you specified.
this will be a text file with a series of
statements in it (at least). here’s a brief excerpt of what i created (you can find the
full version on github
create table [dbo].[friendbookupdates]( [id] [uniqueidentifier] not null, [posteddate] [datetime] not null, [body] [nvarchar](256) not null, [userid] [uniqueidentifier] not null, constraint [pk_friendbookupdates] primary key clustered ( [id] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary] go -- etc...
by the way, this should also work with sql azure databases.
note: hackolade works with other types of ddls too, not just sql server, but also oracle and mysql.
this next part assumes that you have downloaded and installed hackolade. this feature is only available on the professional edition of hackolade, but there is a 30-day free trial available.
once you have a ddl file created, you can open hackolade.
in hackolade, you will be creating/editing models that correspond to json models: couchbase (of course) as well as dynamodb and mongodb. for this example, i’m going to create a new couchbase model.
at this point, you have a brand new model that contains a new bucket . you can use hackolade as a designing tool to visually represent the kinds of documents you are going to put in the bucket, the relationships to other documents, and so on.
we already have a relational model and a sql server ddl file, so let’s see what hackolade can do with it.
reverse engineer sql to json data modeling
in hackolade, go to
data definition language
file. you will be prompted to select a database type and a ddl file location. i’ll select
ms sql server
file from earlier. finally, i’ll hit
to let hackolade do its magic.
hackolade will process the five tables into five different kinds of documents. so, what you end up with is very much like a literal translation.
this diagram gives you a view of your model. but now you can think of it as a canvas to construct your ultimate json model. hackolade gives you some tools to help.
for instance, hackolade can make suggestions about denormalization when doing sql to json data modeling. go to tools > suggest denormalization . you’ll see a list of document kinds in table selection . try selecting shoppingcart and shoppingcartitems . then, in the parameters section, choose array in parent .
after you do this, you will see that the diagram looks different. now, the items are embedded into an array in shoppingcart , and there are dashed lines going to shoppingcartitems . at this point, we can remove shoppingcartitems from the model (in some cases you may want to leave it, that’s why hackolade doesn’t remove it automatically when doing sql to json data modeling).
notice that there are other options here too:
- embedding array in parent : this is what was demonstrated above.
- embedding sub-document in child : if you want to model the opposite way (e.g. store the shopping cart within the shopping cart item).
- embedding both : both array in parent and sub-document approach.
- two-way referencing : represent a many-to-many relationship. in relational tables, this is typically done with a junction table or mapping table.
also, note cascading . this is to prevent circular referencing where there can be a parent, child, grandchild, and so on. you select how far you want to cascade.
there are a couple of other things that i can do to clean up this model.
- add a ‘type’ field . in couchbase, we might need to distinguish shoppingcart documents from other documents. one way to do this is to add a discriminator field, usually called ‘type’ (but you can call it whatever you like). i can give it a default value in hackolade of shoppingcart .
- remove the ‘id’ field from the embedded array . the sql table needed this field for a foreign key relationship. since it’s all embedded into a single document, we no longer need this field.
- change the array name to ‘items’ . again, since a shopping cart is now consolidated into a single document, we don’t need to call it ‘shoppingcartitems’. just ‘items’ will do fine.
a model like this can be a living document that your team works on. hackolade models are themselves stored as json documents. you can share with team members, check them into source control, and so on.
you can also use hackolade to generate static documentation about the model. this documentation can then be used to guide the development and architecture of your application.
go to file > generate documentation > html/pdf . you can choose what components to include in your documentation.
Published at DZone with permission of Matthew Groves, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.