[英]NIFI - upload binary.zip to SQL Server as varbinary
我正在嘗試將 binary.zip 作為varbinary
類型的列內容上傳到 SQL Server。
目標表:
CREATE TABLE myTable ( zipFile varbinary(MAX) );
我的NIFI 流程非常簡單:
-> GetFile:
filter:binary.zip
-> UpdateAttribute:<br>
sql.args.1.type = -3 # as varbinary according to JDBC types enumeration
sql.args.1.value = ??? # I don't know what to put here ! (I've triying everything!)
sql.args.1.format= ??? # Is It required? I triyed 'hex'
-> PutSQL:<br>
SQLstatement= INSERT INTO myTable (zip_file) VALUES (?);
我應該在sql.args.1.value
中放入什么?
我認為它應該是流文件有效負載,但它可以作為 PutSQL 中 INSERT 的一部分工作嗎? 暫時沒有!
謝謝!
解決方案更新:
基於https://issues.apache.org/jira/browse/NIFI-8052 (考慮我發送一些數據作為屬性參數)
import java.nio.charset.StandardCharsets
import org.apache.nifi.controller.ControllerService
import groovy.sql.Sql
def flowFile = session.get()
def lookup = context.controllerServiceLookup
def dbServiceName = flowFile.getAttribute('DatabaseConnectionPoolName')
def tableName = flowFile.getAttribute('table_name')
def fieldName = flowFile.getAttribute('field_name')
def dbcpServiceId = lookup.getControllerServiceIdentifiers(ControllerService).find
{ cs -> lookup.getControllerServiceName(cs) == dbServiceName }
def conn = lookup.getControllerService(dbcpServiceId)?.getConnection()
def sql = new Sql(conn)
flowFile.read{ rawIn->
def parms = [rawIn ]
sql.executeInsert "INSERT INTO " + tableName + " (date, "+ fieldName + ") VALUES (CAST( GETDATE() AS Date ) , ?) ", parms
}
conn?.close()
if(!flowFile) return
session.transfer(flowFile, REL_SUCCESS)
session.commit()
也許有一種插入 blob 的 nifi 本機方式,但是您可以使用ExecuteGroovyScript
而不是 UpdateAttribute 和 PutSQL
在處理器級別添加SQL.mydb
參數並將其鏈接到所需的 DBCP 池。
使用以下腳本主體:
def ff=session.get()
if(!ff)return
def statement = "INSERT INTO myTable (zip_file) VALUES (:p_zip_file)"
def params = [
p_zip_file: SQL.mydb.BLOB(ff.read()) //cast flow file content as BLOB sql type
]
SQL.mydb.executeInsert(params, statement) //committed automatically on flow file success
//transfer to success without changes
REL_SUCCESS << ff
腳本SQL.mydb
內部是對groovy.sql.Sql對象的引用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.