Java读文件写入kafka
Java读文件写入kafka
文件格式
840271 103208 0 0.0 insert 84e66588-8875-4411-9cc6-0ac8302408bf 3 2 4 wangxiao 0.0 0 0.0 9927525 1619330049000 normal 1bd221d7380546be9fe8e10a63cf8130 0 0 NULL 0 0 Qw== 4253976 79
840271 103208 0 0.0 insert cece91f8-8a17-4417-84d8-f6293849e187 3 2 4 wangxiao 0.0 0 0.0 9927525 1619330049000 normal 38204d736e8646fd956131409fc4196e 0 0 NULL 0 0 Qw== 4002760 80
pom依赖
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.11.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<!--编译的文件目录-->
<sourceDirectory>src/main/scala</sourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<!-- build-helper-maven-plugin, 设置多个源文件夹 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/java</source>
<source>src/main/scala</source>
<!-- 我们可以通过在这里添加多个source节点,来添加任意多个源文件夹 -->
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.xueersi.bdc.flink.WordCount</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Java Compiler -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!--Scala Compiler-->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
java代码
import com.alibaba.fastjson.JSON;
import com.bdc.flink.slove_problem.Ans5;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
/**
* @description: 读取D2数据(场景5)写入kafka
* @author: HaoWu
* @create: 2021年04月26日
*/
public class D2ToKafka {
public static void main(String[] args) throws IOException, InterruptedException {
// String bootstrap_servers = "10.90.XXXX:9092,10.90.XXXX:9092,10.90.XXXX:9092"; // 输出kafak路径
// String topic = "test20585696test"; //输出topic
// String path = "/Users/haowu/software/d2_test";
String bootstrap_servers= args[0]; // 输出kafak路径
String topic=args[1]; //输出topic
String path = args[2]; //输入文件路径
Properties props = new Properties();
props.put("bootstrap.servers", bootstrap_servers);//maxwell 测试kafka集群
props.put("acks", "all");
props.put("retries", 1);//重试次数
props.put("batch.size", 16384);//批次大小
props.put("linger.ms", 1);//等待时间
props.put("buffer.memory", 33554432);//RecordAccumulator缓冲区大小
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer<>(props);
readTxt2Json(path, producer, topic);
}
public static void readTxt2Json(String path, Producer producer, String topic) throws IOException, InterruptedException {
File file = new File(path);
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
isr = new InputStreamReader(fis, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
br = new BufferedReader(isr);
String line = null;
System.out.println("================== start ===================:" + System.currentTimeMillis());
while ((line = br.readLine()) != null) {
Ans5 ans5 = str2JsonStr(line);
String key = ans5.getStu_id();
String value = JSON.toJSONString(ans5);
System.out.println(value);
// 写入kafka
producer.send(new ProducerRecord<>(topic, key, value));
}
//System.out.println(jsonStr);
//关闭produce
producer.close();
System.out.println("================== end ===================:" + System.currentTimeMillis());
}
/**
* 构建场景5作答bean,字符串转json字符
*
* @param str
* @return
*/
public static Ans5 str2JsonStr(String str) {
String[] datas = str.split("\t");
D2D3Bean bean = new D2D3Bean(datas[0], datas[1], datas[2], datas[3], datas[4], datas[5]
, datas[6], datas[7], datas[8], datas[9], datas[10]
, datas[11], datas[12], datas[13], datas[14], datas[15]
, datas[16], datas[17], datas[18], datas[19], datas[20], datas[21], datas[22]
, datas[23], datas[24], datas[25], datas[26]);
return new Ans5(bean.getStu_id(), bean.getCourse_id(), bean.getPlan_id(), bean.getQues_id(), bean.getUser_answer(), bean.getAnswer_duration(),
fromTimestampToHour(bean.getSub_time()), bean.getAnswer_status(), bean.getUuid(), bean.getOperate_type(), bean.getAns_scene(), bean.getRecom_id(), bean.getGrade_id(),
bean.getSubject_id(), bean.getOrg_code(), bean.getQue_score(), bean.getStu_score(), bean.getScene_code(), bean.getQue_sort(), bean.getTest_category(), bean.getExam_id(), bean.getTest_num()
);
}
/**
* 毫秒时间戳->yyyy-MM-dd HH:mm:ss
* @param ts
* @return
*/
public static String fromTimestampToHour(String ts){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(Long.valueOf(ts));
return simpleDateFormat.format(date);
}
}
Java读文件写入kafka的更多相关文章
- hdfs文件写入kafka集群
1. 场景描述 因新增Kafka集群,需要将hdfs文件写入到新增的Kafka集群中,后来发现文件不多,就直接下载文件到本地,通过Main函数写入了,假如需要部署到服务器上执行,需将文件读取这块稍做修 ...
- java 读文件 解析
[Java]读取文件方法大全 1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile { /** ...
- Java读文件
public class ReadFromFile { /** * 以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件. */ public static void readFileB ...
- Java读文件夹
使用JAVA读取文件夹中的多个文件 package hx.ReadFile; import java.io.FileNotFoundException; import java.io.IOExcept ...
- java读文件的几个类
链接地址:http://blog.sina.com.cn/s/blog_407a68fc0100f628.html 最初Java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Wri ...
- java 读文件路径问题
文件路径:右键点击src新建Source Folder,创建结果与src目录同级. C:\Users\lenovo\workspace\timedTask\config\userinfo.proper ...
- java创建文件写入内容,并实现下载该文件
public void getText(){ response.setHeader("Content-Disposition", "attachment;filename ...
- JAVA读文件和写文件的的代码模版
有的时候经常为真么读写文件最合理发愁,因为JAVA提过读写文件的方式太多了(C更甚至,fopen & open又有多少人傻傻分不去,更别说ReadFile了). 这里个人绝对比较好的写法,仅供 ...
- spark读文件写入mysql(scala版本)
package com.zjlantone.hive import java.util.Properties import com.zjlantone.hive.SparkOperaterHive.s ...
随机推荐
- 数组中出现次数超过一半的数字 牛客网 剑指Offer
数组中出现次数超过一半的数字 牛客网 剑指Offer 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字 ...
- CentOS7自动备份oracle数据库
1.环境 操作系统:CentOS 7 数据库:11.2.0.1.0 2.登录服务器 切换oracle用户,备份需要在oracle用户下进行 #su - oracle 在oracle家目录下创建bin目 ...
- Vue 基础自查——条件渲染和列表渲染
v-if和v-show的区别是什么? v-if和v-for为什么不能一起用? v-for中的key有什么作用? 1 v-if 和 v-show 1.1 作用 都用来控制元素的显示和隐藏 1.2 控制元 ...
- typedef的用法 单向链表的查找、增加、删除、销毁。
一:typedef的用法. 写一个数据结构(计算机存储数据的一种方式,是抽象的,可以人为组织,提高算法效率),我们需要注意:接口友好,模块化,规范命名等方面,在接口友好方面,typedef是非常 ...
- 『学了就忘』Linux基础命令 — 31、grep命令和通配符
目录 1.grep命令介绍 2.find命令和grep命令的区别(重点) (1)find命令 (2)grep命令 3.通配符与正则表达式的区别 (1)通配符: (2)正则表达式: 1.grep命令介绍 ...
- [LINUX] Arch Linux 硬盘拷贝式装系统+新增 home 分区
目录 前言 1. 实操 1.1 整个磁盘拷贝 1.2 创建 home 分区 1.3 修改 fstab 实现自动挂载 2. 涉及到的知识点 2.1 fstab 2.2 dd 命令 2.3 fdisk 命 ...
- Jquery的常用使用方法
1.获取单个checkbox选中项(三种写法)$("input:checkbox:checked").val()或者$("input:[type='checkbox']: ...
- 问题 F: 背包问题
题目描述 现在有很多物品(它们是可以分割的),我们知道它们每个物品的单位重量的价值v和重量w(1<=v,w<=10):如果给你一个背包它能容纳的重量为m(10<=m<=20), ...
- 论文翻译:2020_Densely connected neural network with dilated convolutions for real-time speech enhancement in the time domain
提出了模型和损失函数 论文名称:扩展卷积密集连接神经网络用于时域实时语音增强 论文代码:https://github.com/ashutosh620/DDAEC 引用:Pandey A, Wang D ...
- Date相关类
Date相关类 SimpleDateFormat类中format()和parse()方法 parse 字符串 --> 日期 format 日期 --> 字符串 Date类中getTime( ...