Using Python in external scripts
Multiple Poster
User avatar
Posts: 3
Joined: Wed Sep 04, 2013 12:04 pm

Getting data to and from my Python script in Kognitio

by swhiteside » Mon Oct 07, 2013 12:22 pm

Hi, I’m having trouble getting data into my Python script from Kognitio and receiving results back. Could someone post a brief explanation?
Reply with quote Top
User avatar
Posts: 21
Joined: Mon Oct 07, 2013 12:15 pm

Re: Getting data to and from my Python script in Kognitio

by ChakLeung » Mon Oct 07, 2013 12:29 pm


Here is a simple example of reading in 3 columns of data in Python and writing it back into KAP without doing any calculations:

Drop external script datapass cascade;
Create external script datapass environment PYTHON
Receives (column1 varchar, column2 varchar, column3 varchar)
Sends (result1 varchar, result2 varchar, result3 varchar)
Limit 1 threads
script S
import csv
import sys
p = csv.reader (sys.stdin)
q = csv.writer (sys.stdout)


for row in p:
DATA_SAMPLES.append (row)

for i in range (TOTAL_DATA_COUNTER):


Select * from
(External script test2 from (select column1, column2, column2 from SCHEMA.TABLE/VIEW)) a;

It’s important to ensure you are using the correct scripting environment, in the example above the Python (ver. 2.7.5) is used.

Secondly, make sure you have dropped the external script before running it again if it worked before. The reason for this being that create an external script (line 2) does not automatically overwrite the old one so you may have a conflict saying that the external script already exists.

Next, the receives statement is where Python receives the data from KAP so you just need to specify the columns you wish to receive from the table/view you specified at the bottom of your script. In the example above you would specify this where “SCHEMA.TABLE/VIEW” is written. It is also possible to use “…” in the receives statement to send all the columns provided that you don’t need to adjust the data types of the columns.

The sends statement is more or less the same as the receives statement in terms of use and is for sending it back to KAP.
For the sake of simplicity, the limit 1 threads only executes on one of the nodes for easier illustration of the results.

The Python code itself is basically receiving the data and appending it in a list. Remember that for each row in KAP that you send to Python, it read in as a list inside another list which houses all the rows.
Reply with quote Top

Who is online

Users browsing this forum: No registered users and 1 guest