Simple and practical of kafka

First, maven imports the kafka package:
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.10.0.0</version>
</dependency>


Come to Producter first By:
public static void main(String[] args) {
        // kafka-like arrangement
		Properties props = new Properties();
		props.put("bootstrap.servers", "localhost:9092");
		props.put("acks", "all");
		props.put("retries", 0);
		props.put("batch.size", 16384);
		props.put("linger.ms", 1);
		props.put("buffer.memory", 33554432);
		//The serialization method of key
		props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
		//The serialization method of value is generally json method, here I wrote an object serialization method myself
		props.put("value.serializer", "com.zfsy.syyx.kafka.MySeri");
		
		Producer<String, RoleBean> producer = new KafkaProducer<String, RoleBean>(props);
		RoleBean bean = new RoleBean();
		bean.setDm("test");
		bean.setMc("Test 2");
		//topic is linlin
		ProducerRecord<String, RoleBean> record = new ProducerRecord<String, RoleBean>("linlin", "test03", bean);
		//Send to kafka client
		Future<RecordMetadata> future = producer.send(record);
		try {
			System.out.println(future.get().partition());
			
			
			
		} catch (InterruptedException e) {
			e.printStackTrace ();
		} catch (ExecutionException e) {
			e.printStackTrace ();
		}
	}



Then there is the consumer:
// consumer configuration
			Properties props2 = new Properties();
			props2.put("bootstrap.servers", "localhost:9092");
			props2.put("group.id", "test");
			props2.put("enable.auto.commit", "false");
			props2.put("auto.commit.interval.ms", "1000");
			props2.put("session.timeout.ms", "30000");
			//key deserialization method
			props2.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
			//The deserialization method of value
			props2.put("value.deserializer", "com.zfsy.syyx.kafka.MySeri");
			Consumer<String, RoleBean> consumer = new KafkaConsumer<String, RoleBean>(props2);
			//topic is linlin
			consumer.subscribe(Lists.newArrayList("linlin"));
			// keep getting
			while(true){
				ConsumerRecords<String, RoleBean> recoreds = consumer.poll(1000);
				Iterator<ConsumerRecord<String, RoleBean>> iterator = recoreds.iterator();
				while(iterator.hasNext()){
					ConsumerRecord<String, RoleBean> record2 = iterator.next();
					System.out.println(record2.value().getDm());
					System.out.println(record2.value().getMc());
				}
				
			}
			
		}



MySeri source code:
public class MySeri implements Deserializer<Object>, Serializer<Object> {

	@Override
	public void configure(Map<String, ?> configs, boolean isKey) {
		// TODO Auto-generated method stub

	}

	@Override
	public Object deserialize(String topic, byte[] data) {
		ByteArrayInputStream in = new ByteArrayInputStream(data);
		try {
			ObjectInputStream objectInputStream = new ObjectInputStream(in);
			return objectInputStream.readObject();
		} catch (Exception e) {
			e.printStackTrace ();
		}
		return null;
	}

	@Override
	public void close() {
		// TODO Auto-generated method stub

	}

	@Override
	public byte[] serialize(String topic, Object data) {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			ObjectOutputStream objectOutputStream = new ObjectOutputStream(out);
			objectOutputStream.writeObject(data);
			return out.toByteArray();
		} catch (IOException e) {
			e.printStackTrace ();
		}
		return null;
	}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326567434&siteId=291194637