UUID generator in Python

Hi

I am trying to find out how to use the Python Script node to make a generator for single UUID. I have not been able to figure out what data type output_tables is respectively how I can convert a scalar or uuid.UUID object to knime.api.Table. I have seen the pandas.DataFrame example, but am reluctant to use it and throw away unused parts in the aftermath.

The background of this is that I want to generate a UUID that will be used to identify a load process. So, I know the overhead of the aftermath would be negligible, but it is very awkward to me.
[edit] An efficient implementation might be suitable as ID generator for payload records.

Can anyone point me into the right direction?

Kind regards

Thiemo

Try this component:

2 Likes

Thanks for the hint. My primary goal is not a pk generator, but a generator for a “session id”, “load id” what ever you want to call it.

And it would prevent me learning how to bring a Python scalar into KNIME. :wink:

Maybe I should go for that generator anyway.

Sorry. Don’t understand your issue.

Hey @Thiemo.Kellner,

If I understand correctly, you want to create a single unique identifier that you will use throughout your workflow. Perhaps to record some sort of “Session ID” for you to reference later on.


Recommended approach: GUID Generator component

If you simply use the Empty Table Creator node and set it to generate one row, then use the GUID Generator component, that will effectively create a single GUID/UUID that can be accessed elsewhere in your workflows.

image

You could also tack on a Table Row to Variable node if you wanted to access the GUID as a flow variable.

Example workflow: single-uuid-for-session – KNIME Community Hub


Alternate approach: Python

If you really wanted to do it in Python, you could use a Python Script node with a script like the one below, but I wouldn’t recommend it. The GUID Generator component uses Java which would likely execute a bit faster.

import knime.scripting.io as knio

import pandas as pd
import uuid

knio.output_tables[0] = knio.Table.from_pandas(
    pd.DataFrame({'session_id': [str(uuid.uuid4())]})
)

Example workflow: single-uuid-for-session-python – KNIME Community Hub


Good luck and have fun!

Cheers,

@sjporter

4 Likes

@Thiemo.Kellner you could do it in Java or R/RStudio.

1 Like

Sorry for not having made me clear.

Quite on point. Many thanks.

From the doc at DataFrame — pandas 2.1.1 documentation I was thinking that DataFrame are two dimensional only. Maybe I need much more time to understand the concept behind it.
Is the input to the constructor invalid JSON?

Hey @Thiemo.Kellner,

In my Python script I am simply declaring a DataFrame with a single column (session_id) and a single row containing the uuid. The output is still a “two dimensional” object with columns and rows, there is just only one column and one row in this case.

Cheers,

@sjporter

Hi @sjporter

Thanks for the shed light. I got confused by the “two”. If they just spoke of a table, I would have got it right from the beginning. I am a simple database person. :blush:

Kind regards

Thiemo

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