<?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 Re: NiFi: Write to custom delimitted file in Archives of Support Questions (Read Only)</title>
    <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/NiFi-Write-to-custom-delimitted-file/m-p/153942#M48895</link>
    <description>&lt;P&gt;Would it be possible to get the result as an AVRO file and work from there?&lt;/P&gt;</description>
    <pubDate>Thu, 15 Dec 2016 09:44:45 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2016-12-15T09:44:45Z</dc:date>
    <item>
      <title>NiFi: Write to custom delimitted file</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/NiFi-Write-to-custom-delimitted-file/m-p/153941#M48894</link>
      <description>&lt;P&gt;After querying from Hive, I wanted to write into a file with a custom delimiter say |, is there a way to achieve that?  I cannot use Replacetext to replace, with the custom delimiter as the column values may have comma in it.&lt;/P&gt;&lt;P&gt;One other option I see is have query producing single string with custom delimiter.  But it encloses the column values with the double quotes for the string type columns and that voids the required fileformat.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Dec 2016 08:47:12 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/NiFi-Write-to-custom-delimitted-file/m-p/153941#M48894</guid>
      <dc:creator>ashsskum</dc:creator>
      <dc:date>2016-12-15T08:47:12Z</dc:date>
    </item>
    <item>
      <title>Re: NiFi: Write to custom delimitted file</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/NiFi-Write-to-custom-delimitted-file/m-p/153942#M48895</link>
      <description>&lt;P&gt;Would it be possible to get the result as an AVRO file and work from there?&lt;/P&gt;</description>
      <pubDate>Thu, 15 Dec 2016 09:44:45 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/NiFi-Write-to-custom-delimitted-file/m-p/153942#M48895</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2016-12-15T09:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: NiFi: Write to custom delimitted file</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/NiFi-Write-to-custom-delimitted-file/m-p/153943#M48896</link>
      <description>&lt;P&gt;Here is what i suggest. the code is simple.&lt;/P&gt;&lt;P&gt;The processor calls HiveJdbcCommon.convertToCsvStream which is a custom class built&lt;/P&gt;&lt;PRE&gt;public void process(final OutputStream out) throws IOException {
                    try {
                        logger.debug("Executing query {}", new Object[]{selectQuery});
                        final ResultSet resultSet = st.executeQuery(selectQuery);
                        if (AVRO.equals(outputFormat)) {
                            nrOfRows.set(HiveJdbcCommon.convertToAvroStream(resultSet, out));
                        } else if (CSV.equals(outputFormat)) {
                            nrOfRows.set(HiveJdbcCommon.convertToCsvStream(resultSet, out));
                        } else {
                            nrOfRows.set(0L);
                            throw new ProcessException("Unsupported output format: " + outputFormat);
                        }
&lt;/PRE&gt;&lt;P&gt;The custom class is here&lt;/P&gt;&lt;PRE&gt;public static long convertToCsvStream(final ResultSet rs, final OutputStream outStream, String recordName, ResultSetRowCallback callback)
            throws SQLException, IOException {


        final ResultSetMetaData meta = rs.getMetaData();
        final int nrOfColumns = meta.getColumnCount();
        List&amp;lt;String&amp;gt; columnNames = new ArrayList&amp;lt;&amp;gt;(nrOfColumns);


        for (int i = 1; i &amp;lt;= nrOfColumns; i++) {
            String columnNameFromMeta = meta.getColumnName(i);
            // Hive returns table.column for column name. Grab the column name as the string after the last period
            int columnNameDelimiter = columnNameFromMeta.lastIndexOf(".");
            columnNames.add(columnNameFromMeta.substring(columnNameDelimiter + 1));
        }


        // Write column names as header row
        outStream.write(StringUtils.join(columnNames, ",").getBytes(StandardCharsets.UTF_8));
        outStream.write("\n".getBytes(StandardCharsets.UTF_8));


        // Iterate over the rows
        long nrOfRows = 0;
        while (rs.next()) {
            if (callback != null) {
                callback.processRow(rs);
            }
            List&amp;lt;String&amp;gt; rowValues = new ArrayList&amp;lt;&amp;gt;(nrOfColumns);
            for (int i = 1; i &amp;lt;= nrOfColumns; i++) {
                final int javaSqlType = meta.getColumnType(i);
                final Object value = rs.getObject(i);


                switch (javaSqlType) {
                    case CHAR:
                    case LONGNVARCHAR:
                    case LONGVARCHAR:
                    case NCHAR:
                    case NVARCHAR:
                    case VARCHAR:
                        String valueString = rs.getString(i);
                        if (valueString != null) {
                            rowValues.add("\"" + StringEscapeUtils.escapeCsv(valueString) + "\"");
                        } else {
                            rowValues.add("");
                        }
                        break;
                    case ARRAY:
                    case STRUCT:
                    case JAVA_OBJECT:
                        String complexValueString = rs.getString(i);
                        if (complexValueString != null) {
                            rowValues.add(StringEscapeUtils.escapeCsv(complexValueString));
                        } else {
                            rowValues.add("");
                        }
                        break;
                    default:
                        if (value != null) {
                            rowValues.add(value.toString());
                        } else {
                            rowValues.add("");
                        }
                }
            }
            // Write row values
            outStream.write(StringUtils.join(rowValues, ",").getBytes(StandardCharsets.UTF_8));
            outStream.write("\n".getBytes(StandardCharsets.UTF_8));
            nrOfRows++;
        }
        return nrOfRows;
    }
&lt;/PRE&gt;&lt;P&gt;The code is very simple.  Basically where it add a comma, you an replace with your delimiter.  build the nar (very simple) and there you go.  in reality I believe this code can be easily enhanced by accepting a input parameter ie delimiter, and the processor would spit out the result set in the delimiter you have identified.  If I have a hour or so next week I will post the custom nar info here.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Dec 2016 11:58:51 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/NiFi-Write-to-custom-delimitted-file/m-p/153943#M48896</guid>
      <dc:creator>sunile_manjee</dc:creator>
      <dc:date>2016-12-15T11:58:51Z</dc:date>
    </item>
    <item>
      <title>Re: NiFi: Write to custom delimitted file</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/NiFi-Write-to-custom-delimitted-file/m-p/153944#M48897</link>
      <description>&lt;P&gt;The only option with Avro available is to split into JSON, but there on any thoughts on how that can be emitted in to a delimited format?&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 02:46:39 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/NiFi-Write-to-custom-delimitted-file/m-p/153944#M48897</guid>
      <dc:creator>ashsskum</dc:creator>
      <dc:date>2016-12-16T02:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: NiFi: Write to custom delimitted file</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/NiFi-Write-to-custom-delimitted-file/m-p/153945#M48898</link>
      <description>&lt;P&gt;Thanks, that appears to be relatively simple change.  I will give a try&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 02:47:24 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/NiFi-Write-to-custom-delimitted-file/m-p/153945#M48898</guid>
      <dc:creator>ashsskum</dc:creator>
      <dc:date>2016-12-16T02:47:24Z</dc:date>
    </item>
    <item>
      <title>Re: NiFi: Write to custom delimitted file</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/NiFi-Write-to-custom-delimitted-file/m-p/153946#M48899</link>
      <description>&lt;P&gt;you could use a simple python script with executescript to achieve that.&lt;/P&gt;&lt;P&gt;&lt;A href="http://funnifi.blogspot.com/2016/03/executescript-json-to-json-revisited_14.html"&gt;http://funnifi.blogspot.com/2016/03/executescript-json-to-json-revisited_14.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 03:08:16 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/NiFi-Write-to-custom-delimitted-file/m-p/153946#M48899</guid>
      <dc:creator>knarayanan</dc:creator>
      <dc:date>2016-12-16T03:08:16Z</dc:date>
    </item>
  </channel>
</rss>

