[ https://issues.apache.org/jira/browse/BEAM-7896?focusedWorklogId=291547&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-291547 ]
ASF GitHub Bot logged work on BEAM-7896: ---------------------------------------- Author: ASF GitHub Bot Created on: 08/Aug/19 20:04 Start Date: 08/Aug/19 20:04 Worklog Time Spent: 10m Work Description: akedin commented on pull request #9298: [BEAM-7896] Implementing RateEstimation for KafkaTable URL: https://github.com/apache/beam/pull/9298#discussion_r312216682 ########## File path: sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/kafka/BeamKafkaTable.java ########## @@ -138,6 +161,96 @@ public String getBootstrapServers() { @Override public BeamTableStatistics getTableStatistics(PipelineOptions options) { - return BeamTableStatistics.UNBOUNDED_UNKNOWN; + if (rowCountStatistics == null) { + try { + rowCountStatistics = + BeamTableStatistics.createUnboundedTableStatistics( + this.computeRate(numberOfRecordsForRate)); + } catch (Exception e) { + LOGGER.warn("Could not get the row count for the topics " + getTopics(), e); + rowCountStatistics = BeamTableStatistics.UNBOUNDED_UNKNOWN; + } + } + + return rowCountStatistics; + } + + /** + * This method returns the estimate of the computeRate for this table using last numberOfRecords + * tuples in each partition. + */ + double computeRate(int numberOfRecords) throws NoEstimationException { + Properties props = new Properties(); + + props.put("bootstrap.servers", bootstrapServers); + props.put("session.timeout.ms", "30000"); + props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); + props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); + + KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props); + + return computeRate(consumer, numberOfRecords); + } + + double computeRate(Consumer consumer, int numberOfRecordsToCheck) throws NoEstimationException { + + List<TopicPartition> topars = new ArrayList<>(); + for (String name : getTopics()) { + List<PartitionInfo> parInfList = consumer.partitionsFor(name); + topars.addAll( + parInfList.stream() + .map(parInf -> new TopicPartition(name, parInf.partition())) + .collect(Collectors.toList())); + } + + consumer.assign(topars); + Map<TopicPartition, Long> offsets = consumer.endOffsets(topars); Review comment: Add a comment what happens with the consumer and topic, what are the offsets ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 291547) Time Spent: 50m (was: 40m) > Rate estimation for Kafka Table > ------------------------------- > > Key: BEAM-7896 > URL: https://issues.apache.org/jira/browse/BEAM-7896 > Project: Beam > Issue Type: New Feature > Components: dsl-sql > Reporter: Alireza Samadianzakaria > Assignee: Alireza Samadianzakaria > Priority: Major > Time Spent: 50m > Remaining Estimate: 0h > > Currently, KafkaTable returns UNKNOWN statistics for its rate. > We can use previously arrived tuples to estimate the rate and return correct > statistics (See > [https://docs.google.com/document/d/1vi1PBBu5IqSy-qZl1Gk-49CcANOpbNs1UAud6LnOaiY|https://docs.google.com/document/d/1vi1PBBu5IqSy-qZl1Gk-49CcANOpbNs1UAud6LnOaiY/]) > -- This message was sent by Atlassian JIRA (v7.6.14#76016)