title: Specify number of partitions on Kafka producer
body: <p>I have the following code from the web page <a href="https://cwiki.apache.org/confluence/display/KAFKA/0.8.0+Producer+Example" rel="nofollow noreferrer">https://cwiki.apache.org/confluence/display/KAFKA/0.8.0+Producer+Example</a></p>
  <p>What seems to be missing is how to configure the number of partitions.  I want to specify 4 partitions but am always ending up with the default of 2 partitions.  How do I change the code to have 4 partitions(without changing the default).</p>
  <pre><code>    Properties props = new Properties();
       
      props.put(&quot;metadata.broker.list&quot;, &quot;localhost:9092,broker2:9092&quot;);
      props.put(&quot;serializer.class&quot;, &quot;kafka.serializer.StringEncoder&quot;);
      props.put(&quot;partitioner.class&quot;, &quot;com.gnip.kafka.SimplePartitioner&quot;);
      props.put(&quot;request.required.acks&quot;, &quot;1&quot;);
      props.put(&quot;num.partitions&quot;, 4);
       
      ProducerConfig config = new ProducerConfig(props);
      Producer&lt;String, String&gt; producer = new Producer&lt;String, String&gt;(config);
      
      Random rnd = new Random();
      for (long nEvents = 0; nEvents &lt; 1000; nEvents++) { 
          long runtime = new Date().getTime();
          String ip = &quot;192.168.2.&quot; + rnd.nextInt(255);
          String msg = runtime + &quot;,www.example.com,&quot; + ip;
          KeyedMessage&lt;String, String&gt; data = new KeyedMessage&lt;String, String&gt;(&quot;page_visits2&quot;, ip, msg);
          producer.send(data);
      }
  
      producer.close();
  </code></pre>