-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add data to neo4j database in docker compose
- Loading branch information
Showing
5 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
ARG HOSTNAME=localhost | ||
FROM python:3.11.3-bullseye AS builder | ||
RUN pip install poetry==1.8.2 | ||
|
||
ENV POETRY_NO_INTERACTION=1 \ | ||
POETRY_VIRTUALENVS_IN_PROJECT=1 \ | ||
POETRY_VIRTUALENVS_CREATE=1 \ | ||
POETRY_CACHE_DIR=/tmp/poetry_cache | ||
|
||
WORKDIR /app | ||
COPY backend/pyproject.toml backend/poetry.lock ./ | ||
COPY backend/src/backend ./src/backend | ||
RUN touch README.md | ||
|
||
RUN poetry install --without dev && rm -rf $POETRY_CACHE_DIR | ||
|
||
FROM python:3.11-slim-buster AS runtime | ||
ARG HOSTNAME | ||
ENV HOSTNAME=${HOSTNAME} | ||
ENV VIRTUAL_ENV=/app/.venv \ | ||
PATH="/app/.venv/bin:$PATH" | ||
COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV} | ||
WORKDIR /app | ||
RUN apt-get update | ||
RUN apt install zip -y | ||
COPY --from=builder /app/src ./src | ||
|
||
ENTRYPOINT [ "/bin/bash", "-c", "echo Hello World" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
49 changes: 49 additions & 0 deletions
49
src/containers/docker/neo4j/backend/src/backend/__main__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import os | ||
|
||
from loguru import logger | ||
from neo4j import GraphDatabase | ||
|
||
HOSTNAME = os.getenv("HOSTNAME", "localhost") | ||
logger.info(HOSTNAME) | ||
|
||
URI = f"neo4j://{HOSTNAME}:7687" | ||
AUTH = ("neo4j", "password") | ||
|
||
with GraphDatabase.driver(URI, auth=AUTH) as driver: | ||
logger.info(driver.verify_connectivity()) | ||
create_cmds = [ | ||
"CREATE CONSTRAINT products IF NOT EXISTS FOR (p:Product) REQUIRE p.name IS UNIQUE", | ||
"CREATE CONSTRAINT keywords IF NOT EXISTS FOR (k:Keyword) REQUIRE k.name IS UNIQUE", | ||
"CREATE CONSTRAINT dosage_forms IF NOT EXISTS FOR (d:Dosage_Form) REQUIRE d.name IS UNIQUE", | ||
"CREATE CONSTRAINT moieties IF NOT EXISTS FOR (m:Moiety) REQUIRE m.name IS UNIQUE" | ||
] | ||
query = ''' | ||
UNWIND $rows AS row | ||
MERGE (p:Product {name: row}) | ||
RETURN row | ||
''' | ||
print(driver.verify_connectivity()) | ||
with driver.session(database="neo4j") as session: | ||
for cmd in create_cmds: | ||
resp = list(session.run(cmd, None)) | ||
print(resp) | ||
|
||
resp = list(session.run(query, parameters={'rows': ["d"]})) | ||
|
||
query = ''' | ||
UNWIND $row.table_keywords AS keyword | ||
MERGE (k:Keyword {name: keyword}) | ||
WITH k | ||
MATCH (p:Product {name: $row.product}) | ||
MERGE (p)-[:CONTAINS]->(k) | ||
RETURN k.name AS name | ||
''' | ||
with driver.session(database="neo4j") as session: | ||
for record in [ | ||
{ | ||
"table_keywords": ["a", "c", "b"], | ||
"product": "d", | ||
} | ||
]: | ||
resp = list(session.run(query, parameters={'row': record})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters