NIFI使用

1 从Kafka接收消息,存储到数据库中。

(1) ConsumerKafka processor

(2)Execute Scripts Processor

我这里是使用JS脚本进行处理。 还有很多其他语言的脚本。

javascript 复制代码
var flowFile = session.get();
if (flowFile != null) {
   var IOUtils = Java.type("org.apache.commons.io.IOUtils");
   var StreamCallback = Java.type("org.apache.nifi.processor.io.StreamCallback");
   var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");
   var DateFormatUtils=Java.type("org.apache.commons.lang3.time.DateFormatUtils");

   // var dataType=flowFile.getAttribute('data_type')
   // var FLAG=flowFile.getAttribute('flag')
   var tm = null;
   try {
      flowFile = session.write(flowFile, new StreamCallback(function (inputStream, outputStream) {
         var inputText = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
         var msg = JSON.parse(inputText);
         var stationId = msg['stationId'];
         var stationName = msg['stationName'];
         var deviceId = msg['deviceId'];
         var deviceName = msg['deviceName'];
         var deviceNo = msg['deviceNo'];
		 var receiveType = msg['receiveType'];
         var createAt = msg['createAt'];
         var createAtString=DateFormatUtils.format(Number(createAt),'yyyy-MM-dd HH:mm:ss');
		 var obTime = msg['obTime'];
		 var obDate = msg['obDate'];
         var obDateString=DateFormatUtils.format(Number(obDate),'yyyy-MM-dd HH:mm:ss');
		 var order = msg['order'];
		 var distance = msg['distance'];
         var channel1SignalStrength = msg['channel1SignalStrength']
		 var powerVoltage = msg['powerVoltage']
		 
		 var sql = 'insert into "SJZT_ODS"."water_data_distance"('
		 + '"station_id", "station_name", "device_id", "device_name", "device_no", "receive_type", "create_at", "ob_time", "ob_date", "order", "distance", "channel1_signal_strength", "power_voltage")'
		 + 'VALUES('
		 + stationId + ', \'' + stationName + '\', ' + deviceId + ', \'' + deviceName + '\', \'' + deviceNo + '\', ' + receiveType + ', \'' + createAtString + '\', \'' + obTime + '\', \'' + obDateString + '\', ' + order + ', ' + distance + ', ' + channel1SignalStrength + ', ' + powerVoltage 
		 + ')';
         outputStream.write(sql.getBytes(StandardCharsets.UTF_8));
      }));

      // flowFile = session.putAttribute(flowFile, "tm",tableName);
      session.transfer(flowFile, REL_SUCCESS);
   } catch (e) {
      flowFile = session.putAttribute(flowFile, "rsvr.transfer.error", e);
      session.transfer(flowFile, REL_FAILURE);
   }
}

注意: 这里只是生成了一个sql字符串,并没有执行sql,因此需要后面的processor来执行sql语句。

(3)PutSql processor

注意:autocommit要设置为true,否则看不到数据库里面的数据的。

2 将一堆Processors移动到一个Group里面界面操作

貌似没有直接的移动操作。

(1) Ctrl + A 全选要移动的processors

(2) 点击左边的group按钮

(3)为新的Group命名

(4)好了。选中的所有的processors都移动到了自己新创建的group中了。

参考材料

1\] https://blog.csdn.net/guijianchouxyz/article/details/120340154

相关推荐
武昌库里写JAVA6 分钟前
【MySQL】MySQL数据库如何改名
java·vue.js·spring boot·sql·学习
花落人散处1 小时前
SpringAI——接入高德MCP服务
java·后端
超浪的晨1 小时前
Java 代理机制详解:从静态代理到动态代理,彻底掌握代理模式的原理与实战
java·开发语言·后端·学习·代理模式·个人开发
天天摸鱼的java工程师1 小时前
🧠 MySQL 索引结构有哪些?优缺点是什么?【原理 + 场景实战】
java·后端·面试
java叶新东老师1 小时前
idea提交时忽略.class、.iml文件和文件夹或目录的方法
java·开发语言
飞翔的佩奇1 小时前
Java项目:基于SSM框架实现的社区团购管理系统【ssm+B/S架构+源码+数据库+毕业论文+答辩PPT+远程部署】
java·数据库·vue.js·毕业设计·mybatis·答辩ppt·社区团购
TDengine (老段)2 小时前
TDengine 转化函数 TO_TIMESTAMP 用户手册
java·大数据·数据库·物联网·时序数据库·tdengine·涛思数据
Warren982 小时前
Java Collections工具类
java·开发语言·笔记·python·学习·oracle·硬件工程
java叶新东老师2 小时前
CMakelists.txt 实现多级目录编译
java·服务器·数据库
_风不会停息2 小时前
JDK1.8升级 JDK21 实践踩坑
java