有没有办法修改以下Spark Python代码以更好地使用内存。

时间:2016-06-24 14:52:19

标签: python memory-management apache-spark garbage-collection

我正在尝试运行以下代码。它给我一个GC错误。我尝试在没有reduceByKey的情况下运行它并且代码可以工作。我想它在reduce函数中占用了大量内存。我尝试增加驱动程序内存,内核数量,执行程序内存。我也尝试了groupByKey而不是reduceByKey。但没有运气。

但我明白的是,我在不释放变量时做错了什么。有人可以帮助我。

        import os
        import sys
        import unicodedata
        from operator import add 

        try:
            from pyspark import SparkConf
            from pyspark import SparkContext
        except ImportError as e:
            print ("Error importing Spark Modules", e)
            sys.exit(1)


        # delimeter function
        def findDelimiter(text):
            sD = text[1] 
            eD = text[2] 
            return (eD, sD) 

        def tokenize(text):
            sD = findDelimiter(text)[1]
            eD = findDelimiter(text)[0]
            arrText = text.split(sD)
            text = ""
            seg = arrText[0].split(eD)
            arrText=""
            senderID = seg[6].strip()
            yield (senderID, 1)


        conf = SparkConf()
        sc = SparkContext(conf=conf)

        textfile = sc.textFile("hdfs://my_IP:9000/data/*/*.txt")

        rdd = textfile.flatMap(tokenize)
        rdd = rdd.reduceByKey(lambda a,b: a+b)
        textfile=""
  rdd.coalesce(1).saveAsTextFile("hdfs://my_IP:9000/data/total_result503")
    rdd=""

0 个答案:

没有答案
相关问题