Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for user defined types with recursion #29

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ docs/_build/

# PyBuilder
target/

dump.cql
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ It's still missing many major Cassandra features that I don't use daily,
so feel free to open an issue pointing them out (or send a pull request)
if you need something.

Setup
-----

::

python setup.py install

::

Usage
-----

Expand Down
99 changes: 94 additions & 5 deletions cassandradump.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import argparse
import sys
import json
import itertools
import codecs
from ssl import PROTOCOL_TLSv1
import six

try:
import cassandra
Expand Down Expand Up @@ -48,23 +50,110 @@ def table_to_cqlfile(session, keyspace, tablename, flt, tableval, filep, limit=0

cnt = 0

def cql_encode_string(val):
"""
Default encoder for all objects that do not have a specific encoder function
registered. This function simply calls :meth:`str()` on the object.
"""
if isinstance(val, object):
#if our object is a UDT, give cassandra what it wants (using JSON to process), and regex to clean
if type(val).__module__.startswith("cassandra"):
return '{%s}' % ', '.join('%s: %s' % (
k,
session.encoder.mapping.get(type(v), cql_encode_object)(v)
) for k, v in six.iteritems(val.__dict__))
return str(val)

def cql_encode_object(val):
"""
Default encoder for all objects that do not have a specific encoder function
registered. This function simply calls :meth:`str()` on the object.
"""
if isinstance(val, object):
#if our object is a UDT, give cassandra what it wants (using JSON to process), and regex to clean
if type(val).__module__.startswith("cassandra"):
return '{%s}' % ', '.join('%s: %s' % (
k,
session.encoder.mapping.get(type(v), cql_encode_object)(v)
) for k, v in six.iteritems(val.__dict__))
return session.encoder.cql_encode_all_types(val)

def cql_encode_map_collection(val):
"""
Converts a dict into a string of the form ``{key1: val1, key2: val2, ...}``.
This is suitable for ``map`` type columns.
"""
return '{%s}' % ', '.join('%s: %s' % (
session.encoder.mapping.get(type(k), cql_encode_object)(k),
session.encoder.mapping.get(type(v), cql_encode_object)(v)
) for k, v in six.iteritems(val))

def cql_encode_set_collection(val):
"""
Converts a sequence to a string of the form ``{item1, item2, ...}``. This
is suitable for ``set`` type columns.
"""
return '{%s}' % ', '.join(session.encoder.mapping.get(type(v), cql_encode_object)(v) for v in val)

def cql_encode_list_collection(self, val):
"""
Converts a sequence to a string of the form ``[item1, item2, ...]``. This
is suitable for ``list`` type columns.
"""
return '[%s]' % ', '.join(session.encoder.mapping.get(type(v), cql_encode_object)(v) for v in val)


def make_non_null_value_encoder(typename):
if typename == 'blob':
return session.encoder.cql_encode_bytes
elif typename.startswith('map'):
return session.encoder.cql_encode_map_collection
return cql_encode_map_collection
elif typename.startswith('set'):
return session.encoder.cql_encode_set_collection
return cql_encode_set_collection
elif typename.startswith('list'):
return session.encoder.cql_encode_list_collection
return cql_encode_list_collection
elif typename.startswith('date'):
return session.encoder.cql_encode_all_types
elif typename.startswith('ascii'):
return session.encoder.cql_encode_all_types
elif typename.startswith('bigint'):
return session.encoder.cql_encode_all_types
elif typename.startswith('blob'):
return session.encoder.cql_encode_all_types
elif typename.startswith('boolean'):
return session.encoder.cql_encode_all_types
elif typename.startswith('counter'):
return session.encoder.cql_encode_all_types
elif typename.startswith('decimal'):
return session.encoder.cql_encode_all_types
elif typename.startswith('double'):
return session.encoder.cql_encode_all_types
elif typename.startswith('float'):
return session.encoder.cql_encode_all_types
elif typename.startswith('inet'):
return session.encoder.cql_encode_all_types
elif typename.startswith('int'):
return session.encoder.cql_encode_all_types
elif typename.startswith('text'):
return cql_encode_string
elif typename.startswith('timestamp'):
return session.encoder.cql_encode_all_types
elif typename.startswith('timeuuid'):
return session.encoder.cql_encode_all_types
elif typename.startswith('uuid'):
return session.encoder.cql_encode_all_types
elif typename.startswith('varchar'):
return cql_encode_string
elif typename.startswith('varint'):
return session.encoder.cql_encode_all_types
else:
return session.encoder.cql_encode_all_types
return cql_encode_object

def make_value_encoder(typename):
e = make_non_null_value_encoder(typename)
return lambda v: session.encoder.cql_encode_all_types(v) if v is None else e(v)

def make_value_encoders(tableval):
def make_value_encoders(tableval):
return dict((to_utf8(k), make_value_encoder(cql_type(v))) for k, v in tableval.columns.iteritems())

def make_row_encoder():
Expand Down