You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cassandra.apache.org by ROBIN SRIVASTAVA <sr...@gmail.com> on 2015/02/27 12:56:39 UTC

how to make unique coloumns in cassandra

I want to make unique constraint in cassandra . As i want that all the
value in my column be unique in my column family ex: name-rahul phone-123
address-abc

now i want that in this row no values equal to rahul ,123 and abc get
inserted again on searching on datastax i found that i can achieve it by
doing query on partition key as IF NOT EXIST ,but not getting the solution
for getting all the three values unique means if name- jacob phone-123
address-qwe

this should also be not inserted into my database as my phone column has
the same value as i have shown with name-rahul.

Re: how to make unique coloumns in cassandra

Posted by Peter Lin <wo...@gmail.com>.
Use a RDBMS 

There is a reason constraints were created and why Cassandra doesn't have it

Sent from my iPhone

> On Mar 2, 2015, at 2:23 AM, Rahul Srivastava <sr...@gmail.com> wrote:
> 
> but what if i want to fetch the value using on table then this idea might fail
> 
>> On Mon, Mar 2, 2015 at 12:46 PM, Ajaya Agrawal <aj...@gmail.com> wrote:
>> Make a table for each of the unique keys. For e.g.
>> 
>> If primary key for user table is user_id and you want the phone number column to be unique then create another table wherein the primary key is (phone_number, user_id). Before inserting to main table try to insert to this table first with "if not exists" clause. If it succeeds then go ahead with your insert to the user table. Similarly while deleting a row from the primary table delete the corresponding row in all other tables. The order of insertion to tables matter here otherwise you would end up inducing race conditions.
>> 
>> The catch here is, you should not be updating the unique column ever. If you do that you would have to use locks and if there are multiple nodes running your application then you would need a distributed lock. I would suggest not to update the unique columns. In stead force your users to delete the entry and recreate it. If you can't do that you need to evaluate your choice of database. Perhaps a relational database would be better suited to your requirements.
>> 
>> Hope this helps!
>> 
>> -Ajaya
>> 
>> Cheers,
>> Ajaya
>> 
>>> On Fri, Feb 27, 2015 at 5:26 PM, ROBIN SRIVASTAVA <sr...@gmail.com> wrote:
>>> I want to make unique constraint in cassandra . As i want that all the value in my column be unique in my column family ex: name-rahul phone-123 address-abc
>>> 
>>> now i want that in this row no values equal to rahul ,123 and abc get inserted again on searching on datastax i found that i can achieve it by doing query on partition key as IF NOT EXIST ,but not getting the solution for getting all the three values unique means if name- jacob phone-123 address-qwe
>>> 
>>> this should also be not inserted into my database as my phone column has the same value as i have shown with name-rahul.
>>> 
> 

Re: how to make unique coloumns in cassandra

Posted by Ajaya Agrawal <aj...@gmail.com>.
Please be clear on questions and spend some time on writing questions so
that other people know what you are trying to ask. I can't read your mind.
:)

Back to your question:
Assuming that you need to search based on the values of the unique column
then invert the index on auxiliary table. So instead of (phone_number,
user_id) index you would have to have (user_id, phone_number) as index.
Then do a query on the auxiliary table and then on user table if you want
other columns. You can replicate other columns in the auxiliary table also
to avoid multiple queries.

Cheers,
Ajaya

On Mon, Mar 2, 2015 at 12:53 PM, Rahul Srivastava <
srivastava.robin71@gmail.com> wrote:

> but what if i want to fetch the value using on table then this idea might
> fail
>
> On Mon, Mar 2, 2015 at 12:46 PM, Ajaya Agrawal <aj...@gmail.com> wrote:
>
>> Make a table for each of the unique keys. For e.g.
>>
>> If primary key for user table is user_id and you want the phone number
>> column to be unique then create another table wherein the primary key is
>> (phone_number, user_id). Before inserting to main table try to insert to
>> this table first with "if not exists" clause. If it succeeds then go ahead
>> with your insert to the user table. Similarly while deleting a row from the
>> primary table delete the corresponding row in all other tables. The order
>> of insertion to tables matter here otherwise you would end up inducing race
>> conditions.
>>
>> The catch here is, you should not be updating the unique column ever. If
>> you do that you would have to use locks and if there are multiple nodes
>> running your application then you would need a distributed lock. I would
>> suggest not to update the unique columns. In stead force your users to
>> delete the entry and recreate it. If you can't do that you need to evaluate
>> your choice of database. Perhaps a relational database would be better
>> suited to your requirements.
>>
>> Hope this helps!
>>
>> -Ajaya
>>
>> Cheers,
>> Ajaya
>>
>> On Fri, Feb 27, 2015 at 5:26 PM, ROBIN SRIVASTAVA <
>> srivastava.robin71@gmail.com> wrote:
>>
>>> I want to make unique constraint in cassandra . As i want that all the
>>> value in my column be unique in my column family ex: name-rahul phone-123
>>> address-abc
>>>
>>> now i want that in this row no values equal to rahul ,123 and abc get
>>> inserted again on searching on datastax i found that i can achieve it by
>>> doing query on partition key as IF NOT EXIST ,but not getting the solution
>>> for getting all the three values unique means if name- jacob phone-123
>>> address-qwe
>>>
>>> this should also be not inserted into my database as my phone column has
>>> the same value as i have shown with name-rahul.
>>>
>>
>>
>

Re: how to make unique coloumns in cassandra

Posted by Rahul Srivastava <sr...@gmail.com>.
but what if i want to fetch the value using on table then this idea might
fail

On Mon, Mar 2, 2015 at 12:46 PM, Ajaya Agrawal <aj...@gmail.com> wrote:

> Make a table for each of the unique keys. For e.g.
>
> If primary key for user table is user_id and you want the phone number
> column to be unique then create another table wherein the primary key is
> (phone_number, user_id). Before inserting to main table try to insert to
> this table first with "if not exists" clause. If it succeeds then go ahead
> with your insert to the user table. Similarly while deleting a row from the
> primary table delete the corresponding row in all other tables. The order
> of insertion to tables matter here otherwise you would end up inducing race
> conditions.
>
> The catch here is, you should not be updating the unique column ever. If
> you do that you would have to use locks and if there are multiple nodes
> running your application then you would need a distributed lock. I would
> suggest not to update the unique columns. In stead force your users to
> delete the entry and recreate it. If you can't do that you need to evaluate
> your choice of database. Perhaps a relational database would be better
> suited to your requirements.
>
> Hope this helps!
>
> -Ajaya
>
> Cheers,
> Ajaya
>
> On Fri, Feb 27, 2015 at 5:26 PM, ROBIN SRIVASTAVA <
> srivastava.robin71@gmail.com> wrote:
>
>> I want to make unique constraint in cassandra . As i want that all the
>> value in my column be unique in my column family ex: name-rahul phone-123
>> address-abc
>>
>> now i want that in this row no values equal to rahul ,123 and abc get
>> inserted again on searching on datastax i found that i can achieve it by
>> doing query on partition key as IF NOT EXIST ,but not getting the solution
>> for getting all the three values unique means if name- jacob phone-123
>> address-qwe
>>
>> this should also be not inserted into my database as my phone column has
>> the same value as i have shown with name-rahul.
>>
>
>

Re: how to make unique coloumns in cassandra

Posted by Ajaya Agrawal <aj...@gmail.com>.
Make a table for each of the unique keys. For e.g.

If primary key for user table is user_id and you want the phone number
column to be unique then create another table wherein the primary key is
(phone_number, user_id). Before inserting to main table try to insert to
this table first with "if not exists" clause. If it succeeds then go ahead
with your insert to the user table. Similarly while deleting a row from the
primary table delete the corresponding row in all other tables. The order
of insertion to tables matter here otherwise you would end up inducing race
conditions.

The catch here is, you should not be updating the unique column ever. If
you do that you would have to use locks and if there are multiple nodes
running your application then you would need a distributed lock. I would
suggest not to update the unique columns. In stead force your users to
delete the entry and recreate it. If you can't do that you need to evaluate
your choice of database. Perhaps a relational database would be better
suited to your requirements.

Hope this helps!

-Ajaya

Cheers,
Ajaya

On Fri, Feb 27, 2015 at 5:26 PM, ROBIN SRIVASTAVA <
srivastava.robin71@gmail.com> wrote:

> I want to make unique constraint in cassandra . As i want that all the
> value in my column be unique in my column family ex: name-rahul phone-123
> address-abc
>
> now i want that in this row no values equal to rahul ,123 and abc get
> inserted again on searching on datastax i found that i can achieve it by
> doing query on partition key as IF NOT EXIST ,but not getting the solution
> for getting all the three values unique means if name- jacob phone-123
> address-qwe
>
> this should also be not inserted into my database as my phone column has
> the same value as i have shown with name-rahul.
>