def save_rdd_as_pickle(rdd, path, batch_size=10, overwrite=False):
    """ Saves an rdd by grouping all the records of each partition as one pickle file

    :param rdd: rdd to save
    :param path: where to save
    :param batch_size: batch size to pass to spark saveAsPickleFile
    :param overwrite: if directory exist whether to overwrite
    """
    if os.path.isdir(path):
        if overwrite:
            logging.getLogger('pySparkUtils').info('Deleting files from: %s' % path)
            shutil.rmtree(path)
            logging.getLogger('pySparkUtils').info('Done deleting files from: %s' % path)
        else:
            logging.getLogger('pySparkUtils').error('Directory %s already exists '
                                                    'and overwrite is false' % path)
            raise IOError('Directory %s already exists and overwrite is false'
                          % path)
    rdd.glom().saveAsPickleFile(path, batchSize=batch_size)
    logging.getLogger('pySparkUtils').info('Saved rdd as pickle to: %s' % path)