|
37 | 37 | import org.apache.seatunnel.api.table.type.SeaTunnelDataType; |
38 | 38 | import org.apache.seatunnel.api.table.type.SeaTunnelRow; |
39 | 39 | import org.apache.seatunnel.api.table.type.SeaTunnelRowType; |
| 40 | +import org.apache.seatunnel.common.utils.JsonUtils; |
40 | 41 | import org.apache.seatunnel.connectors.seatunnel.kafka.config.MessageFormat; |
41 | 42 | import org.apache.seatunnel.connectors.seatunnel.kafka.serialize.DefaultSeaTunnelRowSerializer; |
42 | 43 | import org.apache.seatunnel.e2e.common.TestResource; |
|
64 | 65 | import org.apache.kafka.common.serialization.ByteArraySerializer; |
65 | 66 | import org.apache.kafka.common.serialization.StringDeserializer; |
66 | 67 |
|
| 68 | +import org.jetbrains.annotations.NotNull; |
67 | 69 | import org.junit.jupiter.api.AfterAll; |
68 | 70 | import org.junit.jupiter.api.Assertions; |
69 | 71 | import org.junit.jupiter.api.BeforeAll; |
@@ -693,30 +695,11 @@ public void testKafkaProtobufToAssert(TestContainer container) |
693 | 695 | ProtobufDeserializationSchema deserializationSchema = |
694 | 696 | new ProtobufDeserializationSchema(catalogTable); |
695 | 697 |
|
696 | | - // Create serializer |
697 | 698 | DefaultSeaTunnelRowSerializer serializer = |
698 | | - DefaultSeaTunnelRowSerializer.create( |
699 | | - "test_protobuf_topic_fake_source", |
700 | | - seaTunnelRowType, |
701 | | - MessageFormat.PROTOBUF, |
702 | | - DEFAULT_FIELD_DELIMITER, |
703 | | - readonlyConfig); |
704 | | - |
705 | | - // Produce records to Kafka |
706 | | - IntStream.range(0, 20) |
707 | | - .forEach( |
708 | | - i -> { |
709 | | - try { |
710 | | - SeaTunnelRow originalRow = buildSeaTunnelRow(); |
711 | | - ProducerRecord<byte[], byte[]> producerRecord = |
712 | | - serializer.serializeRow(originalRow); |
713 | | - producer.send(producerRecord).get(); |
714 | | - } catch (InterruptedException | ExecutionException e) { |
715 | | - throw new RuntimeException("Error sending Kafka message", e); |
716 | | - } |
717 | | - }); |
| 699 | + getDefaultSeaTunnelRowSerializer( |
| 700 | + "test_protobuf_topic_fake_source", seaTunnelRowType, readonlyConfig); |
718 | 701 |
|
719 | | - producer.flush(); |
| 702 | + sendData(serializer); |
720 | 703 |
|
721 | 704 | // Execute the job and validate |
722 | 705 | Container.ExecResult execResult = container.executeJob(confFile); |
@@ -769,6 +752,87 @@ public void testKafkaProtobufToAssert(TestContainer container) |
769 | 752 | }); |
770 | 753 | } |
771 | 754 |
|
| 755 | + private @NotNull DefaultSeaTunnelRowSerializer getDefaultSeaTunnelRowSerializer( |
| 756 | + String topic, SeaTunnelRowType seaTunnelRowType, ReadonlyConfig readonlyConfig) { |
| 757 | + // Create serializer |
| 758 | + DefaultSeaTunnelRowSerializer serializer = |
| 759 | + DefaultSeaTunnelRowSerializer.create( |
| 760 | + topic, |
| 761 | + seaTunnelRowType, |
| 762 | + MessageFormat.PROTOBUF, |
| 763 | + DEFAULT_FIELD_DELIMITER, |
| 764 | + readonlyConfig); |
| 765 | + return serializer; |
| 766 | + } |
| 767 | + |
| 768 | + private void sendData(DefaultSeaTunnelRowSerializer serializer) { |
| 769 | + // Produce records to Kafka |
| 770 | + IntStream.range(0, 20) |
| 771 | + .forEach( |
| 772 | + i -> { |
| 773 | + try { |
| 774 | + SeaTunnelRow originalRow = buildSeaTunnelRow(); |
| 775 | + ProducerRecord<byte[], byte[]> producerRecord = |
| 776 | + serializer.serializeRow(originalRow); |
| 777 | + producer.send(producerRecord).get(); |
| 778 | + } catch (InterruptedException | ExecutionException e) { |
| 779 | + throw new RuntimeException("Error sending Kafka message", e); |
| 780 | + } |
| 781 | + }); |
| 782 | + |
| 783 | + producer.flush(); |
| 784 | + } |
| 785 | + |
| 786 | + @TestTemplate |
| 787 | + public void testKafkaProtobufForTransformToAssert(TestContainer container) |
| 788 | + throws IOException, InterruptedException, URISyntaxException { |
| 789 | + |
| 790 | + String confFile = "/protobuf/kafka_protobuf_transform_to_assert.conf"; |
| 791 | + String path = getTestConfigFile(confFile); |
| 792 | + Config config = ConfigFactory.parseFile(new File(path)); |
| 793 | + Config sinkConfig = config.getConfigList("source").get(0); |
| 794 | + ReadonlyConfig readonlyConfig = ReadonlyConfig.fromConfig(sinkConfig); |
| 795 | + SeaTunnelRowType seaTunnelRowType = buildSeaTunnelRowType(); |
| 796 | + |
| 797 | + // Create serializer |
| 798 | + DefaultSeaTunnelRowSerializer serializer = |
| 799 | + getDefaultSeaTunnelRowSerializer( |
| 800 | + "test_protobuf_topic_transform_fake_source", |
| 801 | + seaTunnelRowType, |
| 802 | + readonlyConfig); |
| 803 | + |
| 804 | + // Produce records to Kafka |
| 805 | + sendData(serializer); |
| 806 | + |
| 807 | + // Execute the job and validate |
| 808 | + Container.ExecResult execResult = container.executeJob(confFile); |
| 809 | + Assertions.assertEquals(0, execResult.getExitCode(), execResult.getStderr()); |
| 810 | + |
| 811 | + try (KafkaConsumer<byte[], byte[]> consumer = |
| 812 | + new KafkaConsumer<>(kafkaByteConsumerConfig())) { |
| 813 | + consumer.subscribe(Arrays.asList("verify_protobuf_transform")); |
| 814 | + Map<TopicPartition, Long> offsets = |
| 815 | + consumer.endOffsets( |
| 816 | + Arrays.asList(new TopicPartition("verify_protobuf_transform", 0))); |
| 817 | + Long endOffset = offsets.entrySet().iterator().next().getValue(); |
| 818 | + Long lastProcessedOffset = -1L; |
| 819 | + |
| 820 | + do { |
| 821 | + ConsumerRecords<byte[], byte[]> records = consumer.poll(Duration.ofMillis(100)); |
| 822 | + for (ConsumerRecord<byte[], byte[]> record : records) { |
| 823 | + if (lastProcessedOffset < record.offset()) { |
| 824 | + String data = new String(record.value(), "UTF-8"); |
| 825 | + ObjectNode jsonNodes = JsonUtils.parseObject(data); |
| 826 | + Assertions.assertEquals(jsonNodes.size(), 2); |
| 827 | + Assertions.assertEquals(jsonNodes.get("city").asText(), "city_value"); |
| 828 | + Assertions.assertEquals(jsonNodes.get("c_string").asText(), "test data"); |
| 829 | + } |
| 830 | + lastProcessedOffset = record.offset(); |
| 831 | + } |
| 832 | + } while (lastProcessedOffset < endOffset - 1); |
| 833 | + } |
| 834 | + } |
| 835 | + |
772 | 836 | public static String getTestConfigFile(String configFile) |
773 | 837 | throws FileNotFoundException, URISyntaxException { |
774 | 838 | URL resource = KafkaIT.class.getResource(configFile); |
|
0 commit comments