【问题标题】:How much copies of the environment does spark do?spark做了多少环境副本?
【发布时间】:2017-10-12 19:29:30
【问题描述】:

我有一个 PySpark 应用程序,它必须详细说明大约 5gb 的压缩数据(字符串)。我正在使用具有 12 个内核(24 个线程)和 72Gb 内存的小型服务器。我的 PySpark 程序仅包含 2 个映射操作,由 3 个非常大的正则表达式(每个已编译 3gb)提供帮助并加载了pickle。 Spark 在独立模式下工作,worker 和 master 在同一台机器上。

我的问题是:spark 是否为每个执行程序核心复制每个变量?因为它使用了所有可用的内存,然后使用了大量的交换空间。或者它是否会加载 RAM 中的所有分区? RDD 包含大约 1000 万个字符串,必须由 3 个正则表达式搜索。 RDD 计算大约 1000 个分区。我无法完成这项任务,因为几分钟后内存已满,并且 spark 开始使用交换空间变得非常慢。 我注意到没有正则表达式的情况是一样的。

这是我的代码,它会删除推特推文的所有无用字段,并扫描推文的文本和特定词的描述:

import json
import re
import twitter_util as twu
import pickle

from pyspark import SparkContext
sc = SparkContext()

prefix = '/home/lucadiliello'

source = prefix + '/data/tweets'
dest = prefix + '/data/complete_tweets'

#Regex's path
companies_names_regex = prefix + '/data/comp_names_regex'
companies_names_dict = prefix + '/data/comp_names_dict'
companies_names_dict_to_legal = prefix + '/data/comp_names_dict_to_legal'

#Loading the regex's
comp_regex = pickle.load(open(companies_names_regex))
comp_dict = pickle.load(open(companies_names_dict))
comp_dict_legal = pickle.load(open(companies_names_dict_to_legal))

#Loading the RDD from textfile 
tx = sc.textFile(source).map(lambda a: json.loads(a))


def get_device(input_text):
    output_text = re.sub('<[^>]*>', '', input_text)
    return output_text

def filter_data(a):
    res = {}
    try:
        res['mentions'] = a['entities']['user_mentions']
        res['hashtags'] = a['entities']['hashtags']
        res['created_at'] = a['created_at'] 
        res['id'] = a['id'] 

        res['lang'] = a['lang']
        if 'place' in a and a['place'] is not None:      
            res['place'] = {} 
            res['place']['country_code'] = a['place']['country_code'] 
            res['place']['place_type'] = a['place']['place_type'] 
            res['place']['name'] = a['place']['name'] 
            res['place']['full_name'] = a['place']['full_name']

        res['source'] = get_device(a['source'])
        res['text'] = a['text'] 
        res['timestamp_ms'] = a['timestamp_ms'] 

        res['user'] = {} 
        res['user']['created_at'] = a['user']['created_at'] 
        res['user']['description'] = a['user']['description'] 
        res['user']['followers_count'] = a['user']['followers_count'] 
        res['user']['friends_count'] = a['user']['friends_count']
        res['user']['screen_name'] = a['user']['screen_name']
        res['user']['lang'] = a['user']['lang']
        res['user']['name'] = a['user']['name']
        res['user']['location'] = a['user']['location']
        res['user']['statuses_count'] = a['user']['statuses_count']
        res['user']['verified'] = a['user']['verified']
        res['user']['url'] = a['user']['url']
    except KeyError:
        return []

    return [res]


results = tx.flatMap(filter_data)


def setting_tweet(tweet):

    text = tweet['text'] if tweet['text'] is not None else ''
    descr = tweet['user']['description'] if tweet['user']['description'] is not None else ''
    del tweet['text']
    del tweet['user']['description']

    tweet['text'] = {}
    tweet['user']['description'] = {}
    del tweet['mentions']

    #tweet
    tweet['text']['original_text'] = text
    tweet['text']['mentions'] = twu.find_retweet(text)
    tweet['text']['links'] = []
    for j in twu.find_links(text):
        tmp = {}
        try:
            tmp['host'] = twu.get_host(j)
            tmp['link'] = j
            tweet['text']['links'].append(tmp)
        except ValueError:
            pass

    tweet['text']['companies'] = []
    for x in comp_regex.findall(text.lower()):
        tmp = {}
        tmp['id'] = comp_dict[x.lower()]
        tmp['name'] = x
        tmp['legalName'] = comp_dict_legal[x.lower()]
        tweet['text']['companies'].append(tmp)

    # descr
    tweet['user']['description']['original_text'] = descr
    tweet['user']['description']['mentions'] = twu.find_retweet(descr)
    tweet['user']['description']['links'] = []
    for j in twu.find_links(descr):
        tmp = {}
        try:
            tmp['host'] = twu.get_host(j)
            tmp['link'] = j
            tweet['user']['description']['links'].append(tmp)
        except ValueError:
            pass

    tweet['user']['description']['companies'] = []
    for x in comp_regex.findall(descr.lower()):
        tmp = {}
        tmp['id'] = comp_dict[x.lower()]
        tmp['name'] = x
        tmp['legalName'] = comp_dict_legal[x.lower()]
        tweet['user']['description']['companies'].append(tmp)

    return tweet


res = results.map(setting_tweet)

res.map(lambda a: json.dumps(a)).saveAsTextFile(dest, compressionCodecClass="org.apache.hadoop.io.compress.BZip2Codec")

更新 大约 1 小时后,内存 (72gb) 完全满了,swap (72gb) 也已满。就我而言,使用广播不是解决方案。

更新 2 如果不使用 pickle 加载 3 个变量,则使用最多 10GB 的 RAM 而不是 144GB 就可以毫无问题地结束! (72GB RAM + 72Gb 交换)

【问题讨论】:

  • 代码会很好,但如果没有它来回答您的问题 - Spark 使用的局部变量副本与您分配给 Python 工作者的线程(核心)数量一样多。有一些解决方法,但通常非常复杂。
  • 给定代码,您应该为驱动程序副本添加+1,为驱动程序上的腌制版本添加+1,为每个执行程序JVM(或多或少)添加+1。您可以通过使用广播或直接从执行程序加载数据来稍微改善这一点。
  • 难道没有一个技巧可以为每个执行程序进程在内存中使用相同的正则表达式实例吗?如果没有,我想我会减少执行者的数量.....
  • 问题在于,与 JVM worker 不同,每个 Python worker 都是一个单独的进程。还有一些其他令人讨厌的细微差别,但这里不太重要。您可以使用一些线程变体(例如反应式流),但为什么还要在此基础上使用 Spark?也许用 Tasks 代替 Celery?
  • 谢谢,我想我会先尝试广播,因为我目前无法更改环境。

标签: python apache-spark pyspark distributed-computing bigdata


【解决方案1】:

我的问题是:spark 是否为每个执行器核心复制每个变量?

是的!

每个(本地)变量的副本数等于您分配给 Python 工作者的线程数。


至于你的问题,尝试加载comp_regexcomp_dictcomp_dict_legal而不使用pickle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 2016-10-14
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    相关资源
    最近更新 更多