Skip to content
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

Move Python runner to python module #99

Merged
merged 2 commits into from
Aug 18, 2022
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
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,35 @@ Also, check [caveats](docs/CAVEATS.md) for better understanding the current limi
- Clone from repo: `git clone https://github.com/memiiso/debezium-server-iceberg.git`
- From the root of the project:
- Build and package debezium server: `mvn -Passembly -Dmaven.test.skip package`
- After building, unzip your server distribution: `unzip debezium-server-iceberg-dist/target/debezium-server-iceberg-dist*.zip -d appdist`
- After building, unzip your server
distribution: `unzip debezium-server-iceberg-dist/target/debezium-server-iceberg-dist*.zip -d appdist`
- cd into unzipped folder: `cd appdist`
- Create `application.properties` file and config it: `nano conf/application.properties`, you can check the example configuration in [application.properties.example](debezium-server-iceberg-sink/src/main/resources/conf/application.properties.example)
- Create `application.properties` file and config it: `nano conf/application.properties`, you can check the example
configuration
in [application.properties.example](debezium-server-iceberg-sink/src/main/resources/conf/application.properties.example)
- Run the server using provided script: `bash run.sh`

# Debezium python runner

It's possible to use python to run,operate debezium server

example:

```commandline
pip install git+https://github.com/ismailsimsek/debezium-server-iceberg.git@master#subdirectory=python
debezium
# running with custom arguments
debezium --debezium_dir=/my/debezium_server/dir/ --java_home=/my/java/homedir/
```

# Contributing
The Memiiso community welcomes anyone that wants to help out in any way, whether that includes reporting problems, helping with documentation, or contributing code changes to fix bugs, add tests, or implement new features. See [contributing document](CONTRIBUTING.md) for details.

The Memiiso community welcomes anyone that wants to help out in any way, whether that includes reporting problems,
helping with documentation, or contributing code changes to fix bugs, add tests, or implement new features.
See [contributing document](CONTRIBUTING.md) for details.

### Contributors

<a href="https://github.com/memiiso/debezium-server-iceberg/graphs/contributors">
<img src="https://contributors-img.web.app/image?repo=memiiso/debezium-server-iceberg" />
</a>
109 changes: 109 additions & 0 deletions python/debezium/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import argparse
import jnius_config
import logging
import os
import sys
##### loggger
import threading
from pathlib import Path

log = logging.getLogger(name="debezium")
log.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s %(levelname)s [%(module)s] (%(funcName)s) %(message)s')
handler.setFormatter(formatter)
log.addHandler(handler)


#####

class Debezium():

def __init__(self, debezium_dir: str = None, conf_dir: str = None, java_home: str = None):
if debezium_dir is None:
self.debezium_server_dir: Path = Path(__file__).resolve().parent
else:
if not Path(debezium_dir).is_dir():
raise Exception("Debezium Server directory '%s' not found" % debezium_dir)
self.debezium_server_dir: Path = Path(debezium_dir)
log.info("Setting Debezium dir to:%s" % self.debezium_server_dir.as_posix())

if conf_dir is None:
self.conf_dir = self.debezium_server_dir.joinpath("conf")
else:
if not Path(conf_dir).is_dir():
raise Exception("Debezium conf directory '%s' not found" % conf_dir)
self.conf_dir: Path = Path(conf_dir)
log.info("Setting conf dir to:%s" % self.conf_dir.as_posix())

##### jnius
if java_home:
self.java_home(java_home=java_home)

DEBEZIUM_CLASSPATH: list = [
self.debezium_server_dir.joinpath('*').as_posix(),
self.debezium_server_dir.joinpath("lib/*").as_posix(),
self.conf_dir.as_posix()]
self.add_classpath(*DEBEZIUM_CLASSPATH)

def add_classpath(self, *claspath):
if jnius_config.vm_running:
raise ValueError(
"VM is already running, can't set classpath/options; VM started at %s" % jnius_config.vm_started_at)

jnius_config.add_classpath(*claspath)
log.info("VM Classpath: %s" % jnius_config.get_classpath())

def java_home(self, java_home: str):
if jnius_config.vm_running:
raise ValueError("VM is already running, can't set java home; VM started at" + jnius_config.vm_started_at)

os.putenv("JAVA_HOME", java_home)
os.environ["JAVA_HOME"] = java_home
log.info("JAVA_HOME set to %s" % java_home)

# pylint: disable=no-name-in-module
def run(self, *args: str):

try:
jnius_config.add_options(*args)
log.info("Configured jvm options:%s" % jnius_config.get_options())

from jnius import autoclass
DebeziumServer = autoclass('io.debezium.server.Main')
_dbz = DebeziumServer()
return _dbz.main()
finally:
from jnius import detach
detach()


class DebeziumRunAsyn(threading.Thread):
def __init__(self, debezium_dir: str, java_args: list, java_home: str = None):
threading.Thread.__init__(self)
self.debezium_dir = debezium_dir
self.java_args = java_args
self.java_home = java_home
self._dbz: Debezium = None

def run(self):
self._dbz = Debezium(debezium_dir=self.debezium_dir, java_home=self.java_home)
return self._dbz.run(*self.java_args)


def main():
parser = argparse.ArgumentParser()
parser.add_argument('--debezium_dir', type=str, default=None,
help='Directory of debezium server application')
parser.add_argument('--conf_dir', type=str, default=None,
help='Directory of application.properties')
parser.add_argument('--java_home', type=str, default=None,
help='JAVA_HOME directory')
_args, args = parser.parse_known_args()
ds = Debezium(debezium_dir=_args.debezium_dir, conf_dir=_args.conf_dir, java_home=_args.java_home)
ds.run(*args)


if __name__ == '__main__':
main()
27 changes: 27 additions & 0 deletions python/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
from setuptools import setup, find_packages

setup_py_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(setup_py_dir)

setup(
name='debezium',
entry_points={
'console_scripts': [
'debezium = debezium:main',
],
},
version='0.1.0',
packages=find_packages(),
author="Memiiso Organization",
description='Debezium Server Python runner',
# long_description=pathlib.Path(__file__).parent.joinpath("README.md").read_text(encoding="utf-8"),
# long_description_content_type="text/markdown",
url='https://debezium.io/',
# download_url='https://github.com/memiiso/debezium/archive/master.zip',
include_package_data=True,
license="Apache License 2.0",
test_suite='tests',
install_requires=["pyjnius==1.4.0"],
python_requires='>=3',
)