当前位置:   article > 正文

kafkaStream_kstream.map((key,value)->{})

kstream.map((key,value)->{})

UserFriendsStream

  1. import org.apache.kafka.clients.consumer.ConsumerConfig;
  2. import org.apache.kafka.common.serialization.Serdes;
  3. import org.apache.kafka.streams.*;
  4. import org.apache.kafka.streams.errors.StreamsException;
  5. import org.apache.kafka.streams.kstream.KStream;
  6. import java.util.ArrayList;
  7. import java.util.Properties;
  8. import java.util.concurrent.CountDownLatch;
  9. /**
  10. * 2021.12.28
  11. * user , friends topic:user_friends_row
  12. * 3197468391,1346449342 387324416 4226080662
  13. *
  14. * user , friends topic:user_friends
  15. * 3197468391 1346449342
  16. * 3197468391 387324416
  17. * 3197468391 4226080662
  18. *
  19. */
  20. public class UserFriendsStream {
  21. public static void main(String[] args) {
  22. Properties prop = new Properties();
  23. prop.put(StreamsConfig.APPLICATION_ID_CONFIG,"userFriend");
  24. prop.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.111.131:9092");
  25. prop.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG,3000);
  26. prop.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");
  27. //earliest latest none
  28. prop.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
  29. prop.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
  30. prop.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG,Serdes.String().getClass());
  31. StreamsBuilder builder = new StreamsBuilder();
  32. KStream<String, String> user_friends_rows = builder.stream("user_friends_rows")
  33. .flatMap((key, value) -> { //3197468391,1346449342 387324416 4226080662
  34. ArrayList<KeyValue<String, String>> list = new ArrayList<>();
  35. String[] fields = value.toString().split(","); //[3197468391,1346449342 387324416 4226080662]
  36. if (fields.length == 2) {
  37. String[] friends = fields[1].split("\\s+"); //[1346449342 387324416 4226080662]
  38. String user = fields[0]; // 3197468391
  39. if (user.trim().length() > 0) { // \\s匹配任意空白字符
  40. for (String friend :
  41. friends) {
  42. System.out.println(user + "," + friend);
  43. KeyValue<String, String> keyValue = new KeyValue<>(null, user + "," + friend);
  44. list.add(keyValue);
  45. }
  46. }
  47. }
  48. return list;
  49. });
  50. user_friends_rows.to("user_friends");
  51. //构建 Topology
  52. Topology topo = builder.build();
  53. final KafkaStreams streams = new KafkaStreams(topo, prop);
  54. final CountDownLatch latch = new CountDownLatch(1);
  55. Runtime.getRuntime().addShutdownHook(new Thread("stream"){
  56. @Override
  57. public void run() {
  58. streams.close();
  59. latch.countDown();
  60. }
  61. });
  62. try {
  63. streams.start();
  64. latch.countDown();
  65. }catch (IllegalStateException e){
  66. e.printStackTrace();
  67. }catch (StreamsException e){
  68. e.printStackTrace();
  69. }
  70. // System.exit(0);
  71. }
  72. }

 EventAttendStream

  1. import org.apache.kafka.clients.consumer.ConsumerConfig;
  2. import org.apache.kafka.common.serialization.Serdes;
  3. import org.apache.kafka.streams.*;
  4. import org.apache.kafka.streams.errors.StreamsException;
  5. import org.apache.kafka.streams.kstream.KStream;
  6. import java.util.ArrayList;
  7. import java.util.Properties;
  8. import java.util.concurrent.CountDownLatch;
  9. //2021.12.28
  10. public class EventAttendStream {
  11. public static void main(String[] args) {
  12. Properties prop = new Properties();
  13. prop.put(StreamsConfig.APPLICATION_ID_CONFIG,"eventattend");
  14. prop.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.111.131:9092");
  15. prop.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
  16. prop.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG,Serdes.String().getClass());
  17. prop.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG,3000);
  18. prop.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false"); //自动提交
  19. //earliest latest none
  20. prop.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
  21. StreamsBuilder builder = new StreamsBuilder();
  22. KStream<Object, Object> ear = builder.stream("event_attendess_row"); //流
  23. ear.flatMap((key,value)->{
  24. ArrayList<KeyValue<String,String>>list=new ArrayList<>();
  25. String[] fields = value.toString().split(",");
  26. String eventid = fields[0];
  27. //yes
  28. if (fields.length>=2 && fields[1].trim().length()>0){
  29. String[]yes=fields[1].trim().split("\\s+");
  30. for (String y:
  31. yes){
  32. System.out.println(eventid+","+y+",yes");
  33. KeyValue<String, String> yesKeyValue = new KeyValue<>(null, eventid + "," + y + ",yes");
  34. list.add(yesKeyValue);
  35. }
  36. }
  37. //maybe
  38. if (fields.length>=3 && fields[2].trim().length()>0){
  39. String[]maybe=fields[2].trim().split("\\s+");
  40. for (String mb:
  41. maybe){
  42. System.out.println(eventid+","+mb+",maybe");
  43. KeyValue<String, String> mbKeyValue = new KeyValue<>(null, eventid + "," + mb + ",maybe");
  44. list.add(mbKeyValue);
  45. }
  46. }
  47. //invited
  48. if (fields.length>=4 && fields[3].trim().length()>0){
  49. String[]invited=fields[3].trim().split("\\s+");
  50. for (String i:
  51. invited){
  52. System.out.println(eventid+","+i+",invited");
  53. KeyValue<String, String> invitedKeyValue = new KeyValue<>(null, eventid + "," + i + ",invited");
  54. list.add(invitedKeyValue);
  55. }
  56. }
  57. //no
  58. if (fields.length>=5 && fields[4].trim().length()>0){
  59. String[]nos=fields[4].trim().split("\\s+");
  60. for (String no:
  61. nos){
  62. System.out.println(eventid+","+no+",no");
  63. KeyValue<String, String> noKeyValue = new KeyValue<>(null, eventid + "," + no + ",no");
  64. list.add(noKeyValue);
  65. }
  66. }
  67. return list;
  68. }).to("event_attendess");
  69. Topology topo = builder.build();
  70. final KafkaStreams streams = new KafkaStreams(topo, prop);
  71. final CountDownLatch latch = new CountDownLatch(1);
  72. Runtime.getRuntime().addShutdownHook(new Thread("stream"){
  73. @Override
  74. public void run() {
  75. streams.close();
  76. latch.countDown();
  77. }
  78. });
  79. try {
  80. streams.start();
  81. latch.await();
  82. } catch (IllegalStateException e) {
  83. e.printStackTrace();
  84. } catch (StreamsException e) {
  85. e.printStackTrace();
  86. } catch (InterruptedException e) {
  87. e.printStackTrace();
  88. }
  89. System.exit(0);
  90. }
  91. }

 MyStreamTest

  1. import org.apache.kafka.clients.consumer.ConsumerConfig;
  2. import org.apache.kafka.common.serialization.Serdes;
  3. import org.apache.kafka.streams.KafkaStreams;
  4. import org.apache.kafka.streams.StreamsBuilder;
  5. import org.apache.kafka.streams.StreamsConfig;
  6. import org.apache.kafka.streams.Topology;
  7. import org.apache.kafka.streams.errors.StreamsException;
  8. import org.apache.kafka.streams.kstream.KStream;
  9. import java.util.Properties;
  10. import java.util.concurrent.CountDownLatch;
  11. public class MyStreamTest {
  12. public static void main(String[] args) {
  13. Properties prop = new Properties();
  14. prop.put(StreamsConfig.APPLICATION_ID_CONFIG, "myStream");
  15. prop.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.111.131:9092");
  16. prop.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 3000);
  17. prop.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");
  18. //earliest latest none
  19. prop.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
  20. prop.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
  21. prop.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());
  22. StreamsBuilder builder = new StreamsBuilder();
  23. KStream<Object, Object> in = builder.stream("mystreamin");
  24. in.to("mystreamout");
  25. Topology topo = builder.build();
  26. final KafkaStreams streams = new KafkaStreams(topo, prop);
  27. final CountDownLatch latch = new CountDownLatch(1);
  28. Runtime.getRuntime().addShutdownHook(new Thread("stream"){
  29. @Override
  30. public void run() {
  31. streams.close();
  32. latch.countDown();
  33. }
  34. });
  35. try {
  36. streams.start();
  37. latch.await();
  38. } catch (IllegalStateException e) {
  39. e.printStackTrace();
  40. } catch (StreamsException e) {
  41. e.printStackTrace();
  42. } catch (InterruptedException e) {
  43. e.printStackTrace();
  44. }
  45. System.exit(0);
  46. }
  47. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/749784
推荐阅读