matillion

Is it possible to update a Matillion Job DateTime variable from a Jython script?


I'm trying to pass a DateTime values from a Python (jython) component to a Python (python 3) component but it doesn't seem to work

Matillion Orchestraton Job

There is a Matillion Job Variable myvar with type DateTime and default value 2028-01-28 12:00:00

The Jython code is as follow:

from dateutil.parser import parse
from datetime import timedelta  

print('class', myvar.getClass())
myvar = parse(str(myvar.toInstant())).replace(tzinfo=None)
print('myvar', repr(myvar))
myvar2 = myvar + timedelta(days=1)
print('myvar2', repr(myvar2))
context.updateVariable('myvar', myvar2)
print('myvar',  repr(myvar))

I can see already that myvar has the wrong type (str) the end of the Jython execution

('class', <type 'com.matillion.bi.emerald.server.scripting.MatillionDate'>)
('myvar', 'datetime.datetime(2028, 1, 28, 12, 0)')
('myvar2', 'datetime.datetime(2028, 1, 29, 12, 0)')
('myvar', u'2028-01-29 12:00:00.0')

I assume that I can't update the variable passing a datetime instance. I guess I must convert the datetime back to a com.matillion.bi.emerald.server.scripting.MatillionDate since that seems to be the type of myvar inside Jython.

The question is how do I convert my datetime to a MatillionDate?


Solution

  • I think you're seeing the string value because you're using the repr() function of the variable.

    I've taken your example and added a few additional lines to show what I mean.

    from datetime import datetime
    print('myvar',  type(myvar))
    context.updateVariable('myvar', datetime.now())
    print('myvar', myvar)
    print('myvar', type(myvar))
    

    You can see that the 'type' is actually a Java Timestamp, which makes sense because Jython executes in the JVM. You can update the variable with a Python datetime just as easily where the resulting type is also a Java Timestamp.

    To answer your question though, if you set the Matillion variable to the value of str(myvar2), you should be able to access the same/updated value in the Python3 component afterwards (as a datetime.datetime object), without the necessity of creating another MatillionDate value. This would just require yet another type conversion when consuming that value from Python3.