Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用cqlengine以最快、最有效的方式插入和更新cassandra中的大量行_Python_Csv_Cassandra_Cql - Fatal编程技术网

Python 使用cqlengine以最快、最有效的方式插入和更新cassandra中的大量行

Python 使用cqlengine以最快、最有效的方式插入和更新cassandra中的大量行,python,csv,cassandra,cql,Python,Csv,Cassandra,Cql,我有一个包含15000000条记录的csv文件,我正试图将其处理到cassandra表中。以下是列标题和数据的示例: 为了更好地理解它,以下是我的python模型: class DIDSummary(Model): __keyspace__ = 'processor_api' did = columns.Text(required=True, primary_key=True, partition_key=True) month = columns.DateTime(

我有一个包含15000000条记录的csv文件,我正试图将其处理到cassandra表中。以下是列标题和数据的示例:

为了更好地理解它,以下是我的python模型:

class DIDSummary(Model):
    __keyspace__ = 'processor_api'

    did = columns.Text(required=True, primary_key=True, partition_key=True)
    month = columns.DateTime(required=True, primary_key=True, partition_key=True)
    direction = columns.Text(required=True, primary_key=True)
    duration = columns.Counter(required=True)
    cost = columns.Counter(required=True)
现在,我正在尝试处理csv文件每行中的数据,并将它们分为500、1000、10000、250等批插入,但时间结果相同(大约每1000行0.33秒,这意味着需要90分钟才能全部插入)。我还尝试使用一个多处理池,对每个
批处理.execute()
调用
apply\u async()
,但没有得到更好的结果。在python中,有没有一种方法可以使用SSTableWriter,或者做一些其他事情来更好地将它们插入cassandra?以下是我的
process\u sheet\u row()
方法供参考:

def process_sheet_row(self, row, batch):
    report_datetime = '{0}{1:02d}'.format(self.report.report_year, self.report.report_month)
    duration = int(float(row[self.columns['DURATION']]) * 10)
    cost = int(float(row[self.columns['COST']]) * 100000)

    anisummary = DIDSummary.batch(batch).create(did='{}{}'.format(self.report.ani_country_code, row[self.columns['ANI']]),
                                                direction='from',
                                                month=datetime.datetime.strptime(report_datetime, '%Y%m'))
    anisummary.duration += duration
    anisummary.cost += cost
    anisummary.batch(batch).save()

    destsummary = DIDSummary.batch(batch).create(did='{}{}'.format(self.report.dest_country_code, row[self.columns['DEST']]),
                                                 direction='to',
                                                 month=datetime.datetime.strptime(report_datetime, '%Y%m'))
    destsummary.duration += duration
    destsummary.cost += cost
    destsummary.batch(batch).save()
任何帮助都将不胜感激。谢谢

编辑:以下是我浏览文件并对其进行处理的代码:

with open(self.path) as csvfile:
    reader = csv.DictReader(csvfile)
    if arr[0] == 'inventory':
            self.parse_inventory(reader)
    b = BatchQuery(batch_type=BatchType.Unlogged)
    i = 1
    for row in reader:
        self.parse_sheet_row(row, b)
        if not i % 1000:
            connection.check_connection() # This just makes sure we're still connected to cassandra. Check code below
            self.pool.apply_async(b.execute())
            b = BatchQuery(batch_type=BatchType.Unlogged)
        i += 1
print "Done processing: {}".format(self.path)
print "Time to Execute: {}".format(datetime.datetime.now() - start)
print "Batches: {}".format(i / 1000)
print "Records processed: {}".format(i - 1)
正因为这可能会有点帮助,下面是
连接。检查连接()
方法(以及周围的方法):


通常,批处理并不是执行插入的最快方法。尤其是在包含各种分区的未标记批中。关于批次的一些阅读

如果您可以从cqlengine中退出以进行插入,那么应该尝试在Python驱动程序中的以下位置实现:

在误用了不同大小的批次后,我在inserts/sec方面有了重大改进,转而使用此方法,但YMMV

def setup_defaults():
    connection.setup(['127.0.0.1'], 'processor_api', lazy_connect=True)

def check_connection():
    from cdr.models import DIDSummary
    try:
        DIDSummary.objects.all().count()
    except CQLEngineException:
        setup_defaults()