title: How to send Map to Kafka topic so that ProducerRecord key is same as corresponding Map key
body: <p>I am using Spark streaming and data is being sent to Kafka. I am sending a Map to Kafka. Assuming I have a Map of 20(which may grow to 1000 in a Streaming Batch duration) elements like below:</p>
  
  <pre><code>HashMap&lt;Integer,String&gt; input = new HashMap&lt;Integer,String&gt;();
          input.put(11,"One");
          input.put(312,"two");
          input.put(33,"One");
          input.put(24,"One");
          input.put(35,"One");
          input.put(612,"One");
          input.put(7,"One");
          input.put(128,"One");
          input.put(9,"One");
          input.put(10,"One");
          input.put(11,"One1");
          input.put(12,"two1");
          input.put(13,"One1");
          input.put(14,"One1");
          input.put(15,"One1");
          input.put(136,"One1");
          input.put(137,"One1");
          input.put(158,"One1");
          input.put(159,"One1");
          input.put(120,"One1");
  
  
  
          Set&lt;Integer&gt; inputKeys = input.keySet();
          Iterator&lt;Integer&gt; inputKeysIterator = inputKeys.iterator();
          while (inputKeysIterator.hasNext()) {
              Integer key = inputKeysIterator.next();
              ProducerRecord&lt;Integer, String&gt; record = new ProducerRecord&lt;Integer, String&gt;(topic,
                      key%10, input.get(key));
              KafkaProducer.send(record);
          }
  </code></pre>
  
  <p>My Kafka topic is having 10 partitions. Here I am calling kafkaProducer.send() 20 times and hence 20 Kafka call. how can I send whole data in a batch i.e. in one Kafka call, but again I want to ensure each record goes to specific partition driven by formula <strong>key%10</strong> as in </p>
  
  <p><em>ProducerRecord record = new ProducerRecord(topic,
                          <strong>key%10</strong>, input.get(key));</em></p>
  
  <p>Options I see: <strong>linger.ms=1</strong> may ensure that but with a latency of 1ms. 
  How to avoid this latency and to avoid 20 network(Kafka) call or to minimize Kafka calls?</p>