Skip to content

Commit a44ac37

Browse files
Rus DanielRusEu
authored andcommitted
initial commit
0 parents  commit a44ac37

File tree

8 files changed

+272
-0
lines changed

8 files changed

+272
-0
lines changed

.gitignore

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
98+
__pypackages__/
99+
100+
# Celery stuff
101+
celerybeat-schedule
102+
celerybeat.pid
103+
104+
# SageMath parsed files
105+
*.sage.py
106+
107+
# Environments
108+
.env
109+
.venv
110+
env/
111+
venv/
112+
ENV/
113+
env.bak/
114+
venv.bak/
115+
116+
# Spyder project settings
117+
.spyderproject
118+
.spyproject
119+
120+
# Rope project settings
121+
.ropeproject
122+
123+
# mkdocs documentation
124+
/site
125+
126+
# mypy
127+
.mypy_cache/
128+
.dmypy.json
129+
dmypy.json
130+
131+
# Pyre type checker
132+
.pyre/
133+
134+
# pytype static type analyzer
135+
.pytype/
136+
137+
# Cython debug symbols
138+
cython_debug/

Readme.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Kafkat
2+
3+
![Python Logo](https://www.python.org/static/community_logos/python-logo.png "Python logo")
4+
5+
6+
This is a simple Python application intended to provide a way to search for data in kafka.
7+
8+
9+
## How to use it
10+
11+
12+
13+
Command:
14+
15+
`$ kafkat --help`
16+
17+
```
18+
Options:
19+
-b, --bootstrap-server TEXT Bootstrap servers [default: 0.0.0.0:9092]
20+
-c, --auto-commit Enable autocommit [default: True]
21+
-o, --auto-offset-reset TEXT Auto offset reset [default: earliest]
22+
-e, --separator TEXT Separator [default: ,]
23+
-p, --prettify Prettify Json Output. [default: False]
24+
-t, --topic TEXT Topic Pattern [default: .*]
25+
-s, --search TEXT Search Pattern [default: .*]
26+
--help Show this message and exit.
27+
```
28+
29+
----
30+
31+
Command:
32+
33+
`$ kafkat --search '.*982834084219' -t 'topic_1.*' -t 'topic_2.*' -t 'topic_3.*' --prettify`
34+
35+
```json
36+
{
37+
"order": "982834084219",
38+
"updatedAt": "2021-05-24T17:06:34.267Z",
39+
},
40+
{
41+
"order": "982834084219",
42+
"updatedAt": "2021-05-24T17:06:34.267Z",
43+
},
44+
```
45+

kafkat/__init__.py

Whitespace-only changes.

kafkat/bin/kafkat

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python
2+
3+
from kafkat.cli import kafkat
4+
5+
kafkat()
6+

kafkat/cli.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import re
2+
3+
import click
4+
from kafka import KafkaConsumer
5+
6+
from .common.utils import decode_value, prettify_json
7+
8+
9+
@click.command()
10+
@click.option('-b', '--bootstrap-server', help='Bootstrap servers',
11+
default=['0.0.0.0:9092'], multiple=True, show_default=True)
12+
@click.option('-c', '--auto-commit', help='Enables autocommit',
13+
default=True, type=bool, is_flag=True, show_default=True)
14+
@click.option('-o', '--auto-offset-reset', help='Auto offset reset',
15+
default='earliest', show_default=True)
16+
@click.option('-e', '--separator', help='Separator',
17+
default=',', show_default=True)
18+
@click.option('-p', '--prettify', help='Prettify Json Output.',
19+
default=False, type=bool, is_flag=True, show_default=True)
20+
@click.option('-t', '--topic', help="Topic Pattern",
21+
multiple=True, default='.*', show_default=True)
22+
@click.option('-s', '--search', help="Search Pattern",
23+
default='.*', show_default=True)
24+
def kafkat(bootstrap_server, auto_commit, auto_offset_reset,
25+
separator, prettify, topic, search):
26+
27+
consumer = KafkaConsumer(
28+
bootstrap_servers=bootstrap_server,
29+
auto_offset_reset=auto_offset_reset,
30+
enable_auto_commit=auto_commit,
31+
value_deserializer=decode_value
32+
)
33+
34+
consumer.subscribe(pattern='|'.join(topic))
35+
36+
pattern = re.compile(search, re.DOTALL)
37+
38+
for message in consumer:
39+
value = message.value
40+
41+
if prettify:
42+
value = prettify_json(value)
43+
44+
if separator:
45+
value = f'{value}{separator}'
46+
47+
if re.match(pattern, value):
48+
click.echo(value)

kafkat/common/__init__.py

Whitespace-only changes.

kafkat/common/utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import json
2+
3+
4+
def decode_value(value):
5+
try:
6+
return json.loads(value.decode('utf-8'))
7+
except Exception as e:
8+
return {'error': str(e)}
9+
10+
11+
def prettify_json(value):
12+
return json.dumps(value, indent=4, sort_keys=True)

setup.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
3+
from distutils.core import setup
4+
5+
import setuptools # noqa
6+
7+
setup(
8+
name='kafkat',
9+
version='1.0',
10+
description='Kafka Python Cli',
11+
author='Daniel Rus',
12+
author_email='[email protected]',
13+
url="",
14+
packages=[
15+
'kafkat',
16+
'kafkat/common'
17+
],
18+
install_requires=[
19+
'click==7.1.2',
20+
'kafka-python==2.0.2'
21+
],
22+
scripts=["./kafkat/bin/kafkat"]
23+
)

0 commit comments

Comments
 (0)