1

コミュニティ、私のメッセージの約 3%HDFSが. JAVA1000 万のメッセージを生成する単純なプロデューサーを作成しました。

public static final String TEST_SCHEMA = "{"
        + "\"type\":\"record\","
        + "\"name\":\"myrecord\","
        + "\"fields\":["
        + "  { \"name\":\"str1\", \"type\":\"string\" },"
        + "  { \"name\":\"str2\", \"type\":\"string\" },"
        + "  { \"name\":\"int1\", \"type\":\"int\" }"
        + "]}";

public KafkaProducerWrapper(String topic) throws UnknownHostException {
    // store topic name
    this.topic = topic;

    // initialize kafka producer
    Properties config = new Properties();
    config.put("client.id", InetAddress.getLocalHost().getHostName());
    config.put("bootstrap.servers", "myserver-1:9092");
    config.put("key.serializer", "io.confluent.kafka.serializers.KafkaAvroSerializer");
    config.put("value.serializer", "io.confluent.kafka.serializers.KafkaAvroSerializer");
    config.put("schema.registry.url", "http://myserver-1:8089");
    config.put("acks", "all");

    producer = new KafkaProducer(config);

    // parse schema
    Schema.Parser parser = new Schema.Parser();
    schema = parser.parse(TEST_SCHEMA);
}

public void send() {
    // generate key
    int key = (int) (Math.random() * 20);

    // generate record
    GenericData.Record r = new GenericData.Record(schema);
    r.put("str1", "text" + key);
    r.put("str2", "text2" + key);
    r.put("int1", key);

    final ProducerRecord<String, GenericRecord> record = new ProducerRecord<>(topic, "K" + key, (GenericRecord) r);
    producer.send(record, new Callback() {
        public void onCompletion(RecordMetadata metadata, Exception e) {
            if (e != null) {
                logger.error("Send failed for record {}", record, e);
                messageErrorCounter++;
                return;
            }
            logger.debug("Send succeeded for record {}", record);
            messageCounter++;
        }
    });
}

public String getStats() { return "Messages sent: " + messageCounter + "/" + messageErrorCounter; }

public long getMessageCounter() {
    return messageCounter + messageErrorCounter;
}

public void close() {
    producer.close();
}

public static void main(String[] args) throws InterruptedException, UnknownHostException {
    // initialize kafka producer
    KafkaProducerWrapper kafkaProducerWrapper = new KafkaProducerWrapper("my-test-topic");

    long max = 10000000L;
    for (long i = 0; i < max; i++) {
        kafkaProducerWrapper.send();
    }

    logger.info("producer-demo sent all messages");
    while (kafkaProducerWrapper.getMessageCounter() < max)
    {
        logger.info(kafkaProducerWrapper.getStats());
        Thread.sleep(2000);
    }

    logger.info(kafkaProducerWrapper.getStats());
    kafkaProducerWrapper.close();
}

そして、スタンドアロン モードで を使用してConfluent HDFS Connectorデータを書き込みますHDFS。構成は次のとおりです。

name=hdfs-consumer-test
connector.class=io.confluent.connect.hdfs.HdfsSinkConnector
tasks.max=1

topics=my-test-topic

hdfs.url=hdfs://my-cluster/kafka-test
hadoop.conf.dir=/etc/hadoop/conf/
flush.size=100000
rotate.interval.ms=20000

# increase timeouts to avoid CommitFailedException
consumer.session.timeout.ms=300000
consumer.request.timeout.ms=310000

heartbeat.interval.ms= 60000
session.timeout.ms= 100000

コネクタはデータを HDFS に書き込みますが、20000 ミリ秒待機した後 (原因によりrotate.interval.ms)、一部のメッセージが受信されません。

scala> spark.read.avro("/kafka-test/topics/my-test-topic/partition=*/my-test-topic*")
  .count()
res0: Long = 9749015

この動作の理由は何ですか?私の間違いはどこですか?Confluent 3.0.1/Kafka 10.0.0.1を使用しています。

4

1 に答える 1