如何使用Python驱动程序加快execute_async插入Cassandra的速度

时间:2019-06-10 22:35:56

标签: python cassandra

我正在尝试使用python驱动程序将数据加载到Cassandra中。我获得的最快速度是每秒约6k次写入。我正在读取的csv大约有115万行,导致总插入时间约为3分10秒。我真的需要将时间缩短到2分钟或更短,以跟上数据的传输速度。

我的数据由115万行和52列组成。

当前,我正在使用session.execute_async函数插入数据。一次更改我允许的asnyc请求数量似乎确实可以加快它的速度。似乎在大约5-6k请求之后进行阻塞会导致最快的插入速度。

我确实尝试过批量插入,但是速度非常慢。

这是我当前将数据插入Cassandra的方法。

# insert data into cassandra table
execution_profile = ExecutionProfile(request_timeout=10)
profiles = {'node1': execution_profile}
auth_provider = PlainTextAuthProvider(username='cassandra', password='cassandra')
cluster = Cluster(['11.111.11.11'], 9042, auth_provider=auth_provider, execution_profiles=profiles) 
session = cluster.connect() # connect to your keyspace

# Read csv rows into cassandra
count = 0
futures = []
with open('/massaged.csv') as f:
    next(f) #skip the header row
    for line in f:
        query = SimpleStatement("INSERT INTO hrrr.hrrr_18hr( loc_id,utc,sfc_vis,sfc_gust,sfc_pres,sfc_hgt,sfc_tmp,sfc_snow_0Xacc,sfc_cnwat,sfc_weasd,sfc_snowc,sfc_snod,two_m_tmp,two_m_pot,two_m_spfh,two_m_dpt,two_m_rh,ten_m_ugrd,ten_m_vgrd,ten_m_wind_1hr_max,ten_m_maxuw_1hr_max,ten_m_maxvw_1hr_max,sfc_cpofp,sfc_prate,sfc_apcp_0Xacc,sfc_weasd_0Xacc,sfc_frozr_0Xacc,sfc_frzr_0Xacc,sfc_ssrun_1hr_acc,sfc_bgrun_1hr_acc,sfc_apcp_1hr_acc,sfc_weasd_1hr_acc,sfc_frozr_1hr_acc,sfc_csnow,sfc_cicep,sfc_cfrzr,sfc_crain,sfc_sfcr,sfc_fricv,sfc_shtfl,sfc_lhtfl,sfc_gflux,sfc_vgtyp,sfc_cape,sfc_cin,sfc_dswrf,sfc_dlwrf,sfc_uswrf,sfc_ulwrf,sfc_vbdsf,sfc_vddsf,sfc_hpbl) VALUES (%s)" %(line), consistency_level=ConsistencyLevel.ONE)
        futures.append(session.execute_async(query, execution_profile='node1'))
        count += 1
        if count % 5000 == 0:
            for f in futures:
                f.result() # blocks until remaining inserts are completed.
                futures = []
            print("rows processed: " + str(count))

# Catch any remaining async requests that haven't finished
for f in futures:
    f.result() # blocks until remaining inserts are completed.
print("rows processed: " + str(count))

我需要将插入时间缩短到2分钟或更短(每秒插入大约10K)。我应该使用多重处理来实现这一点,还是我不正确地使用了execute_async函数?

更新

根据亚历克斯的建议,我试图执行一份准备好的陈述。这是我想出的,但似乎要慢得多?对我做错了什么想法?

hrrr_prepared = session.prepare("INSERT INTO hrrr.hrrr_18hr( loc_id,utc,...,sfc_hpbl) VALUES (?, ..., ?)")

for row in range(0, len(data)):
    futures.append(session.execute_async(hrrr_prepared, tuple(data.iloc[row])))
    count += 1
    if count % 5000 == 0:
        for f in futures:
            f.result() # blocks until remaining inserts are completed.
            futures = []
        print("rows processed: " + str(count))

注意:为了便于阅读,我在准备好的语句中添加了“ ...”,实际代码中没有。

1 个答案:

答案 0 :(得分:1)

大幅提高应该来自使用prepared statements而不是使用SimpleStatement-对于准备好的语句,它仅解析一次(循环外),然后仅将数据与查询ID。使用SimpleStatement,每次都会解析查询。

此外,如果您不等待所有期货完成,但是有某种“计数信号量”不允许您超出“运行中”请求的最大数量,则可以提高吞吐量。您可以在其中一些执行后立即发送新请求。我不是Python专家,所以无法确切说明如何执行此操作,但是您可以研究Java implementation来了解一个想法。