title: How to store and read data from Spark PairRDD
body: <p>Spark PairRDD has the option to save the file.</p>
  
  <pre><code>JavaRDD&lt;String&gt; baseRDD = context.parallelize(Arrays.asList("This", "is", "dummy", "data"));
  
  JavaPairRDD&lt;String, Integer&gt; myPairRDD =
      baseRDD.mapToPair(new PairFunction&lt;String, String, Integer&gt;() {
  
        @Override
        public Tuple2&lt;String, Integer&gt; call(String input) throws Exception {
          // TODO Auto-generated method stub
          return new Tuple2&lt;String, Integer&gt;(input, input.length());
        }
      });
  
  myPairRDD.saveAsTextFile("path");
  </code></pre>
  
  <p>Spark context <code>textfile</code> reads the data to JavaRDD only.</p>
  
  <p>How to reconstruct the PairRDD directly from source?</p>
  
  <p><strong>Note:</strong></p>
  
  <ul>
  <li><p>Possible approach is to read the data to <code>JavaRDD&lt;String&gt;</code> and construct <code>JavaPairRDD</code>. </p></li>
  </ul>
  
  <p>But with huge data it is taking considerable amount of resources.</p>
  
  <ul>
  <li><p>Storing this intermediate file in non-text format is also fine.</p></li>
  <li><p>Execution environment - JRE 1.7</p></li>
  </ul>