How to call transform api using python

I am trying to develop a python script since the max frequency parameter for transform api ranges from 1s to 1h.

I am using:

  • python verison 3.6.8
  • Elasticsearch python api: python -m pip install elasticsearch

UPDATE 1

  • ELK : 7.15.1
  • testing python scripts from windows 10

tried two versions of python script.

version 1:

from elasticsearch import Elasticsearch
from elasticsearch._async.client.transform import TransformClient
import logging

logger = logging.getLogger(__name__)
logging.basicConfig(filename="transform.log", level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%d/%m/%Y %H:%M:%S')
logger.setLevel(logging.DEBUG)


# ES info
ES_HOST = "localhost"
ES_PORT = "9201"
try:
    es = Elasticsearch([{'host':ES_HOST,'port':ES_PORT}])
    logging.info(es)
except Exception as e:
    logging.fatal(e)

# start transform
try:
    tc = es.transform.TransformClient()
    start_transform_resp = tc.start_transform('poc_aggs')
    print(start_transform_resp)

except Exception as e:
    logging.fatal(e)

this one result in 'TransformClient' object has no attribute 'TransformClient'.

version 2:

from elasticsearch import Elasticsearch
from elasticsearch._async.client.transform import TransformClient
import logging

logger = logging.getLogger(__name__)
logging.basicConfig(filename="transform.log", level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%d/%m/%Y %H:%M:%S')
logger.setLevel(logging.DEBUG)


# ES info
ES_HOST = "localhost"
ES_PORT = "9201"
try:
    es = Elasticsearch([{'host':ES_HOST,'port':ES_PORT}])
    logging.info(es)
except Exception as e:
    logging.fatal(e)

# start transform
try:
    tc = TransformClient(es)
    start_transform_resp = tc.start_transform('poc_aggs')
    print(start_transform_resp)

except Exception as e:
    logging.fatal(e)

result in sys:1: RuntimeWarning: coroutine 'start_transform' was never awaited and the transform isn't executed in Elasticsearch.

what's the correct syntax please ? can't find an example to call the TransformClient using python.

I found it based on the doc, simply do:

from elasticsearch import Elasticsearch
es = Elasticsearch(
            [{'host':ES_HOST,'port':ES_PORT}],
            http_auth=(USER, SECRET),
            #port=ES_PORT,
            use_ssl=True,
            verify_certs=True,
            client_certs=CA_CERT_CLIENT
        )
# start transform
es.transform.start_transform(transform_name, ...)
# create transform
es.transform.put_transform(transform_name, body)
# etc..

more Transform API here.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.