读文件放到kafka

package com.pb;
import com.pb.common.Executable;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
public class ReadCSV implements Executable {
///读csv文件打到kafka的topic
@Override
public void execute(String …parameters) {
// public void execute(String[] parameters) {
Properties props = new Properties(); props.put(“bootstrap.servers”,ReadUtil.getProperties(“brokerUrl”));
props.put(“acks”, ReadUtil.getProperties(“acks”));
props.put(“retries”, ReadUtil.getProperties(“retries”));
props.put(“batch.size”, ReadUtil.getProperties(“batch.size”));
props.put(“linger.ms”, ReadUtil.getProperties(“linger.ms”));
props.put(“buffer.memory”, ReadUtil.getProperties(“buffer.memory”));
props.put(“key.serializer”, ReadUtil.getProperties(“key.serializer”));
props.put(“value.serializer”, ReadUtil.getProperties(“value.serializer”));
FileInputStream csvFile = null;
InputStreamReader is = null;
BufferedReader br = null;
Producer<String, String> producer = new KafkaProducer<String, String>(props);
try {
csvFile = new FileInputStream(“users.csv”);
is = new InputStreamReader(csvFile);
br = new BufferedReader(is);
String line = br.readLine();
Long position = 0L;
int i = 0;
while (line != null) {
position += line.length();
producer.send(new ProducerRecord<String, String>(“streams-plaintext-input”, Long.toString(position), line));
line = br.readLine();
i++;
}
System.out.println(i);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvFile.close();
is.close();
br.close();
producer.flush();
producer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}

驱动
main

package com.pb.main;
import com.pb.common.Executable;
import java.util.Arrays;
public class Start {
public static void main(String[] args) throws Exception{
//public void run(String… args) {
String className=args[0];
//
String[] parameters= Arrays.copyOfRange(args,1,args.length);
//
Object object=Class.forName(className).newInstance();
if(object instanceof Executable){
((Executable) object).execute();
// }
for(String str :parameters){
System.out.println(str);
}
}
}
}

接口

package com.pb.common;
public interface Executable {
// void execute(String[] parameters) throws Exception;
//void execute() throws Exception;
void execute(String …parameters) throws Exception;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值