title: How to use map function in my code below instead of collect function?
body: <p>Kafka producer tries to send bulk message (10k) at a time but after transferring 3k it's start throwing error. I have used collect function in below code that might be a problem so I want to replace that with map.</p>
  
  <pre class="lang-java prettyprint-override"><code>//Memberjson is JavaRDD&lt;String&gt;
  
  for (String outputMbrJson : memberjson.collect()) {
  
      try {
  
          Producer kafkaProducer = new Producer(configFile);
  
          kafkaProducer.runProducer(Arrays.asList(outputMbrJson).iterator());
          } 
          catch (Throwable e) {
              e.printStackTrace();
          }
      }
  ---------------------------------------
  //runProducer method 
  
  public Iterator&lt;String&gt; runProducer(Iterator&lt;String&gt; jsons) throws Exception {
  
          final Producer&lt;Long, String&gt; producer = createProducer();
  
          ArrayList&lt;String&gt; ret = new ArrayList&lt;String&gt;();
  
          try {
              while (jsons.hasNext()) {
                  String jsonobj = jsons.next();
  
                  ret.add(EMPTY_STRING);
                  try {
                      ProducerRecord&lt;Long, String&gt; record = new ProducerRecord&lt;Long, String&gt;(conf.getProperty("producer.topic"), jsonobj.toString());
  
                      producer.send(record);
  
                  } catch (Exception e) {
                      System.out.println(e.getMessage());
                  }
              }
          } 
          catch (Exception e) {
              e.printStackTrace();
          } 
          finally {
              producer.flush();
              producer.close();
          }
          return ret.iterator();
  }
  </code></pre>