title: scala spark how to get latest day's record
body: <pre><code>data=
  """
  user date      item1 item2
  1    2015-12-01 14  5.6
  1    2015-12-01 10  0.6
  1    2015-12-02 8   9.4
  1    2015-12-02 90  1.3
  2    2015-12-01 30  0.3
  2    2015-12-01 89  1.2
  2    2015-12-30 70  1.9
  2    2015-12-31 20  2.5
  3    2015-12-01 19  9.3
  3    2015-12-01 40  2.3
  3    2015-12-02 13  1.4
  3    2015-12-02 50  1.0
  3    2015-12-02 19  7.8
  """
  </code></pre>
  
  <p>if I have some data like above, how can I get each user's latest day's record?
  I tried to use the groupByKey, but have no idea.</p>
  
  <pre><code>val user = data.map{
  case(user,date,item1,item2)=&gt;((user,date),Array(item1,item2))
  }.groupByKey()
  </code></pre>
  
  <p>and then I don't know how to deal with it. Can anyone give me some suggestions? Thanks a lot:)</p>
  
  <h2>update:</h2>
  
  <p>I changed my data,and now user has several records in the latest day, and I want to get all of them. Thx:)</p>
  
  <h2>second update:</h2>
  
  <p>I want to get the result is:</p>
  
  <pre><code>user1 (2015-12-02,Array(8,9.4),Array(90,1.3))
  user2 (2015-12-31,Array(20,2.5))
  user3 (2015-12-02,Array(13,1.4),Array(50,1.0),Array(19,7,8))
  </code></pre>
  
  <p>and now I write some code:</p>
  
  <pre><code>val data2=data.trim.split("\\n").map(_split("\\s+")).map{
  f=&gt;{(f(0),ArrayBuffer(
                      f(1),
                      f(2).toInt,
                      f(3).toDouble)
      )}
  }
  val data3 = sc.parallelize(data2)
  data3.reduceByKey((x,y)=&gt;
               if(x(0).toString.compareTo(y(0).toString)&gt;=0) x++=y
                    else y).foreach(println)
  </code></pre>
  
  <p>result is:</p>
  
  <pre><code>(2,ArrayBuffer(2015-12-31, 20, 2.5))
  (1,ArrayBuffer(2015-12-02, 8, 9.4, 2015-12-02, 90, 1.3))
  (3,ArrayBuffer(2015-12-02, 13, 1.4, 2015-12-02, 50, 1.0, 2015-12-02, 19, 7.8))
  </code></pre>
  
  <p>Is there anything can do to improve it? :)</p>