Refactor amplab spark queries