<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Join JSON property in array of objects, set it to attribute in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/Join-JSON-property-in-array-of-objects-set-it-to-attribute/m-p/363884#M239078</link>
    <description>&lt;P&gt;I have an avro schema that I can get from a registry:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{
    "name": "bar_tbl",
    "fields": [
        { "name": "foo1", "type": "string" },
        { "name": "foo2", "type": "string" }
    ]
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know how to use `EvaluateJsonPath` and get `name` and `fields`.&lt;/P&gt;&lt;P&gt;I'd like to set an attribute with `UpdateAttribute` to equal:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;select foo1, foo2 from bar_tbl&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;to use it later downstream in another processor.&lt;/P&gt;&lt;P&gt;How can I achieve this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or, more directly, I have a REST api that takes an url-encoded sql statement.&amp;nbsp; How can I feed the sql statement to `InvokeHttp`?&lt;/P&gt;</description>
    <pubDate>Wed, 15 Feb 2023 21:54:58 GMT</pubDate>
    <dc:creator>CRISSAEGRIM</dc:creator>
    <dc:date>2023-02-15T21:54:58Z</dc:date>
    <item>
      <title>Join JSON property in array of objects, set it to attribute</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Join-JSON-property-in-array-of-objects-set-it-to-attribute/m-p/363884#M239078</link>
      <description>&lt;P&gt;I have an avro schema that I can get from a registry:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{
    "name": "bar_tbl",
    "fields": [
        { "name": "foo1", "type": "string" },
        { "name": "foo2", "type": "string" }
    ]
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know how to use `EvaluateJsonPath` and get `name` and `fields`.&lt;/P&gt;&lt;P&gt;I'd like to set an attribute with `UpdateAttribute` to equal:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;select foo1, foo2 from bar_tbl&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;to use it later downstream in another processor.&lt;/P&gt;&lt;P&gt;How can I achieve this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or, more directly, I have a REST api that takes an url-encoded sql statement.&amp;nbsp; How can I feed the sql statement to `InvokeHttp`?&lt;/P&gt;</description>
      <pubDate>Wed, 15 Feb 2023 21:54:58 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Join-JSON-property-in-array-of-objects-set-it-to-attribute/m-p/363884#M239078</guid>
      <dc:creator>CRISSAEGRIM</dc:creator>
      <dc:date>2023-02-15T21:54:58Z</dc:date>
    </item>
    <item>
      <title>Re: Join JSON property in array of objects, set it to attribute</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Join-JSON-property-in-array-of-objects-set-it-to-attribute/m-p/363962#M239090</link>
      <description>&lt;P&gt;I was able to achieve this with `ExecuteScript` with pyhon.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import json
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets

flow_file = session.get()

if flow_file is not None:

    # get the flowfile content as json
    stream_content = session.read(flow_file)
    text_content = IOUtils.toString(stream_content, StandardCharsets.UTF_8)
    json_content = json.loads(text_content)
    
    # close the stream; without this, `putAttribute` will fail
    stream_content.close()
    
    # get table name and fields
    table_name = json_content["name"]
    fields = json_content["fields"]
    
    columns = ",".join(col["name"] for col in fields)
    
    # format the select statement
    select_statement = "select " + columns + " from " + table_name
    
    # set the select statement as an attr
    flow_file = session.putAttribute(flow_file, "select_statement", select_statement)
    
    # finalize and pass it on
    session.transfer(flow_file, REL_SUCCESS)
    session.commit()&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 16 Feb 2023 14:38:13 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Join-JSON-property-in-array-of-objects-set-it-to-attribute/m-p/363962#M239090</guid>
      <dc:creator>CRISSAEGRIM</dc:creator>
      <dc:date>2023-02-16T14:38:13Z</dc:date>
    </item>
  </channel>
</rss>

