惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
云风的 BLOG
云风的 BLOG
罗磊的独立博客
C
Check Point Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Blog — PlanetScale
Blog — PlanetScale
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
宝玉的分享
宝玉的分享
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
I
InfoQ
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog
B
Blog RSS Feed

博客园 - 自由港

postgres 支持全文索引 查看 milvus 中的数据 查询数据库锁死情况 使用 keepalived 实现 tendis 高可用 部署tendis IDEA 运行 main 方法导致整个项目 install 自定义classloader hive 基础操作 在 服务器部署 seatunnel web服务 使用 seatunnel web 设计一个数据同步 如何 运行 seatunnel web 开发版 使用 seatunnel 实现数据同步 avro 数据入门 NIFI国际化 NIFI 使用HTTP 作为数据源接收数据 使用NIFI 同步数据库表 使用 NIFI监控数据库表 切换JDK NIFI实现配置分发
使用 NIFI读取EXCEL 数据到数据库
自由港 · 2025-11-08 · via 博客园 - 自由港

1.概述

从EXCEL读取数据后,写入数据库这种场景 是很常见的,本次使用NIFI实现将 EXCEL 读取后吸入数据库的过程。

image

2.配置过程

2.1 准备一个EXCEL 文件

image

excel 长这样。

2.2 配置EXCELREADER

image

在画布上右键点击 controller services

image

image

这里选 Use 'Schema Name' Property

Schema Registry 这里我们可以创建一个AvroSchemaRegistry

image

这里增加一个 demo1 的 schema 的配置

配置如下:

{
  "type": "record",
  "name": "Person",
  "namespace": "example",
  "fields": [
    { "name": "id", "type": "string" },
    { "name": "name", "type": "string" }
  ]
}

这个定义读取excel 后的数据的schema。

image

2.2配置读取文件组件

image
这里只要简单的配置下excel目录路径就可以了

2.3. 配置 UpdateAttribute组件

image

这里配置属性
image

这个shcma.name 和之前配置的 AvroSchemaRegistry 的 需要对应上。

2.4 配置ConvertRecord转换记录组件

这个组件负责将excel 转换成JSON 数据

image

这里主要是配置读取excel,并返回 json 数据格式。

2.5. 配置执行脚本组件( ExecuteGroovyScript)

这个组件的作用是,我希望 我对这个读取的json数据,将id 属性改成我自己生成的。

image

这里配置将ID 配置为uuid。

脚本为:

import groovy.json.JsonSlurper
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.core.JsonGenerator
import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets

def flowFile = session.get()
if (!flowFile) return

try {
    def text = ''
    session.read(flowFile, { inputStream ->
        text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
    } as InputStreamCallback)

    // 用 Groovy 解析 JSON(支持宽松语法)
    def jsonSlurper = new JsonSlurper()
    def jsonArray = jsonSlurper.parseText(text)

    // 为每条记录添加 id: UUID
    jsonArray.each { record ->
        record.id = java.util.UUID.randomUUID().toString()
        // 注意:Groovy 会自动将 "name" 保留在原位置,新增 id 字段
    }

    // 使用 Jackson 序列化,不转义非 ASCII 字符(保留中文)
    def mapper = new ObjectMapper()
    mapper.getFactory().setCodec(mapper)
    // 关键:禁用 Unicode 转义
    def writer = mapper.writer()
        .without(JsonGenerator.Feature.ESCAPE_NON_ASCII)

    def updatedJson = writer.writeValueAsString(jsonArray)

    flowFile = session.write(flowFile, { outputStream ->
        outputStream.write(updatedJson.getBytes(StandardCharsets.UTF_8))
    } as OutputStreamCallback)

    flowFile = session.putAttribute(flowFile, "mime.type", "application/json; charset=utf-8")
    session.transfer(flowFile, REL_SUCCESS)

} catch (Exception e) {
    log.error("处理FlowFile时发生错误", e)
    session.transfer(flowFile, REL_FAILURE)
}

2.6 写入数据库

写入数据库使用 PutDatabaseRecord 组件

image

record reader 需要选择JsonTreeReader,并配置对应的数据库表。

2.7 入库的效果

image