Skip to main content



Apache Kafka: Low Level api Consumer

If you want greater control over partition consumption then High Level api consumer you may implement it in low level api. It will require to do more work that is not required in consumer group, like:

  • Keeping track of offset where consumer left consumption.
  • Identify lead Broker and adjust with Broker leader changes.
Steps for implementing :

  • Find an active Broker and find out which Broker is the leader for your topic and partition
  • Determine who the replica Brokers are for your topic and partition
  • Build the request defining what data you are interested in
  • Fetch the data
  • Identify and recover from leader changes
package learning.kafka;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import kafka.api.FetchRequestBuilder;
import kafka.api.PartitionOffsetRequestInfo;
import kafka.cluster.Broker;
import kafka.common.ErrorMapping;
import kafka.common.TopicAndPartition;
import kafka.javaapi.FetchResponse;
import kafka.javaapi.OffsetRequest;
import kafka.javaapi.OffsetResponse;
import kafka.javaapi.PartitionMetadata;
import kafka.javaapi.TopicMetadata;
import kafka.javaapi.TopicMetadataRequest;
import kafka.javaapi.TopicMetadataResponse;
import kafka.javaapi.consumer.SimpleConsumer;
import kafka.message.MessageAndOffset;

public class SimpleExample {

       public static void main(String[] args) {
              SimpleExample example = new SimpleExample();

              // max messages to read
              long maxReads = Long.parseLong(args[0]);

              String topic = args[1];
              int partition = Integer.parseInt(args[2]);

              List<String> seeds = new ArrayList<String>();
              // broker to detrmine lead
              seeds.add(args[3]);

              // port
              int port = Integer.parseInt(args[4]);

              try {
                     example.run(maxReads, topic, partition, seeds, port);
              } catch (Exception e) {
                     System.out.println("Oops:" + e);
                     e.printStackTrace();
              }
       }
      
      
       public void run(long maxReads, String topic, int partition, List<String> seeds, int port) throws Exception {
              PartitionMetadata metadata = findLeader(seeds, port, topic, partition);
             
              if (metadata == null) {
                   System.err.println("Can't find metadata for Topic and Partition. Exiting");
                   return;
            }
             
              if(metadata.leader() ==null){
                       System.err.println("Can't find Leader for Topic and Partition. Exiting");
                 return;
              }
             
              String leadBroker = metadata.leader().host();
              String clientName = "Client_" + topic + "_" + partition;
             
              SimpleConsumer consumer = new SimpleConsumer(leadBroker, port, 100000, 64 * 1024, clientName);
              long readoffset = getLastOffset(consumer, topic, partition, kafka.api.OffsetRequest.EarliestTime(), clientName);
             
              int numErrors = 0;
             
              while(maxReads > 0){
                     if (consumer == null) {
                consumer = new SimpleConsumer(leadBroker, port, 100000, 64 * 1024, clientName);
            }
                    
                     kafka.api.FetchRequest req = new FetchRequestBuilder().clientId(clientName)
                                  .addFetch(topic, partition, readoffset, 100000)
                                  .build();
                    
                     FetchResponse fetchResponse = consumer.fetch(req);
                    
                     if (fetchResponse.hasError()) {
                            numErrors++;
                           // Something went wrong!
                            short code = fetchResponse.errorCode(topic, partition);
                            System.out.println("Error fetching data from the Broker:" + leadBroker + " Reason: " + code);
                            if (numErrors > 5) break;
                            if (code == ErrorMapping.OffsetOutOfRangeCode())  {
                                  // We asked for an invalid offset. For simple case ask for the last element to reset
                                   readoffset = getLastOffset(consumer,topic, partition, kafka.api.OffsetRequest.LatestTime(), clientName);
                        continue;
                            }
                            consumer.close();
                    consumer = null;
                    leadBroker = findNewLeader(leadBroker, topic, partition, port);
                    continue;
                     }
                     numErrors = 0;
                     long numRead = 0;
                    
                     for(MessageAndOffset messageAndOffset : fetchResponse.messageSet(topic, partition)){
                           long currentOffset = messageAndOffset.offset();
                           // Safety check: It may be case we get old messages so just discard them (like in case of compression)
                           if(currentOffset< readoffset){
                                  System.out.println("Found an old offset: " + currentOffset + " Expecting: " + readoffset);
                    continue;
                           }
                           readoffset = messageAndOffset.nextOffset();
                           ByteBuffer payload = messageAndOffset.message().payload();
                          
                           byte[] bytes = new byte[payload.limit()];
                           payload.get(bytes);
                            System.out.println(String.valueOf(messageAndOffset.offset()) + ": " + new String(bytes, "UTF-8"));
                numRead++;
                maxReads--;
                     }
                    
                     if (numRead == 0) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ie) {
                }
            }
                    
                    
              }
              if (consumer != null) consumer.close();
       }


       private List<String> m_replicaBrokers = new ArrayList<String>();

       private PartitionMetadata findLeader(List<String> a_seedBrokers,
                     int a_port, String a_topic, int a_partition) {

              PartitionMetadata returnMetaData = null;

              loop: for (String seed : a_seedBrokers) {
                     SimpleConsumer consumer = null;
                     try {
                           consumer = new SimpleConsumer(seed, a_port, 100000, 64 * 1024,
                                         "leaderLookup");

                           List<String> topics = Collections.singletonList(a_topic);
                           TopicMetadataRequest req = new TopicMetadataRequest(topics);
                           TopicMetadataResponse resp = consumer.send(req);
                           List<TopicMetadata> metaData = resp.topicsMetadata();

                           for (TopicMetadata item : metaData) {
                                  for (PartitionMetadata part : item.partitionsMetadata()) {
                                         if (part.partitionId() == a_partition) {
                                                returnMetaData = part;
                                                break loop;
                                         }
                                  }
                           }

                     } catch (Exception e) {
                           System.out.println("Error communicating with Broker [" + seed
                                         + "] to find Leader for [" + a_topic + ", "
                                         + a_partition + "] Reason: " + e);
                     } finally {
                           if (consumer != null)
                                  consumer.close();
                     }
              }

              if (returnMetaData != null) {
                     m_replicaBrokers.clear();
                    
                     for (Broker replica : returnMetaData.replicas()) {
                           m_replicaBrokers.add(replica.host());
                     }
              }

              return returnMetaData;
       }

       public static long getLastOffset(SimpleConsumer consumer, String topic,
                     int partition, long whichTime, String clientName) {
              TopicAndPartition topicAndPartition = new TopicAndPartition(topic,
                           partition);
              Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
              requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(
                           whichTime, 1));
              OffsetRequest request = new OffsetRequest(requestInfo,
                           kafka.api.OffsetRequest.CurrentVersion(), clientName);

              OffsetResponse response = consumer.getOffsetsBefore(request);

              if (response.hasError()) {
                     System.out
                                  .println("Error fetching data Offset Data the Broker. Reason: "
                                                + response.errorCode(topic, partition));
                     return 0;
              }
              long[] offsets = response.offsets(topic, partition);
              return offsets[0];
       }

       private String findNewLeader(String a_oldLeader, String a_topic,
                     int a_partition, int a_port) throws Exception {
              for (int i = 0; i < 3; i++) {
                     boolean goToSleep = false;
                     PartitionMetadata metadata = findLeader(m_replicaBrokers, a_port,
                                  a_topic, a_partition);
                     if (metadata == null) {
                           goToSleep = true;
                     } else if (metadata.leader() == null) {
                           goToSleep = true;
                     } else if (a_oldLeader.equalsIgnoreCase(metadata.leader().host())
                                  && i == 0) {
                           // first time through if the leader hasn't changed give
                           // ZooKeeper a second to recover
                           // second time, assume the broker did recover before failover,
                           // or it was a non-Broker issue
                           //
                           goToSleep = true;
                     } else {
                           return metadata.leader().host();
                     }
                     if (goToSleep) {
                           try {
                                  Thread.sleep(1000);
                           } catch (InterruptedException ie) {
                           }
                     }
              }
              System.out
                           .println("Unable to find new leader after Broker failure. Exiting");
              throw new Exception(
                           "Unable to find new leader after Broker failure. Exiting");
       }
}


Comments

Popular posts

Spring MongoDB Rename field with derived Value of another field

Input Collection -  [ { 'k' : 'Troubleshooting' , 'hour' : '2024-10-10T16' , 'v' : [ 'WebPage, Login' ] }, { 'k' : 'TroubleshootingMe' , 'hour' : '2024-10-07T01' , 'v' : [ 'Accounts, etc' ] }  ] Expected Output -  [ { 'hour' : '2024-10-10T16' , 'Troubleshooting' : [ 'WebPage, Login' ] }, { 'hour' : '2024-10-07T01' , 'TroubleshootingMe' : [ 'Accounts, etc' ] }  ]   Above Can be achieved by  $replaceRoot / $replaceWith as follows - { $replaceWith : { $mergeObjects : [ { hour : "$hour" }, { "$arrayToObject" : [ [ { k : "$k" , v : "$v" } ] ] } ] } } or { $replaceRoo...




What is Leadership

 




Spark Error - missing part 0 of the schema, 2 parts are expected

 Exception -  Caused by: org.apache.spark.sql.AnalysisException : Could not read schema from the hive metastore because it is corrupted. (missing part 0 of the schema, 2 parts are expected).; Analysis -  ·          Check for table definition. In TBLProperties, you might find something like this – > spark.sql.sources.schema.numPartCols > 'spark.sql.sources.schema.numParts' 'spark.sql.sources.schema.part.0' > 'spark.sql.sources.schema.part.1' 'spark.sql.sources.schema.part.2' > 'spark.sql.sources.schema.partCol.0' > 'spark.sql.sources.schema.partCol.1' That’s what error seems to say that part1 is defined but part0 is missing.  Solution -  Drop & re-create table. If Table was partitioned  then all partitions  would have been removed. So do either of below -  ·          Msck repair table <db_name>.<table_name> ·    ...




Spark MongoDB Connector Not leading to correct count or data while reading

  We are using Scala 2.11 , Spark 2.4 and Spark MongoDB Connector 2.4.4 Use Case 1 - We wanted to read a Shareded Mongo Collection and copy its data to another Mongo Collection. We noticed that after Spark Job successful completion. Output MongoDB did not had many records. Use Case 2 -  We read a MongoDB collection and doing count on dataframe lead to different count on each execution. Analysis,  We realized that MongoDB Spark Connector is missing data on bulk read as a dataframe. We tried various partitioner, listed on page -  https://www.mongodb.com/docs/spark-connector/v2.4/configuration/  But, none of them worked for us. Finally, we tried  MongoShardedPartitioner  this lead to constant count on each execution. But, it was greater than the actual count of records on the collection. This seems to be limitation with MongoDB Spark Connector. But,  MongoShardedPartitioner  seemed closest possible solution to this kind of situation. But, it per...




Read from a hive table and write back to it using spark sql

In context to Spark 2.2 - if we read from an hive table and write to same, we get following exception- scala > dy . write . mode ( "overwrite" ). insertInto ( "incremental.test2" ) org . apache . spark . sql . AnalysisException : Cannot insert overwrite into table that is also being read from .; org . apache . spark . sql . AnalysisException : Cannot insert overwrite into table that is also being read from .; 1. This error means that our process is reading from same table and writing to same table. 2. Normally, this should work as process writes to directory .hiveStaging... 3. This error occurs in case of saveAsTable method, as it overwrites entire table instead of individual partitions. 4. This error should not occur with insertInto method, as it overwrites partitions not the table. 5. A reason why this happening is because Hive table has following Spark TBLProperties in its definition. This problem will solve for insertInto met...