kafka批量写mysql_Flink 读取 Kafka 批量写入到 MySQL 0444

本文介绍了一个使用Java实现的Kafka模拟数据生成程序。该程序通过创建Employee实体类并利用Gson进行JSON序列化,每秒将一条员工数据发送到Kafka主题中。数据包含员工ID、姓名、密码、年龄、薪资及部门等属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模拟数据生成代码

新建一个employee实体类

package com.zuoan;

public class Employee {

public int id;

public String name;

public String password;

public int age;

public Integer salary;

public String department;

public Employee(int id, String name, String password, int age, Integer salary, String department) {

this.id = id;

this.name = name;

this.password = password;

this.age = age;

this.salary = salary;

this.department = department;

}

public Employee() {

}

@Override

public String toString() {

return "Employee{" +

"id=" + id +

", name='" + name + '\'' +

", password='" + password + '\'' +

", age=" + age +

", salary=" + salary +

", department='" + department + '\'' +

'}';

}

public Integer getSalary() {

return salary;

}

public void setSalary(Integer salary) {

this.salary = salary;

}

public String getDepartment() {

return department;

}

public void setDepartment(String department) {

this.department = department;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}

模拟生成数据,这里是每秒生成一条数据到kafka

package com.zuoan;

import com.google.gson.Gson;

import org.apache.kafka.clients.producer.ProducerRecord;

import org.apache.kafka.clients.producer.KafkaProducer;

import java.util.Properties;

import java.util.Random;

public class KafkaProducerTest {

public static void main(String[] args) {

Producer();

}

public static void Producer() {

String broker = "localhost:9092";

String topic = "demo";

Properties props = new Properties();

props.put("bootstrap.servers", broker);

props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");

props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

KafkaProducer producer = new KafkaProducer(props);

String[] depLists = new String[5];

depLists[0] = "行政部";

depLists[1] = "账务部";

depLists[2] = "市场部";

depLists[3] = "技术部";

depLists[4] = "销售部";

Random rand = new Random(300);

Gson gson = new Gson();

for (int i = 1; i <= 1000; i++) {

Employee employee = new Employee(i, "user" + i, "password" + i, rand.nextInt(40) + 20, (rand.nextInt(300) + 1) * 100, depLists[rand.nextInt(5)]);

String temp = gson.toJson(employee).toString();

ProducerRecord record = new ProducerRecord(topic, null, "user" + i, temp);

producer.send(record);

System.out.println("发送数据: " + temp);

try {

Thread.sleep(1 * 1000); //发送一条数据 sleep

} catch (InterruptedException e) {

e.printStackTrace();

}

}

producer.flush();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值