title: Spark iterate over RDD grouped by keys
body: <p>I've to develop a Spark application, I have to use Spark 1.3 and so I can't use window functions. I've chosen to iterate over single groups of elements, created grouping by key the rdd. The solution I found so far is to collect the keys and then to take the corresponding RDD with a lookup(key). I know that my approach is very inefficient, but I don't know how to apply my function that converts the RDD to a list and then returns another list in another way. </p>
  
  <pre><code>logon_dhcp = logons.map(lambda logon: (logon.user, (logon.dhcpscopename, logon.city, logon.timestamp)))
  logon_dhcp = logon_dhcp.groupByKey()
  
  dhcp_change_list = []
  for key in logon_dhcp.keys().collect():
      new_list = dhcp_changed(key,logon_dhcp.lookup(key))
      dhcp_change_list = list(set().union(dhcp_change_list,new_list))
  
  def dhcp_changed(key,group):
      values = list(group[0])
      values_sorted = sorted(values, key=lambda tup: tup[2])
      prevCity = None
      prevValue = None
      prevTime = None
      res = list()
      for value in values_sorted:
          if prevCity != None and prevCity != value[1] and notEnoughTime(prevTime,value[2]):
              res.append((key, prevTime.strftime('%Y-%m-%d %H:%M:%S'), prevCity, value[2].strftime('%Y-%m-%d %H:%M:%S'), value[1]))
          prevCity = value[1]
          prevTime = value[2]
          prevValue = value
      return res
  </code></pre>
  
  <p>How I can do the same thing with like an aggregateByKey()?</p>