API Node: Creating a Python endpoint that executes arbitrary SQL passed in the request

hugodipaolo
hugodipaolo Registered Posts: 4 ✭✭

Hi everyone,

I’m working with Dataiku API Node (Python endpoint) and I’m exploring an API pattern where the endpoint can execute a SQL query that is passed dynamically in the API call.

Use case

The goal is to:

  • Create a Python API endpoint
  • Accept a SQL query as part of the request payload, for example:

{
"query": "SELECT COUNT(*) FROM TABLE"
}

  • Execute that query on a predefined database connection (e.g. SNOWFLAKE)
  • Return the results as JSON

This endpoint would only be used internally, but I’m fully aware that executing arbitrary SQL brings security concerns, so I’m trying to understand what is possible with Dataiku.

Is there a way to interact with a snowflake database using a sql statement in a Python endpoint ?

Here is what I tried :

import dataiku
from dataiku.core.sql import SQLExecutor2

dataiku.set_default_project_key("PROJECT_KEY")

# function to execute the query using SQLExecutor2
def executeQuery(database, query):


query_result = SQLExecutor2(connection=database).query_to_df(query)


return query_result


def api_py_function():

return executeQuery("SNOWFLAKE_CXN_NAME",
"SELECT COUNT(*) FROM SCEHMAS.TABLE")

result : no adapters were found for intercom/sql-queries/start-streaming

Thanks in advance.

Dataiku version used: 13.5.5

Answers

Setup Info
    Tags
      Help me…