Skip to content

Commit

Permalink
add data to neo4j database in docker compose
Browse files Browse the repository at this point in the history
  • Loading branch information
zcemycl committed Jul 31, 2024
1 parent 1104008 commit 69f3e29
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/containers/docker/neo4j/backend/Dockerfile.etl
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" ]
2 changes: 1 addition & 1 deletion src/containers/docker/neo4j/backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
description = ""
authors = ["zcemycl <lyc010197@gmail.com>"]
readme = "README.md"
package-mode = false
packages = [{ include = "backend", from = "src" }]

[tool.poetry.dependencies]
python = ">=3.11,<4.0"
Expand Down
Empty file.
49 changes: 49 additions & 0 deletions src/containers/docker/neo4j/backend/src/backend/__main__.py
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}))
24 changes: 22 additions & 2 deletions src/containers/docker/neo4j/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
version: '3.10'

networks:
net:
driver: bridge
services:
neo4j:
image: neo4j:latest
Expand All @@ -15,3 +16,22 @@ services:
ports:
- "7474:7474"
- "7687:7687"
networks:
- net
add-data:
build:
context: .
dockerfile: backend/Dockerfile.etl
args:
- HOSTNAME=neo4j
entrypoint: python -m backend
depends_on:
- neo4j
networks:
- net
deploy:
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 5
window: 10s

0 comments on commit 69f3e29

Please sign in to comment.