我的任务是将一个安装了数百万条记录的cassandra从2.1版升级到3.11版(最新版本)。更为复杂的是,misencoded utf8值存在一些内部格式问题。
我通过将每个验证器更改为字节类型来修复utf8问题,因此至少现在所有记录都是可见的(即不触发格式错误)。但我无法将数据导出到csv文件:
一个cql select命令,无论是在cqlsh中还是在通过datastax驱动程序的脚本中,都是显而易见的解决方案。但是select语句的默认限制是10000,我已经读到将其更改为更大的值将导致各种错误,事实上文档建议不能将其设置为2000000以上。所以这就是排除的cql方法。
dsbulk实用程序将是下一个选择。但是当我在一个测试表上尝试这个方法时,它以一种我无法理解的奇怪的编码产生了输出字节字符串。
所以我不得不回到计划c,也就是使用pycassa驱动程序来导出数据。然而,这带来了另一个问题-它读取的记录数大约是cql看到的记录数的一半!我的问题是为什么?!
这是我的python2 pycassa脚本(其输出与sql select相同,以便比较):
# !/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Python 2 Script to export raw CQL byte output of 'complete' or 'partial' column familes from Cassandra 2.1.x tables
#
# For small table exports, the CQL SELECT statement in table_export2.sh is fine. But SELECT has a default LIMIT of
# 10,000 and a zero LIMIT (meaning none) cannot be specified, and even if it could the process would hang and fall
# over in a heap if millions of rows had to be exported.
#
import sys
import os
import re
import codecs
import collections
import binascii
import pycassa
from pycassa.pool import ConnectionPool
from pycassa.columnfamily import ColumnFamily
cass_node = sys.argv[1] if len(sys.argv) >= 2 else ''
namespace = sys.argv[2] if len(sys.argv) >= 3 else ''
col_fam = sys.argv[3] if len(sys.argv) >= 4 else ''
if col_fam not in [ 'complete', 'partial' ]:
print("Usage: python compart.py {cassandra_host_name} {cassandra_namespace_name}" +
" { complete | partial } [ {CQL_file_name} ]")
sys.exit(1)
file_path = sys.argv[4] if len(sys.argv) == 5 else ''
write_file = sys.stdout if file_path == '' else open(file_path, 'w')
# Export rows
#
# Text file /etc/cassandra/conf/cassandra.yaml must include:
#
# start_rpc: true
# rpc_port: 9171
#
pool = ConnectionPool(namespace, [ cass_node + ':9171' ], \
credentials={'username': 'cassandra', 'password': 'cassandra'} )
table = pycassa.ColumnFamily(pool, col_fam)
print " key | column1 | value"
print "----------+------------------------------------------------------------------------------+-------"
for fk, fv in table.get_range(start='', finish=''):
key = binascii.hexlify(fk)
if type(fv) == type(collections.OrderedDict()):
for column1 in fv.keys():
value = fv[column1]
if value != '':
value = binascii.hexlify(value)
print '%9s |%77s |%6s' % ('0x' + key, '0x' + binascii.hexlify(column1), '0x' + value)
这将产生大约700行,但是当我在cqlsh中运行“selectcount(*)fromcomplete”时,大约有1300行。我使用winmerge检查了输出,丢失的记录是一个具有较大键值的块。看来皮卡萨/节俭司机因为某种原因错过了最近的记录?!!
有什么想法吗?
-=-=-=-=-=-=-=-=-=-=-=
针对alex ott的第一条评论:
非常感谢你的及时回复。
表架构为:
CREATE TABLE app2_uat.complete (
key blob,
column1 blob,
value blob,
PRIMARY KEY (key, column1)
) WITH COMPACT STORAGE
AND CLUSTERING ORDER BY (column1 ASC)
AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
AND comment = ''
AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy'}
AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND dclocal_read_repair_chance = 0.0
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = 'NONE';
我尝试的dsbulk命令是:
dsbulk unload -u cassandra -p cassandra -h '["172.31.44.160"]' -query "SELECT * from app2_uat.complete;"
我对这种方法的第一个问题是,它使用了坏的旧cql select和它的限制,我不能随意增加或消除-select是出于这个原因!
我的第二个问题是它以一种杂乱无章的格式输出blob,例如:
key,column1,value
MTU4,Yy0xNTgyZmYxYzkwMGYxNzQ0ZmFhOWVlOGRkZWQxOTM2OGI3MA==,IA==
MTU4,Yy0xNThhMmYyNDliMWJhOWI0YWVmOTc4OTM5ZjE0NzZmNGFjNQ==,IA==
MTU4,Yy0xNThjODJmZGYyZDIyMjk0YWFjODBhNjQ5Y2NiZGZhMDk5Mg==,IA==
OTQ=,Yy05NDRhYjYyM2YxODMzNDQwM2M5MmNmOTc0ZWJkNjRiZmY0,IA==
MTE3,Yy0xMTc3OTEzNWE0OTNlYjU0YzU1YTNjMTdhNzc5YTk2ZTM1ZQ==,
MTE3,Yy0xMTdiYzQ2ZmNhNTc1ZmQ0MDk3YmQ0NTYxODdhMzQxYTQ1Ng==,
NTg=,Yy01ODZmZmVhNTczYjRjNzQwNGJiYjFjNzM2MzMxNTM5Mzhj,
NTg=,Yy01ODhjMmI3ZWJjNWYwNjQ1OGQ5NGMwNDljOWI1OGRiYjk0,
我无法将其与cql(和我的pycassa脚本)生成的十六进制输出联系起来:
key | column1 | value
----------+------------------------------------------------------------------------------+-------
0x36 | 0x632d3632373566376561633136323131653338343730303230303839646531306266 | 0x20
0x36 | 0x632d3633383563356632633136323131653339636363303230303839646531306266 | 0x20
0x36 | 0x632d3634363737643663633136323131653362346135303230303839646531306266 | 0x20
0x36 | 0x632d3635353933303361633136323131653361326137303230303839646531306266 | 0x20
0x36 | 0x632d3636326264653836633136323131653362323363303230303839646531306266 | 0x20
0x36 | 0x632d3637306662343934633136323131653361616439303230303839646531306266 | 0x20
0x36 | 0x632d3637663931376230633136323131653361623963303230303839646531306266 | 0x20
0x36 | 0x632d3638636164373061633136323131653339396134303230303839646531306266 | 0x20
你能想到为什么pycassa不输出一个在cql中可见的记录块吗?
暂无答案!
目前还没有任何答案,快来回答吧!