We're Moving!

The Vertica Forum is moving to a new OpenText Analytics Database (Vertica) Community.

Join us there to post discussion topics, learn about

product releases, share tips, access the blog, and much more.

Create My New Community Account Now


Passing List of Params while Calling REST API using PythonUDx — Vertica Forum

Passing List of Params while Calling REST API using PythonUDx

priteshpritesh Vertica Customer

Hi Team,
I need a quick help in getting to know that: How can I pass below request packet to udx function which will call api with this request format,
Request:
[ {
"id" : ,
"values" : {
"variable_name1" : "value1",
"variable_name2" : "value2",
} },
{
"id" : ,
"values" : {
"variable_name1" : "value1",
"variable_name3" : "value3",
} }
]

Thanks

Answers

  • Bryan_HBryan_H Vertica Employee Administrator

    Could you elaborate more on the use case and data flow? If this is a parameter to the UDx, I would think you could convert to string and pass as VARCHAR. Or is it used inside the UDx? If so, return as string / VARCHAR, then parse into a flex table or flex column.

  • priteshpritesh Vertica Customer

    @Bryan_H said:
    Could you elaborate more on the use case and data flow? If this is a parameter to the UDx, I would think you could convert to string and pass as VARCHAR. Or is it used inside the UDx? If so, return as string / VARCHAR, then parse into a flex table or flex column.

    Hi Bryan,
    arg1=[{},{},{}]
    Let say, my udx function is GetApiResponse(arg1). In this scenario, how it will work as I want to pass list as a param to Udx function.
    Thanks !!

  • Bryan_HBryan_H Vertica Employee Administrator

    How about this python script "json_demo.py":

    import json
    import vertica_sdk

    class json_demo(vertica_sdk.ScalarFunction):
    def processBlock(self, server_interface, arg_reader, res_writer):
    while True:
    x = arg_reader.getString(0)
    d = json.loads(x)
    res_writer.setString(d["success"]+" "+str(d["status"])+" "+d["message"])
    res_writer.next()
    if not arg_reader.next():
    break

    class json_demo_factory(vertica_sdk.ScalarFunctionFactory):
    def getPrototype(self, srv_interface, arg_types, return_type):
    arg_types.addVarchar()
    return_type.addVarchar()
    def getReturnType(self, srv_interface, arg_types, return_type):
    return_type.addVarchar(80)
    def createScalarFunction(self, srv):
    return json_demo()

    Use this SQL to test:

    \set libfile '\''`pwd`'/json_demo.py\''
    CREATE LIBRARY JsonDemoFunctions AS :libfile LANGUAGE 'Python';

    -- Step 2: Create functions
    CREATE FUNCTION json_demo AS NAME 'json_demo_factory' LIBRARY JsonDemoFunctions;

    -- Step 3: Test
    SELECT json_demo('{"success": "true", "status": 200, "message": "Hello"}');

    -- Step 4: Clean up
    DROP LIBRARY JsonDemoFunctions CASCADE;

  • priteshpritesh Vertica Customer

    @Bryan_H said:
    How about this python script "json_demo.py":

    import json
    import vertica_sdk

    class json_demo(vertica_sdk.ScalarFunction):
    def processBlock(self, server_interface, arg_reader, res_writer):
    while True:
    x = arg_reader.getString(0)
    d = json.loads(x)
    res_writer.setString(d["success"]+" "+str(d["status"])+" "+d["message"])
    res_writer.next()
    if not arg_reader.next():
    break

    class json_demo_factory(vertica_sdk.ScalarFunctionFactory):
    def getPrototype(self, srv_interface, arg_types, return_type):
    arg_types.addVarchar()
    return_type.addVarchar()
    def getReturnType(self, srv_interface, arg_types, return_type):
    return_type.addVarchar(80)
    def createScalarFunction(self, srv):
    return json_demo()

    Use this SQL to test:

    \set libfile '\''`pwd`'/json_demo.py\''
    CREATE LIBRARY JsonDemoFunctions AS :libfile LANGUAGE 'Python';

    -- Step 2: Create functions
    CREATE FUNCTION json_demo AS NAME 'json_demo_factory' LIBRARY JsonDemoFunctions;

    -- Step 3: Test
    SELECT json_demo('{"success": "true", "status": 200, "message": "Hello"}');

    -- Step 4: Clean up
    DROP LIBRARY JsonDemoFunctions CASCADE;

    Thanks Bryan !!

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file