把HDFS裡的json資料轉換成csv格式

來源:互聯網
上載者:User

標籤:

?

1. 全景

2. 用ListHDFS擷取所有檔案名稱

?

如果想重新再取一次,右健view state:

?

點擊 clear state, 再運行,即可再次採集資料了。

?

3. 用FetchHDFS 取出json 資料

?

4. 用ExecuteScript 轉換

import org.apache.commons.io.IOUtils import java.nio.charset.* import java.text.SimpleDateFormat import groovy.json.*

def flowFile = session.get()

flowFile = session.write(flowFile, {inputStream, outputStream ->

def js = IOUtils.toString(inputStream, StandardCharsets.UTF_8) def data = new JsonSlurper().parseText( js ) def columns = data.data*.keySet().flatten().unique()

// Wrap strings in double quotes, and remove nulls def encode = { e -> e == null ? ‘‘ : e instanceof String ? /"$e"/ : "$e" }

// Print all the column names def columnName = columns.collect { c -> encode( c ) }.join( ‘,‘ )

// Then create all the rows def columnData = data.data.collect { row -> // A row at a time columns.collect { colName -> encode( row[ colName ] ) }.join( ‘,‘ ) }.join( ‘\n‘ )

StringBuilder cd = new StringBuilder() cd.append(columnName + "\n") cd.append(columnData)

outputStream.write(cd.toString().getBytes(StandardCharsets.UTF_8)) }as StreamCallback)

session.transfer(flowFile, REL_SUCCESS)

?

參考:http://stackoverflow.com/questions/21576162/groovy-code-to-convert-json-to-csv-file

?

5. 用PutHDFS 插入

?

問題:

最近加了cluster,發現listhdfs不能取到資料了:

?

查看日誌:

?

發現日誌裡提到了zookeeper導致connection refused

?

nifi設定成cluster必須走zookeeper來調度資源,所以必須要連上我們的zookeeper server,有一個配置要加

conf/state-management.xml裡面有個配置

<cluster-provider> <id>zk-provider</id> <class>org.apache.nifi.controller.state.providers.zookeeper.ZooKeeperStateProvider</class> <property name="Connect String">wdp.xxx.cn:2181</property> <property name="Root Node">/nifi</property> <property name="Session Timeout">30 seconds</property> <property name="Access Control">CreatorOnly</property> <property name="Username">nifi</property> <property name="Password">nifi</property> </cluster-provider>

把HDFS裡的json資料轉換成csv格式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.