-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎉 Destination Meilisearch: migrating to python and fix integration (#…
…18036) * Add meilisearch python destionation * add destination * first working version * Writting tests * writer more resilient * Adding unit test * correct integration test stream param * update doc * remove unused variables * auto-bump connector version Co-authored-by: marcosmarxm <[email protected]> Co-authored-by: Octavia Squidington III <[email protected]>
- Loading branch information
1 parent
4117f9d
commit 0f4ef13
Showing
21 changed files
with
448 additions
and
357 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
airbyte-integrations/connectors/destination-meilisearch/.dockerignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
* | ||
!Dockerfile | ||
!build | ||
!main.py | ||
!destination_meilisearch | ||
!setup.py |
38 changes: 28 additions & 10 deletions
38
airbyte-integrations/connectors/destination-meilisearch/Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=1.0.0 | ||
LABEL io.airbyte.name=airbyte/destination-meilisearch |
113 changes: 102 additions & 11 deletions
113
airbyte-integrations/connectors/destination-meilisearch/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
24 changes: 3 additions & 21 deletions
24
airbyte-integrations/connectors/destination-meilisearch/build.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
8 changes: 8 additions & 0 deletions
8
airbyte-integrations/connectors/destination-meilisearch/destination_meilisearch/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
69 changes: 69 additions & 0 deletions
69
...te-integrations/connectors/destination-meilisearch/destination_meilisearch/destination.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# | ||
# 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) | ||
|
||
create_index_job = client.create_index("_airbyte", {"primaryKey": "id"}) | ||
client.wait_for_task(create_index_job["taskUid"]) | ||
|
||
add_documents_job = client.index("_airbyte").add_documents( | ||
[ | ||
{ | ||
"id": 287947, | ||
"title": "Shazam", | ||
"overview": "A boy is given the ability", | ||
} | ||
] | ||
) | ||
client.wait_for_task(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)}") |
8 changes: 4 additions & 4 deletions
8
...-meilisearch/src/main/resources/spec.json → ...isearch/destination_meilisearch/spec.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
airbyte-integrations/connectors/destination-meilisearch/destination_meilisearch/writer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from collections.abc import Mapping | ||
from logging import getLogger | ||
from uuid import uuid4 | ||
|
||
from meilisearch import Client | ||
|
||
logger = getLogger("airbyte") | ||
|
||
|
||
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): | ||
buffer_size = len(self.write_buffer) | ||
if buffer_size == 0: | ||
return | ||
logger.info(f"flushing {buffer_size} records") | ||
response = self.client.index(self.steam_name).add_documents(self.write_buffer) | ||
self.client.wait_for_task(response.task_uid, 1800000, 1000) | ||
self.write_buffer.clear() |
Oops, something went wrong.