{"id":19710783,"url":"https://github.com/vemonet/stardog-demo","last_synced_at":"2026-05-15T19:32:46.916Z","repository":{"id":48182528,"uuid":"516732154","full_name":"vemonet/stardog-demo","owner":"vemonet","description":"🐕‍🦺💫 Use a Stardog triplestore to create Virtual Knowledge Graphs","archived":false,"fork":false,"pushed_at":"2023-10-06T16:35:18.000Z","size":225,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-04-02T08:23:23.139Z","etag":null,"topics":["postgresql","sparql","sql","stardog","triplestore","virtual-knowledge-graph"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vemonet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-07-22T11:53:40.000Z","updated_at":"2025-08-06T04:21:59.000Z","dependencies_parsed_at":"2025-02-27T16:26:10.376Z","dependency_job_id":"eda6ba3a-e517-4973-b803-ef9b06c60fd8","html_url":"https://github.com/vemonet/stardog-demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/vemonet/stardog-demo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vemonet%2Fstardog-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vemonet%2Fstardog-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vemonet%2Fstardog-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vemonet%2Fstardog-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vemonet","download_url":"https://codeload.github.com/vemonet/stardog-demo/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vemonet%2Fstardog-demo/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33076188,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-15T11:35:32.926Z","status":"ssl_error","status_checked_at":"2026-05-15T11:35:31.362Z","response_time":103,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["postgresql","sparql","sql","stardog","triplestore","virtual-knowledge-graph"],"created_at":"2024-11-11T22:08:25.917Z","updated_at":"2026-05-15T19:32:46.900Z","avatar_url":"https://github.com/vemonet.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🐕‍🦺💫 Create a Virtual Knowledge Graph with Stardog\n\nRepository to demo how to create a Virtual Knowledge Graph in a Stardog triplestore using data from a PostgreSQL database.\n\nFor this demo we use the **MIMIC-IV dataset**, more details and request access at https://physionet.org/content/mimiciv/2.2/\n\n## ➡️ Access IDS Stardog\n\n1. Go to **https://cloud.stardog.com**\n\n2. Connect with your Google account (or any other option)\n3. Create a **New Connection** for the Stardog server deployed at IDS\n   1. Provide the username and password you were given by the IDS Stardog admin (Vincent probably)\n   2. And the IDS Stardog server endpoint URL: **https://stardog.137.120.31.102.nip.io**\n4. You can now connect to IDS server, create database, create model, etc\n\nIn the future you will just need to reconnect to https://cloud.stardog.com with your Google account, and access IDS Stardog server from there (it will save your connections credentials)\n\nStardog proposes 3 main interfaces to manage your knowledge graphs:\n\n* **Studio** to query and navigate your KG\n* **Designer** to define models\n* **Explorer** to do full text searches\n\n---\n\n## ⚛️ Create a Virtual Knowledge Graph\n\nTo federate multiple SQL databases\n\n### 🔌 Create the data sources in Stardog Studio\n\nGo to the [**Data** tab](https://cloud.stardog.com/u/1/studio/#/data) in **Stardog Studio**, and click the **+** button to add a data source.\n\n**Add PostgreSQL database sources for cohort 1 and 2:**\n\n1. Data Source Type: PostgreSQL\n\n2. JDBC Connection URL (use `postgres-mimic-iv-2` for cohort 2):\n\n   ```\n   jdbc:postgresql://postgres-mimic-iv:5432/mimic_iv\n   ```\n\n3. JDBC username is `postgres`, and the password is the one you defined (or `passwordtochange` if you kept the default)\n4. Driver Class: keep `org.postgresql.Driver`\n\n**Add MariaDB database source for cohort 2:**\n\nAlternatively you could also use MariaDB instead of PostgreSQL for cohort 2:\n\n1. Data Source Type: MariaDB\n\n2. JDBC Connection URL:\n\n   ```\n   jdbc:mariadb://mariadb-mimic-iv:3306/mimic_iv\n   ```\n\n3. JDBC username is `root`, and the password is the one you defined (or `passwordtochange` if you kept the default),\n\n4. Driver Class: ⚠️ change to `org.mariadb.jdbc.Driver`\n\n\u003e ℹ️ Build scripts are available to load MIMIC-IV in various DBMS: https://github.com/MIT-LCP/mimic-code/tree/main/mimic-iv/buildmimic\n\n---\n\n### 🧶 Create the model\n\nGo to the [**Models** tab](https://cloud.stardog.com/u/1/studio/#/models) in **Stardog Studio**.\n\nAdd classes with their properties from the [**OMOP Common Data Model**](https://github.com/OHDSI/CommonDataModel/blob/main/inst/csv/), e.g. Patient, Death\n\nThrough this interface you can browse the model through a tree view, and edit the model ontology as turtle RDF, making it easier if you need to import an existing ontology.\n\nValidation that the data complies with the model can be set using SHACL: https://docs.stardog.com/data-quality-constraints\n\n\u003cdetails\u003e\u003csummary\u003eModel creation and mapping can also be done through the \u003cb\u003eStardog Designer\u003c/b\u003e interface, it offers limited customization of the mappings and model, but can be helpful to pre-generate mappings that are then improved manually in Stardog Studio\u003c/summary\u003e\n\nTo create a new model and mappings manually:\n\n* Create classes and properties of the model\n\n* Create a **new project resource** \u003e New Virtual Graph \u003e PostgreSQL\n\n  * Select the `patients` table if the option is available,\n\n  * Otherwise provide the following custom SQL query to retrieve the patients table:\n\n    ```sql\n    SELECT * FROM patients\n    ```\n\n* Provide a **name for the resource**, such as `cohort1`,and click create\n\n* On the canvas click the newly created resource, and click **Add mapping** to map it to the **Patient** model\n\n* In the mapping interface connect the 3 properties of our Patient to the right columns in the SQL table.\n\nFinally publish your model to the database of your choice in Stardog\n\n\u003c/details\u003e\n\n---\n\n### 🗺️ Define the mappings\n\nIn the [**Virtual Graphs** tab](https://cloud.stardog.com/u/1/studio/#/virtual-graphs) in **Stardog Studio**\n\nMappings in Stardog is done using the Stardog Mapping Syntax (SMS).\n\nHere we provide an example of mappings from a `patients.csv` file to a Person, and it's Death, if recorded.\n\nMapping from patients to the Person class, converting the gender from M/F to 0/1 to comply with the OMOP CDM:\n\n```SPARQL\n# Map patients to persons\nPREFIX omop-cdm: \u003ctag:stardog:designer:omop-cdm:model:\u003e\nMAPPING\nFROM SQL {\n  SELECT *, (CASE \"gender\"\n    WHEN 'M' THEN '0'\n    WHEN 'F' THEN '1'\n  END) AS gender_id FROM patients\n}\nTO {\n  ?Person_iri a omop-cdm:Person ;\n    omop-cdm:year_of_birth ?anchor_year_integer_field ;\n    omop-cdm:gender_concept_id ?gender_integer_field ;\n    omop-cdm:id ?subject_id_integer_field .\n}\nWHERE {\n  BIND(TEMPLATE(\"tag:stardog:designer:omop-cdm:data:Person:{subject_id}\") AS ?Person_iri)\n  BIND(StrDt(?anchor_year, \u003chttp://www.w3.org/2001/XMLSchema#integer\u003e) AS ?anchor_year_integer_field)\n  BIND(StrDt(?gender_id, \u003chttp://www.w3.org/2001/XMLSchema#integer\u003e) AS ?gender_integer_field)\n  BIND(StrDt(?subject_id, \u003chttp://www.w3.org/2001/XMLSchema#integer\u003e) AS ?subject_id_integer_field)\n}\n```\n\nMapping from patients to the Death class, we only create Death entities when a `dod` is present:\n\n```SPARQL\n# Map patients to deaths\nPREFIX omop-cdm: \u003ctag:stardog:designer:omop-cdm:model:\u003e\nMAPPING\nFROM SQL {\n  SELECT * FROM patients WHERE dod IS NOT NULL\n}\nTO {\n  ?Death_iri a omop-cdm:Death ;\n    omop-cdm:death_date ?dod_date_field .\n\n  ?Death_iri omop-cdm:person_id ?Person_iri .\n}\nWHERE {\n  BIND(TEMPLATE(\"tag:stardog:designer:omop-cdm:data:Person:{subject_id}\") AS ?Person_iri)\n  BIND(TEMPLATE(\"tag:stardog:designer:omop-cdm:data:Death:{subject_id}\") AS ?Death_iri)\n  BIND(StrDt(?dod, \u003chttp://www.w3.org/2001/XMLSchema#date\u003e) AS ?dod_date_field)\n}\n```\n\n---\n\n### 💬 Query the virtual graphs in Stardog Studio\n\nGo to the [**Workspace** tab](https://cloud.stardog.com/u/1/studio/#/) in **Stardog Studio**\n\nOr directly query the SPARQL endpoint at https://stardog.137.120.31.102.nip.io/icare4cvd\n\n**Query all virtual graphs with SPARQL**:\n\n```sparql\nSELECT *\nFROM stardog:context:virtual\nWHERE {\n    ?s ?p ?o .\n} LIMIT 10000\n```\n\n\u003e You can also use `stardog:context:all` to query all materialized and virtual graphs.\n\n**Query a specific virtual graph** using its name:\n\n```sparql\nSELECT *\nWHERE {\n  GRAPH \u003cvirtual://virtual_graph_name\u003e {\n    ?s ?p ?o .\n  }\n} LIMIT 10000\n```\n\n**Get all persons**:\n\n```SPARQL\nSELECT DISTINCT ?id ?gender ?year_of_birth ?death_date\nFROM stardog:context:virtual\nWHERE {\n    ?s a omop-cdm:Person ;\n        omop-cdm:id ?id ;\n        omop-cdm:gender_concept_id ?gender ;\n        omop-cdm:year_of_birth ?year_of_birth .\n    OPTIONAL {\n        ?death omop-cdm:person_id ?s ;\n               omop-cdm:death_date ?death_date\n    }\n\n} LIMIT 1000000\n```\n\n**Get persons with no death date**:\n\n```SPARQL\nSELECT DISTINCT ?id ?gender ?year_of_birth\nFROM stardog:context:virtual\nWHERE {\n    ?s a omop-cdm:Person ;\n        omop-cdm:id ?id ;\n        omop-cdm:gender_concept_id ?gender ;\n        omop-cdm:year_of_birth ?year_of_birth .\n    FILTER NOT EXISTS {?death omop-cdm:person_id ?s}\n\n} LIMIT 1000000\n```\n\n**Get how many years the patients stayed in hospital before dying**:\n\n```SPARQL\nSELECT DISTINCT ?id ?gender ?year_of_birth ?death_date (?year_of_death - ?year_of_birth AS ?age_of_death)\nFROM stardog:context:virtual\nWHERE {\n    ?s a omop-cdm:Person ;\n        omop-cdm:id ?id ;\n        omop-cdm:gender_concept_id ?gender ;\n        omop-cdm:year_of_birth ?year_of_birth .\n        ?death omop-cdm:person_id ?s ;\n               omop-cdm:death_date ?death_date\n   BIND(xsd:integer(STRBEFORE(str(?death_date), \"-\")) AS ?year_of_death)\n\n} LIMIT 1000000\n```\n\n\u003e ⚠️ `omop-cdm:year_of_birth` is not the year of birth, but the year of admission at the hospital (to be fixed)\n\n**Get persons born after a specific date**:\n\n```SPARQL\nSELECT DISTINCT *\nFROM stardog:context:virtual\nWHERE {\n    ?s a omop-cdm:Person ;\n        omop-cdm:year_of_birth ?birthYear .\n    FILTER (?birthYear \u003e 2130)\n} LIMIT 10000\n```\n\n\u003e See the [Stardog introduction to SPARQL](https://docs.stardog.com/getting-started-series/getting-started-1) if you need to.\n\n---\n\n## ℹ️ Additional infos\n\n### 🧞 Generate SQL schema for CSV files\n\nInstall dependencies:\n\n```bash\npython3 -m venv .venv\nsource .venv/bin/activate\npip install csvkit mysql-connector-python\n```\n\nGenerate schema from CSV. Note it needs to be manually fixed as they don't add `(128)` after VARCHAR\n\n```bash\ncsvsql --db mysql://user:password@localhost:3306/heart-failure-db --insert stroke-prediction-cohort1.csv\n```\n\n### 🔒️ Change the Stardog admin password\n\nFix the password, cf. https://docs.stardog.com/stardog-admin-cli-reference/user/user-passwd\n\n```bash\ndocker-compose exec stardog stardog-admin user passwd --username admin admin\n```\n\n### 🗺️ Convert SMS mappings to R2RML\n\nTo run in the Stardog docker container:\n\n```bash\ndocker-compose exec stardog stardog-admin virtual mappings -f r2rml virtualgraph\n```\n\n### 🔩 Create a VKG with Apache Drill\n\n\u003e TODO\n\n```sql\nSELECT COLUMNS[0] AS id, COLUMNS[1] AS age FROM dfs.`/data/stroke-prediction-cohort1.csv` LIMIT 3\n```\n\n### 🔗 Links\n\nThe Stardog documentation is quite consequent, please look into it when you want to do something: **https://docs.stardog.com**\n\n* Docs to easily load CSV/JSON through the UI: https://docs.stardog.com/virtual-graphs/importing-json-csv-files\n\n* Docs to access the SPARQL, HTTP, GRAPHQL APIs: https://stardog-union.github.io/http-docs/\n\nCommunity forum: https://community.stardog.com\n\nExample docker-compose for cluster: https://github.com/stardog-union/pystardog/blob/develop/docker-compose.cluster.yml\n\n---\n\n## 🚀 Deploy the stack\n\nRequirements: docker 🐳, and you will need to get your **Stardog license** at https://www.stardog.com/license-request ⚠️\n\nDeploys a local Stardog triplestore, a PostgreSQL database, and a MariaDB SQL database to create a Virtual Knowledge Graph (VKG).\n\nPlace the `stardog-license-key.bin` file in the root folder of this repository.\n\nDownload the JDBC drivers in the `drivers/` folder by running this script:\n\n```bash\n./prepare.sh\n```\n\n\u003e Optionally create a `.env` file with the password for the SQL database, otherwise the default is `passwordtochange`:\n\u003e\n\u003e ```bash\n\u003e echo \"PASSWORD=yourpassword\" \u003e .env\n\u003e ```\n\nStart Stardog and postgreSQL:\n\n```bash\ndocker-compose up -d\n```\n\n\u003e ℹ️ The PostgreSQL database will be automatically initialized using the schema and data in `virtual-kg/`\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvemonet%2Fstardog-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvemonet%2Fstardog-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvemonet%2Fstardog-demo/lists"}