pyspark mapper

def mapper(seq):
    freq = dict()
    for x in list(seq):
        if x in freq:
            freq[x] += 1
        else:
            freq[x] = 1

    kv = [(x, freq[x]) for x in freq.keys()]    
    return kv


from pyspark import SparkContext

if __name__ == "__main__":

    sc = SparkContext('local', 'mapper')
    lines = sc.textFile("./data/dna_seq.txt", 1)    

    rdd = lines.flatMap(mapper)
    cnt = rdd.reduceByKey(lambda x, y: x + y)
    print (cnt.collect())
[('A', 7), ('T', 7), ('C', 6), ('G', 6)]
posted @ 2022-08-19 22:58  luoganttcc  阅读(2)  评论(0编辑  收藏  举报