Cassandra UDT TYPE remove a field name - cassandra

I have a UDT type in Cassandra. I want to ALTER this type to remove the country field. I don't find any delete or remove field for ALTER TYPE documentation. https://docs.datastax.com/en/cql/3.3/cql/cql_reference/cqlAlterType.html
create type bank_payment (
account_number text,
name text,
city text,
country text,
key text
);
Please help sharing ALTER command to remove one field from above UDT.

Cassandra UDT doesn't store any actual data and its the table where this UDT referred to has actual data. So go ahead and drop the UDT & recreate it with the correct definition that you want. Remember the serialization of old sstables that had this UDT (with additional column) will be different from the new one.
So if possible add a new version name to this UDT as you recreate, say
bank_payment_v2

Related

Frozen keyword in cassandra

No matter if i use or do not use frozen keyword with UDT, it is updating records.
I have table user:
CREATE TABLE demokeyspace.user (
userid text PRIMARY KEY,
address1 frozen<address>,
password text,
uname text
)
and address type:
CREATE TYPE demokeyspace.address (
street text,
city text,
zip_code int
);
It is updating street:'updated street' and city and zip_code to null
As per Cassandra, it does not allow update of UDT values if it is declared as FROZEN.
can somebody help me on this ?
If you use not-frozen UDT, and want to update one field, then you can use following (see docs):
update user set address1.street='updated street' where userid='2';
In your case, you're updating the full address1 field with UDT that has only one value inside...
In case if you're using frozen UDT, you must to specify all values during update, something like you're doing right now, but providing all values.
Use of frozen values is encouraged if you're always updating the full record, but if you'll need to update parts of the record, you shouldn't use it.

Selecting data based on specific column in user-defined type

So I have the following columnfamily with its respective types:
CREATE TYPE subudt (
id varint,
-- snipped --
);
CREATE TYPE udt (
name text,
-- snipped --
subudt frozen <subudt>
);
CREATE COLUMNFAMILY tablename (
id varint,
-- snipped --
udt frozen <udt>,
PRIMARY KEY (id)
);
How can I perform a select query on the name field in the udt type? I was looking around and it seems that you cannot use CREATE INDEX on the udt fields, but only on the entire user defined type itself.
Ideally you model the data in Cassandra based on queries it will serve. Querying for specific fields within UDT, defeats the purpose of having them combined in the first place.
Having secondary indexes will depend on what type of query its trying to solve. The performance varies depending on different query structures explained here.
In short, you can't create Indexes on specific fields and ideally should look at modeling the data different. Its absolutely okay to duplicate the data to serve different query patterns. Say maintaining a whole new table to serve queries based on "names" is common.

How to change the type of a column name in Cassandra?

I read somewhere that Cassandra supports different types for the Column Names unlike RDBMS's out there that supports only Strings.
How do i go about changing the column name of a table in Cassandra?. Or How do i create a table FOO with a Column name as 1985-12-05 ?
You can use alter command. Check https://docs.datastax.com/en/cql/3.1/cql/cql_reference/alter_table_r.html
but 1985-12-05 won't work as Keyspace, column, and table names created using CQL can only contain alphanumeric and underscore characters.
You can use Map column type
CREATE TABLE FOO(
id int,
mymap map<timestamp, text>,
PRIMARY KEY (int));
This will serve as dynamic column for you.

What is the Difference between tuple and user defined type in Cassandra

Can someone tell me the difference between tuple and user defined types in Cassandra
Datastax documents that
You can use a tuple as an alternative to a user-defined type when you
don't need to add new fields.
User-defined types gives you more flexibility with altering the number of fields in case you need to update the data later on, as well as allowing you to give meaningful names to each field. The classic example of how UDTs work is an address.
CREATE TYPE mykeyspace.address (
street_number int,
street text,
city text,
zip_code int,
phones set<text>
);
and creating the table
CREATE TABLE users (
id uuid PRIMARY KEY,
address frozen <address>
);
The tuple equivalent would be
CREATE TABLE users (
id uuid PRIMARY KEY,
address <tuple<int, text, text, int, set<text>>
);
So tuples would be best for a fixed amount of collected data where field names aren't important (an address column is definitely not a good use case; fields matter--street_number and zip_code could potentially be confused--and you wouldn't be able to add detailed fields later on). UDT would allow this, and also let you query by field name.
Furthermore, there is no significant difference in performance.

Cassandra: change type from UUID to TIMEUUID

I'm trying to change the type of a column from UUID to TIMEUUID, but I'm unable to do so.
ALTER TABLE Person ALTER KEY TYPE timeuuid;
Error:
Bad Request: Cannot change key from type uuid to type timeuuid: types are incompatible.
Any idea on how to achieve this without losing the data from the column family?
It is not allowed. This is because TimeUUIDs have a different sorting pattern than regular UUIDs so Cassandra does not allow this. If you don't care about Columns being sorted by time, you can generate and use TimeUUID in your application without changing the column type in Cassandra, but the sorting may be out of order if mixed with those existing columns. If you absolutely need to do this, your only option would be to migrate your existing data and change its columns to UUID to a new column family.

Resources