Package: relatable
relatablerelatableis a Python package for converting a collection of documents,
such as a MongoDB collection, into an interrelated set of tables, such as a
schema in a relational database.Installationpip3 install relatableExample of useIn this example we will walk through a use case of relatable for the sample dataset found in the repository of this
package in the data folder,data/example_input.json.Each document in this dataset has a complex structure with nested objects and lists.To generate a relational schema for this dataset, let's make an instance ofRelationalSchemawith the list of
documents as input:from relatable import RelationalSchema

import json

with open("data/example_input.json", "r") as fp:
    docs = json.load(fp)

rs = RelationalSchema(docs, "person")Once the RelationalSchema is instantiated, we can check its metadata. This metadata is a list of flat dictionaries, so
we can make use of Pandas to load it into a DataFrame:import pandas as pd

pd.DataFrame(rs.generate_metadata())tablecolumntypenullableunique0personperson.__id__numberFalseTrue1personnamestringFalseTrue2personagenumberFalseTrue3experienceexperience.__id__numberFalseTrue4experienceperson.__id__numberFalseFalse5experienceexperience.companystringFalseTrue6experienceexperience.rolestringFalseTrue7experienceexperience.fromnumberFalseTrue8experienceexperience.tonumberFalseFalse9experience.technologiesexperience.technologies.__id__numberFalseTrue10experience.technologiesexperience.__id__numberFalseFalse11experience.technologiesperson.__id__numberFalseFalse12experience.technologiesexperience.technologies.namestringFalseTrue13experience.technologiesexperience.technologies.primarybooleanFalseFalse14experience.responsibilitiesexperience.responsibilities.__id__numberFalseTrue15experience.responsibilitiesexperience.__id__numberFalseFalse16experience.responsibilitiesperson.__id__numberFalseFalse17experience.responsibilitiesexperience.responsibilities.namestringFalseTrueWe can see that RelationalSchema has inferred a relational schema consisting of four tables with primary keys and
foreign keys interrelating the tables.The relationships between the tables are the following:The tablepersonrepresents the main entity of the dataset, with a row for each person.The tableexperiencereferences the tableperson.The tablesexperience.technologiesandexperience.responsibilitiesreference the tableexperience, and
inherits the reference ofpersonfromexperience.Finally, let's look at each of the tables:dfs = [pd.DataFrame(t.data).set_index(t.primary_key) for t in rs.tables]Tableperson:person.__id__nameage0Alice341Bob27Tableexperience:experience.__id__person.__id__experience.companyexperience.roleexperience.fromexperience.to00GoogleSoftware Engineer2020202210FacebookSenior Data Scientist2017202021OpenAINLP Engineer20192022Tableexperience.technologies:experience.technologies.__id__experience.__id__person.__id__experience.technologies.nameexperience.technologies.primary000C++True100LolCodeFalse210PythonTrue310ExcelFalse421TritonTrue521LaTeXFalseTableexperience.responsibilities:experience.responsibilities.__id__experience.__id__person.__id__experience.responsibilities.name000Google stuff100Mark TensorFlow issues as "Won't Do"210Censor media310Learn the foundations of ML410Do Kaggle competitions521Assert that GPT-2 is racist621Assert that GPT-3 is racist721Develop a prototype of a premium non-racist language modelExample of use with the Airbnb MongoDB sample datasetAnother example of use with the AirbnbMongoDBsample dataset, downloadableherecan be
found in the repository of this package in the scriptexamples/airbnb_example.py