-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
🎉 Destination Meilisearch: migrating to python and fix integration #18036
Changes from 6 commits
dcba28f
4b4970e
5bd1eb2
e9acba0
68e93d5
b6dff5e
58eff3b
b59341a
49c90bd
13da109
b535d98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
* | ||
!Dockerfile | ||
!build | ||
!main.py | ||
!destination_meilisearch | ||
!setup.py |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,38 @@ | ||
FROM airbyte/integration-base-java:dev AS build | ||
FROM python:3.9.11-alpine3.15 as base | ||
|
||
WORKDIR /airbyte | ||
# build and load all requirements | ||
FROM base as builder | ||
WORKDIR /airbyte/integration_code | ||
|
||
ENV APPLICATION destination-meilisearch | ||
# upgrade pip to the latest version | ||
RUN apk --no-cache upgrade \ | ||
&& pip install --upgrade pip \ | ||
&& apk --no-cache add tzdata build-base | ||
|
||
COPY build/distributions/${APPLICATION}*.tar ${APPLICATION}.tar | ||
|
||
RUN tar xf ${APPLICATION}.tar --strip-components=1 && rm -rf ${APPLICATION}.tar | ||
COPY setup.py ./ | ||
# install necessary packages to a temporary folder | ||
RUN pip install --prefix=/install . | ||
|
||
FROM airbyte/integration-base-java:dev | ||
# build a clean environment | ||
FROM base | ||
WORKDIR /airbyte/integration_code | ||
|
||
WORKDIR /airbyte | ||
# copy all loaded and built libraries to a pure basic image | ||
COPY --from=builder /install /usr/local | ||
# add default timezone settings | ||
COPY --from=builder /usr/share/zoneinfo/Etc/UTC /etc/localtime | ||
RUN echo "Etc/UTC" > /etc/timezone | ||
|
||
ENV APPLICATION destination-meilisearch | ||
# bash is installed for more convenient debugging. | ||
RUN apk --no-cache add bash | ||
|
||
COPY --from=build /airbyte /airbyte | ||
# copy payload code only | ||
COPY main.py ./ | ||
COPY destination_meilisearch ./destination_meilisearch | ||
|
||
LABEL io.airbyte.version=0.2.13 | ||
ENV AIRBYTE_ENTRYPOINT "python /airbyte/integration_code/main.py" | ||
ENTRYPOINT ["python", "/airbyte/integration_code/main.py"] | ||
|
||
LABEL io.airbyte.version=0.1.0 | ||
LABEL io.airbyte.name=airbyte/destination-meilisearch |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,123 @@ | ||
# MeiliSearch Destination | ||
# Meilisearch Destination | ||
|
||
This is the repository for the MeiliSearch destination connector, written in Java. | ||
This is the repository for the Meilisearch destination connector, written in Python. | ||
For information about how to use this connector within Airbyte, see [the documentation](https://docs.airbyte.io/integrations/destinations/meilisearch). | ||
|
||
## Local development | ||
|
||
### Prerequisites | ||
**To iterate on this connector, make sure to complete this prerequisites section.** | ||
|
||
#### Build & Activate Virtual Environment | ||
First, build the module by running the following from the `airbyte` project root directory: | ||
#### Minimum Python version required `= 3.7.0` | ||
|
||
#### Build & Activate Virtual Environment and install dependencies | ||
From this connector directory, create a virtual environment: | ||
``` | ||
python -m venv .venv | ||
``` | ||
|
||
This will generate a virtualenv for this module in `.venv/`. Make sure this venv is active in your | ||
development environment of choice. To activate it from the terminal, run: | ||
``` | ||
source .venv/bin/activate | ||
pip install -r requirements.txt | ||
``` | ||
If you are in an IDE, follow your IDE's instructions to activate the virtualenv. | ||
|
||
Note that while we are installing dependencies from `requirements.txt`, you should only edit `setup.py` for your dependencies. `requirements.txt` is | ||
used for editable installs (`pip install -e`) to pull in Python dependencies from the monorepo and will call `setup.py`. | ||
If this is mumbo jumbo to you, don't worry about it, just put your deps in `setup.py` but install using `pip install -r requirements.txt` and everything | ||
should work as you expect. | ||
|
||
#### Building via Gradle | ||
From the Airbyte repository root, run: | ||
``` | ||
./gradlew :airbyte-integrations:connectors:destination-meilisearch:build | ||
``` | ||
|
||
#### Create credentials | ||
If you are running MeiliSearch locally you may not need an api key at all. If there is an API key set for MeiliSearch, you can find instruction on how to find it in the [MeiliSearch docs](https://docs.meilisearch.com/reference/features/authentication.html#master-key). | ||
**If you are a community contributor**, follow the instructions in the [documentation](https://docs.airbyte.io/integrations/destinations/meilisearch) | ||
to generate the necessary credentials. Then create a file `secrets/config.json` conforming to the `destination_meilisearch/spec.json` file. | ||
Note that the `secrets` directory is gitignored by default, so there is no danger of accidentally checking in sensitive information. | ||
See `integration_tests/sample_config.json` for a sample config file. | ||
|
||
**If you are an Airbyte core member**, the integration tests do not require any external credentials. MeiliSearch is run from a test container. | ||
**If you are an Airbyte core member**, copy the credentials in Lastpass under the secret name `destination meilisearch test creds` | ||
and place them into `secrets/config.json`. | ||
|
||
### Locally running the connector | ||
``` | ||
python main.py spec | ||
python main.py check --config secrets/config.json | ||
python main.py discover --config secrets/config.json | ||
python main.py read --config secrets/config.json --catalog integration_tests/configured_catalog.json | ||
``` | ||
|
||
### Locally running the connector docker image | ||
|
||
#### Build | ||
First, make sure you build the latest Docker image: | ||
``` | ||
docker build . -t airbyte/destination-meilisearch:dev | ||
``` | ||
|
||
You can also build the connector image via Gradle: | ||
``` | ||
# in airbyte root directory | ||
./gradlew :airbyte-integrations:connectors:destination-meilisearch:airbyteDocker | ||
``` | ||
When building via Gradle, the docker image name and tag, respectively, are the values of the `io.airbyte.name` and `io.airbyte.version` `LABEL`s in | ||
the Dockerfile. | ||
|
||
#### Run | ||
Then run any of the connector commands as follows: | ||
``` | ||
docker run --rm airbyte/destination-meilisearch:dev spec | ||
docker run --rm -v $(pwd)/airbyte-integrations/connectors/destination-meilisearch/secrets:/secrets airbyte/destination-meilisearch:dev check --config /secrets/config.json | ||
docker run --rm -v $(pwd)/airbyte-integrations/connectors/destination-meilisearch/secrets:/secrets airbyte/destination-meilisearch:dev discover --config /secrets/config.json | ||
docker run --rm -v $(pwd)/airbyte-integrations/connectors/destination-meilisearch/secrets:/secrets -v $(pwd)/airbyte-integrations/connectors/destination-meilisearch/sample_files:/sample_files airbyte/destination-meilisearch:dev read --config /secrets/config.json --catalog /sample_files/configured_catalog.json | ||
docker run --rm -v $(pwd)/secrets:/secrets airbyte/destination-meilisearch:dev check --config /secrets/config.json | ||
# messages.jsonl is a file containing line-separated JSON representing AirbyteMessages | ||
cat messages.jsonl | docker run --rm -v $(pwd)/secrets:/secrets -v $(pwd)/integration_tests:/integration_tests airbyte/destination-meilisearch:dev write --config /secrets/config.json --catalog /integration_tests/configured_catalog.json | ||
``` | ||
## Testing | ||
Make sure to familiarize yourself with [pytest test discovery](https://docs.pytest.org/en/latest/goodpractices.html#test-discovery) to know how your test files and methods should be named. | ||
First install test dependencies into your virtual environment: | ||
``` | ||
pip install .[tests] | ||
``` | ||
### Unit Tests | ||
To run unit tests locally, from the connector directory run: | ||
``` | ||
python -m pytest unit_tests | ||
``` | ||
|
||
### Integration Tests | ||
1. From the airbyte project root, run `./gradlew :airbyte-integrations:connectors:destination-meilisearch:integrationTest` to run the standard integration test suite. | ||
There are two types of integration tests: Acceptance Tests (Airbyte's test suite for all destination connectors) and custom integration tests (which are specific to this connector). | ||
#### Custom Integration tests | ||
Place custom tests inside `integration_tests/` folder, then, from the connector root, run | ||
``` | ||
python -m pytest integration_tests | ||
``` | ||
#### Acceptance Tests | ||
Coming soon: | ||
|
||
### Using gradle to run tests | ||
All commands should be run from airbyte project root. | ||
To run unit tests: | ||
``` | ||
./gradlew :airbyte-integrations:connectors:destination-meilisearch:unitTest | ||
``` | ||
To run acceptance and custom integration tests: | ||
``` | ||
./gradlew :airbyte-integrations:connectors:destination-meilisearch:integrationTest | ||
``` | ||
|
||
## Dependency Management | ||
All of your dependencies should go in `setup.py`, NOT `requirements.txt`. The requirements file is only used to connect internal Airbyte dependencies in the monorepo for local development. | ||
We split dependencies between two groups, dependencies that are: | ||
* required for your connector to work need to go to `MAIN_REQUIREMENTS` list. | ||
* required for the testing need to go to `TEST_REQUIREMENTS` list | ||
|
||
### Publishing a new version of the connector | ||
You've checked out the repo, implemented a million dollar feature, and you're ready to share your changes with the world. Now what? | ||
1. Make sure your changes are passing unit and integration tests. | ||
1. Bump the connector version in `Dockerfile` -- just increment the value of the `LABEL io.airbyte.version` appropriately (we use [SemVer](https://semver.org/)). | ||
1. Create a Pull Request. | ||
1. Pat yourself on the back for being an awesome contributor. | ||
1. Someone from Airbyte will take a look at your PR and iterate with you to merge it into master. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,8 @@ | ||
plugins { | ||
id 'application' | ||
id 'airbyte-python' | ||
id 'airbyte-docker' | ||
id 'airbyte-integration-test-java' | ||
} | ||
|
||
application { | ||
mainClass = 'io.airbyte.integrations.destination.meilisearch.MeiliSearchDestination' | ||
// Needed for JDK17 | ||
applicationDefaultJvmArgs = ['-XX:+ExitOnOutOfMemoryError', '-XX:MaxRAMPercentage=75.0','--add-opens', 'java.base/java.lang=ALL-UNNAMED'] | ||
} | ||
|
||
dependencies { | ||
implementation project(':airbyte-db:db-lib') | ||
implementation project(':airbyte-integrations:bases:base-java') | ||
implementation project(':airbyte-protocol:protocol-models') | ||
|
||
implementation 'com.meilisearch.sdk:meilisearch-java:0.6.0' | ||
|
||
integrationTestJavaImplementation project(':airbyte-integrations:bases:standard-destination-test') | ||
integrationTestJavaImplementation project(':airbyte-integrations:connectors:destination-meilisearch') | ||
|
||
integrationTestJavaImplementation libs.connectors.testcontainers | ||
|
||
implementation files(project(':airbyte-integrations:bases:base-java').airbyteDocker.outputs) | ||
airbytePython { | ||
moduleDirectory 'destination_meilisearch' | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
|
||
from .destination import DestinationMeilisearch | ||
|
||
__all__ = ["DestinationMeilisearch"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
|
||
from logging import Logger | ||
from typing import Any, Iterable, Mapping | ||
|
||
from airbyte_cdk.destinations import Destination | ||
from airbyte_cdk.models import AirbyteConnectionStatus, AirbyteMessage, ConfiguredAirbyteCatalog, DestinationSyncMode, Status, Type | ||
from destination_meilisearch.writer import MeiliWriter | ||
from meilisearch import Client | ||
|
||
|
||
def get_client(config: Mapping[str, Any]) -> Client: | ||
host = config.get("host") | ||
api_key = config.get("api_key") | ||
return Client(host, api_key) | ||
|
||
|
||
class DestinationMeilisearch(Destination): | ||
primary_key = "_ab_pk" | ||
|
||
def write( | ||
self, config: Mapping[str, Any], configured_catalog: ConfiguredAirbyteCatalog, input_messages: Iterable[AirbyteMessage] | ||
) -> Iterable[AirbyteMessage]: | ||
client = get_client(config=config) | ||
|
||
for configured_stream in configured_catalog.streams: | ||
steam_name = configured_stream.stream.name | ||
if configured_stream.destination_sync_mode == DestinationSyncMode.overwrite: | ||
client.delete_index(steam_name) | ||
client.create_index(steam_name, {"primaryKey": self.primary_key}) | ||
|
||
writer = MeiliWriter(client, steam_name, self.primary_key) | ||
for message in input_messages: | ||
if message.type == Type.STATE: | ||
writer.flush() | ||
yield message | ||
elif message.type == Type.RECORD: | ||
writer.queue_write_operation(message.record.data) | ||
else: | ||
continue | ||
writer.flush() | ||
|
||
def check(self, logger: Logger, config: Mapping[str, Any]) -> AirbyteConnectionStatus: | ||
try: | ||
client = get_client(config=config) | ||
writer = MeiliWriter(client, "_airbyte", self.primary_key) | ||
|
||
create_index_job = client.create_index("_airbyte", {"primaryKey": "id"}) | ||
writer.wait_for_job(create_index_job["taskUid"]) | ||
|
||
add_documents_job = client.index("_airbyte").add_documents( | ||
[ | ||
{ | ||
"id": 287947, | ||
"title": "Shazam", | ||
"overview": "A boy is given the ability", | ||
} | ||
] | ||
) | ||
writer.wait_for_job(add_documents_job.task_uid) | ||
|
||
client.index("_airbyte").search("Shazam") | ||
client.delete_index("_airbyte") | ||
return AirbyteConnectionStatus(status=Status.SUCCEEDED) | ||
except Exception as e: | ||
logger.error(f"Check connection failed. Error: {e}") | ||
return AirbyteConnectionStatus(status=Status.FAILED, message=f"An exception occurred: {repr(e)}") |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,47 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
# | ||||||||||||||||||||||||||||||||||||||||||||||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||||||||||||||||||||||||||||||||||||||||||||||
# | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
import time | ||||||||||||||||||||||||||||||||||||||||||||||
from collections.abc import Mapping | ||||||||||||||||||||||||||||||||||||||||||||||
from uuid import uuid4 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
from meilisearch import Client | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
class MeiliWriter: | ||||||||||||||||||||||||||||||||||||||||||||||
write_buffer = [] | ||||||||||||||||||||||||||||||||||||||||||||||
flush_interval = 50000 | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
def __init__(self, client: Client, steam_name: str, primary_key: str): | ||||||||||||||||||||||||||||||||||||||||||||||
self.client = client | ||||||||||||||||||||||||||||||||||||||||||||||
self.steam_name = steam_name | ||||||||||||||||||||||||||||||||||||||||||||||
self.primary_key = primary_key | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
def queue_write_operation(self, data: Mapping): | ||||||||||||||||||||||||||||||||||||||||||||||
random_key = str(uuid4()) | ||||||||||||||||||||||||||||||||||||||||||||||
self.write_buffer.append({**data, self.primary_key: random_key}) | ||||||||||||||||||||||||||||||||||||||||||||||
if len(self.write_buffer) == self.flush_interval: | ||||||||||||||||||||||||||||||||||||||||||||||
self.flush() | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
def flush(self): | ||||||||||||||||||||||||||||||||||||||||||||||
if len(self.write_buffer) == 0: | ||||||||||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
response = self.client.index(self.steam_name).add_documents(self.write_buffer) | ||||||||||||||||||||||||||||||||||||||||||||||
self.wait_for_job(response.task_uid) | ||||||||||||||||||||||||||||||||||||||||||||||
self.write_buffer.clear() | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
def wait_for_job(self, task_uid: str): | ||||||||||||||||||||||||||||||||||||||||||||||
timer = 1 | ||||||||||||||||||||||||||||||||||||||||||||||
while True: | ||||||||||||||||||||||||||||||||||||||||||||||
time.sleep(timer) | ||||||||||||||||||||||||||||||||||||||||||||||
if timer < 60: | ||||||||||||||||||||||||||||||||||||||||||||||
timer = timer * 2 | ||||||||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||||||||
task = self.client.get_task(task_uid) | ||||||||||||||||||||||||||||||||||||||||||||||
status = task["status"] | ||||||||||||||||||||||||||||||||||||||||||||||
if status == "succeeded" or status == "failed": | ||||||||||||||||||||||||||||||||||||||||||||||
break | ||||||||||||||||||||||||||||||||||||||||||||||
except: | ||||||||||||||||||||||||||||||||||||||||||||||
timer = 60 | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please simplify the function, if you're going to do a try/except you must raise the error you're dealing with. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @marcosmarxm meilisearch sdk has a method to wait for the async operation to finished. Now is much simpler. The Building a Python Destination docs refers the KvDB destination connector but it lacks unit tests and logger message on flush method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a logger message saying you're flushing the records.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added!