Найти дубликаты в большом наборе данных с помощью Python - PullRequest
0 голосов
/ 23 декабря 2018

Я хочу найти дубликаты из большого файла JSON (12 ГБ) в Python.Теперь я читаю весь файл, использую set () в качестве справочной таблицы и сохраняю уникальные значения, а затем записываю дубликаты в файл.

Но если входные данные имеют размер 100 Гб, set () не будетбыть в состоянии обрабатывать уникальные значения, и, следовательно, мой код не является скалярным.

Любая идея, как я могу сделать это альтернативно?

Использование python

import json
import time

""" This calss contains the business logic for identifying the duplicates and creating an output file for further processing """

class BusinessService:

    """ The method identiifes the duplicate """

    def service(ipPath,opPath):
        start_time = time.time()    #We start the timer 
        uniqueHandleSet = set();     #Creating a set to store unique values #

        try:
            # Opening and creating an output file to catch the duplicate hanndles #                     
            duplicateHandles = open(opPath,'w+',encoding='utf-8') 
            #Reading the JSON File by buffering and using 20mb as it is too big to read at once #       
            with open(ipPath,buffering = 200000000, encoding = 'utf-8') as infile:     
                for line in infile:
                    tweetJsonObject = json.loads(line);
                    if tweetJsonObject["name"] not in uniqueHandleSet:
                        uniqueHandleSet.add(tweetJsonObject["name"]);
                    else:
                        duplicateHandles.write(line);  
            print("--- %s seconds --- memory 200mb while buffering" % (time.time() - start_time));  #Printing the total time required to execute 
        except:
            print("Error")

        finally:
            duplicateHandles.close();

Мне нужноиспользуйте альтернативу для set () для поиска

...