cognitedata / how-to-cognite-function-extr-pipeline-config

Repo for Cognite Hub article on using extraction pipeline configuration for managing Cognite Function configuration.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cognite function template

Using Cognite Functions

Structure of this repository

Functions

Here you can find two simple functions implemented: sine_function.

Generally a function, named azure_jackupdata_cognitefunction (previously as example function) in the example below, consists of the following files:

📦my_cognite_function (azure_jackupdata_cognitefunction in this code)
 ┣ 📂schedules
 ┃ ┗ 📜main.yaml - (Optional) Schedule if you want to execute the function on a schedule.
 ┣ 📜__init__.py - Empty file (required to make the function into a package)
 ┣ 📜function_config.yaml - (Optional) Configuration for the function
 ┣ 📜handler.py - Module with script inside a handle function
 ┗ 📜requirements.txt - Explicitly states the dependencies needed to run the handler.py script.
schedules/master.yaml

Each function's folder contains a schedules folder where you can put your files that define your schedules. By default, we have added a file here called main.yaml which will be used whenever you merge a PR to main This file is ues with GitHub actions and automatic deployment - If you don't need any schedules for a specific function, just delete it!

Example

- name: My daily schedule
  cron: "0 0 * * *"
  data:
    ExtractionPipelineExtId: "sine-function"
function_config.yaml

Each function's folder contains a function_config.yaml file where you can specify most configuration parameters (per function). These parameters are extracted and used by the Github Workflow files during deployment (read more in the "build and deployment" section).

Example template, see function details for description of all configuration parameters.

description: "Cognite Function that emits a simple Sine curve into a time series in CDF"
owner: your.name@domain.com
cpu: 0.25
memory: 1.00
metadata:
  version: "1.0.0"
handler.py

This is the code for your function is provided, where the handle object is required.

Example below, for a full description of the arguments that can be passed to this function see cognite-sdk - create function.

def handle(data, client):
    print(f"Hello from {__name__}!")
    print("I got the following data:")
    print(data)
    print("Will now return data")
    return data
requirements.txt

Each function's folder contains requirements.txt. You can use that file to add extra dependencies that your code is depending on. By default, you have a newer version of cognite-sdk installed, but it doesn't hurt to be specific here!

Example requirements.txt file

cognite-extractor-utils>=2.2.0
cognite-logger>=0.5
numpy>=1.21.6

Run code locally

To run code locally, you may create a file .env. Content of this file should be:

PYTHONPATH=<path to your root folder of this repository>

COGNITE_PROJECT=<Cognite data Fusion project name>
COGNITE_BASE_URL=https://<CDF cluster>.cognitedata.com

# OIDC - Ex: Azure AD connection details
TENANT_ID=<Azure AD tenant ID>
CLIENT_ID=<Azure AD application object ID>
CLIENT_SECRET=<Azure AD application secret ID>

With this file you should be able to run the provide test python file that wil trigger the handle function in your handler.py file


About

Repo for Cognite Hub article on using extraction pipeline configuration for managing Cognite Function configuration.

License:Apache License 2.0


Languages

Language:Python 100.0%