Skip to content

article save and query using elasticsearch #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 1 addition & 128 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,129 +1,2 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
.idea

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
91 changes: 91 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,93 @@
# python-elasticsearch-with-kafka
Python Restful service with elasticsearch and kafka integration

### 1. Server setup
1.1 Download and install Java 1.8 or higher
1.2 Download and install [Kafka Server](https://kafka.apache.org/downloads)
1.3 Download and install [Elasticsearch Server](https://www.elastic.co/downloads/elasticsearch)

### 2. Client setup
2.1 Download and install [Python](https://www.python.org/downloads/)

2.2 Create and activate a python virtual environment
> py -m venv eskafkavenv # can use any name in place of eskafkavenv

2.3 Activate the created virtual environment
> eskafkavenv\Scripts\activate

#### 3. Install python packages
3.1 pip install kafka-python
3.2 pip install urllib3
3.3 pip install certifi
3.4 pip install elasticsearch
Alternatively we can download the tar.gz package from the Download page and run command
> py -m pip install .\<python-package-name>.tar.gz # replace the <python-package-name>

## 4. Start Kafka server
4.1 Start Zookeeper node instance
> linux$ bin/zookeeper-server-start.sh config/zookeeper.properties
> windows$ bin\windows\zookeeper-server-start.bat config\zookeeper.properties

4.2 Start Kafka server
> linux$ bin/kafka-server-start.sh config/server.properties
> windows$ bin\windows\kafka-server-start.bat config\server.properties

4.3 Once kafka service is up create the kafka topic
> bin/kafka-topic.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic kafka-message-topic

4.4 consuming test message
> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic kafka-message-topic --from-beginning

4.5 produce test message
> bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic kafka-message-topic
> Hello Listeners

## 5. Start Elasticsearch server
Elasticsearch is a distributed, real-time, search analysis platform.
Elasticsearch can store data in json format, and hence can be used as NoSQL database.
> bin/elasticsearch.bat

- index: An index is equivalent to database in relational database
- mapping: A mapping is equivalent to schema in relational database

## 6 Elasticsearch REST APIs
6.1 Check elasticsearch is running
> http://localhost:9200

6.2 Index APIs
6.2.1 Create an index with name articles
> PUT http://localhost:9200/articles

6.2.2 Query an index with name articles
> GET http://localhost:9200/articles

6.2.3 Delete an index with name articles
> DELETE http://localhost:9200/articles

6.2.4 Update Index with proper mappings
> PUT http://localhost:9200/articles
{
"mappings": {
"dynamic": "strict",
"properties": {
"author": {"type": "text"},
"title": {"type": "text"},
"publish_date": {"type": "date"}
}
}
}

6.3 Document APIs
6.3.1 Add a document to index. Adding id at the end is optional while saving document
> POST http://localhost:9200/articles/_doc/1
{"author": "Sushil", "title": "Small intro to Elasticsearch using Python", "publish_date": "2021-11-14"}

6.3.2 Get a document from index by id
> GET http://localhost:9200/articles/_doc/1

6.3.3 Get all documents from index
> GET http://localhost:9200/articles/_doc/_search

6.3.4 Delete a document
> DELETE http://localhost:9200/_doc/1

3 changes: 3 additions & 0 deletions article-search/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
*.iml

9 changes: 9 additions & 0 deletions article-search/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## article-search service

- This is a demo application to save, and search the article information using elasticsearch.
- kafka is used to send error message in case of exception as an alarm.
- article-service is made restful using FastAPI and uvicorn.
- Read the [README](https://github.com/smallintro/python-elasticsearch-with-kafka/README) file to know how to set up the environment to run and test this application.
- Run the main.py file to start the service.
- Access the rest service at [127.0.0.1:8080/docs](http://127.0.0.1:8080/docs)

130 changes: 130 additions & 0 deletions article-search/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
*.iml

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
9 changes: 9 additions & 0 deletions article-search/app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
requests
starlette
pydantic
fastapi
uvicorn
urllib3
certifi
kafka-python
elasticsearch
Empty file.
Loading