3
votes

I'm trying to insert UDT(user-defined type) into Cassandra through python.

The object is

'dob':{'d': 1,'m': 1,'y': 2000}

I converted this object into UDT.

dob = udt_date(arg0, arg1, arg2)


class udt_date(object):
def __init__(self, d, m, y):
    self.d = d
    self.m = m
    self.y = y

From the above code I got the class object for dob as

<connector_cassandra_processing.crud.udts_registration.udt_date object at 0x7f0c20461650>

Now I tried to insert the above UDT into cassandra with the following query

 INSERT INTO error_input_log_profile_mobile(mobile,ctdon,dob) VALUES (%s,%s,%s) ['111111342', 1234, <connector_cassandra_processing.crud.udts_registration.udt_date object at 0x7f0c20461650>]

But I got the folllowing error while inserting.

Traceback (most recent call last):
File "/home/sys1010/PycharmProjects/test_connector_cassandra_processing/connector_cassandra_processing/dao/dao.py", line 23, in execute_query
result = session.execute(query, params, timeout=2000)
File "cassandra/cluster.py", line 2012, in cassandra.cluster.Session.execute (cassandra/cluster.c:35058)
return self.execute_async(query, parameters, trace, custom_payload, timeout, execution_profile, paging_state).result()
File "cassandra/cluster.py", line 3801, in cassandra.cluster.ResponseFuture.result (cassandra/cluster.c:73464)
raise self._final_exception
SyntaxException: <Error from server: code=2000 [Syntax error in CQL query] message="line 1:87 no viable alternative at input '<' (...dob) VALUES ('111111342',1234,[<]...)">

I tried everywhere to solve the issue, but didn't find any solution. Somebody please help me out.

Schema for the table

CREATE TABLE input_data_profile.error_input_log_profile_mobile (
    mobile text PRIMARY KEY,
    addrs set<frozen<udt_addrs>>,
    asset set<frozen<udt_asset>>,
    cid int,
    cntno set<frozen<udt_cntno>>,
    ctdat bigint,
    ctdon bigint,
    dob frozen<udt_date>,
    dvc set<frozen<udt_dvc>>,
    eaka set<text>,
    edmn text,
    educ set<frozen<udt_educ>>,
    error_status tinyint,
    gen tinyint,
    hobby set<text>,
    income set<frozen<udt_income>>,
    interest set<text>,
    lang set<frozen<udt_lang>>,
    levnt set<frozen<udt_levnt>>,
    like map<text, frozen<set<text>>>,
    loc set<frozen<udt_loc>>,
    mapp set<text>,
    name frozen<udt_name>,
    params map<text, frozen<set<text>>>,
    prfsn set<frozen<udt_prfsn>>,
    rel set<frozen<udt_rel>>,
    rel_s tinyint,
    skills_prfsn set<frozen<udt_skill_prfsn>>,
    snw set<frozen<udt_snw>>,
    sport set<text>
);
CREATE CUSTOM INDEX err_idx ON input_data_profile.error_input_log_profile_mobile (error_status) USING 'org.apache.cassandra.index.sasi.SASIIndex' WITH OPTIONS = {'mode': 'CONTAINS', 'analyzer_class': 'org.apache.cassandra.index.sasi.analyzer.NonTokenizingAnalyzer', 'case_sensitive': 'false'};
1

1 Answers

1
votes

If you want to store a object into your data base! you have to override the __repr__ and __str__ method. something like:

class udt_date(object):
    def __init__(self, d, m, y):
        self.d = d
        self.m = m
        self.y = y

    def __repr__(self):
        return "d:{} m:{} y:{}".format(self.d, self.m, self.y)

and then you can easily insert it to your database by:

 my_date = udt_date(12,13, 14)

 INSERT INTO error_input_log_profile_mobile(mobile,ctdon,dob) VALUES (%s,%s,%s) ['111111342', 1234, my_date]