Change the type of a column in Cassandra - cassandra

I have created a table my_table with a column phone, which has been declared as of type varint. After entering some data, I realized that it would have been better if I had declared this column as list<int>.
I tried to:
ALTER TABLE my_table
ALTER phone TYPE list<int>
but unfortunately I am not allowed to do so. Hopefully, there is a way to make this change.
UPDATE: Assume that I make a new column phonelist of type list<int>. Is there any efficient way to move the data in the phone column into the phonelist column?

You cannot change the type of an existing column to a map or collection.
The table shows the allowed alterations for data types

Related

Couldnt convert to number when expanding a table power query

I have a very annoying problem when i try to merge two tables on power query excel. I use one column to match records from both tables and when i try to expand the second table it pops up the following message:
DataFormat.Error: We couldn't convert to Number.
Λεπτομέρειες:
ECS
I have no idea how to fix this. The columns that are matched have text, not value. There are no errors when i import data. Is there anyone that can help?
Try the following:
Delete the step #"Changed Type" in both queries
Make sure that the two merged columns have the same type (text ABC, in your case)
When you create a query from a table, Power Query try to guess (based on the first 200 lines) the type of each column. Now, the value "Λεπτομέρειες: ECS" is probably included in a different column (than the two merged) that has Number 123 as a type. It's kind of a mixed column (due to the source of data itself or to a delimiter issue).

Cassandra alter column type: best way with non-compatible types?

I have a large table in Cassandra with a column of type int but no values are outside the range 0-10. I want to reduce the table size by changing the type of the column to tinyint.
This is the error I get
[Query invalid because of configuration issue] message="Cannot change COLUMN_NAME from type int to type tinyint: types are not order-compatible.">
Is there a nice way to handle this with a cast or other such query trickery?
If not ... and without taking the database down, is there a better way to solve this than doing the following?
make a new column of type tinyint
update my code to duplicate data to this column during write operations
copy old data to the new column [will take a while probably]
swap the names of the columns
revert my code change (only update one column)
delete the old int column
I would say deleting old columns and copying data to new columns is not ideal.
If your cassandra column family is accessed by a single entry point (service), my suggestion would be,
Add a new column.
Retain the old column. (You can rename it like COLUMNNAME_OBSOLETE).
After updating your code, only populate the data against new column in your code.
While reading data into domain object, if your new column is null then fill it with old column.
In one of our project, we followed the above steps against prod data and it worked fine. After few months, when we weren't need of COLUMNNAME_OBSOLETE we dropped that column.

Cassandra: Adding new column to the table

Hi I just added a new column Business_sys to my table my_table:
ALTER TABLE my_table ALTER business_sys TYPE set<text>;
But again I just droped this column name because I wanted to change the type of column:
ALTER TABLE my_table DROP business_sys;
Again when I tried to add the same colmn name with different type am getting error message
"Cannnot add a collection with the name business_sys because the collection with the same name and different type has already been used in past"
I just tried to execute this command to add a new column with different type-
ALTER TABLE my_table ADD business_sys list<text>;
What did I do wrong? I am pretty new to Cassandra. Any suggestions?
You're running into CASSANDRA-6276. The problem is when you drop a column in Cassandra that the data in that column doesn't just disappear, and Cassandra may attempt to read that data with its new comparator type.
From the linked JIRA ticket:
Unfortunately, we can't allow dropping a component from the comparator, including dropping individual collection columns from ColumnToCollectionType.
If we do allow that, and have pre-existing data of that type, C* simply wouldn't know how to compare those...
...even if we did, and allowed [users] to create a different collection with the same name, we'd hit a different issue: the new collection's comparator would be used to compare potentially incompatible types.
The JIRA suggests that this may not be an issue in Cassandra 3.x, but I just tried it in 3.0.3 and it fails with the same error.
What did I do wrong? I am pretty new to Cassandra. Any suggestions?
Unfortunately, the only way around this one is to use a different name for your new list.
EDIT: I've tried this out in Cassandra and ended up with inconsistent missing data. Best way to proceed is to change the column name as suggested in CASSANDRA-6276. And always follow documentation guidelines :)
-WARNING-
According to this comment from CASSANDRA-6276, running the following workaround is unsafe.
Elaborating on #masum's comment - it's possible to work around the limitation by first recreating the column with a non-collection type such as an int. Afterwards, you can drop and recreate again using the new collection type.
From your example, assuming we have a business_sys set:
ALTER TABLE my_table ADD business_sys set<text>;
ALTER TABLE my_table DROP business_sys;
Now re-add the column as int and drop it again:
ALTER TABLE my_table ADD business_sys int;
ALTER TABLE my_table DROP business_sys;
Finally, you can re-create the column with the same name but different collection type:
ALTER TABLE my_table ADD business_sys list<text>;
Cassandra doesn't allow you to recreate a column with the same name and the same datatype, but there is an workaround to fix it.
Once you have dropped the column with SET type, you can recreate it with only another "default" type such as varchar or interger.
After recreating with one of those types, you can drop the column once again and finally recreate with the proper type.
I illustrated it below
ALTER TABLE my_table DROP business_sys; # the drop you've done
ALTER TABLE my_table ADD business_sys varchar; # recreating with another type
ALTER TABLE my_table DROP business_sys; # dropping again
ALTER TABLE my_table ADD business_sys list<text>; # recreating with proper type

How to retrieve data from child cells EXCEL

I want to retrieve all items within a specific column of a table.
In this scenario, I have 2 tables, The first table contains a primary key, and the second table contains a foreign key. a 1 to many relationship is set up for the tables respectively.
I want a function/way of retrieving all items within a column in table 2 that has a foreign key that matches the primary key in table 1.
One way of doing this is through a VLOOKUP, though surely through using DAX, or some other function set, I can exploit the relationship I have made in the DataModel to make this easier for me to do.
Why don't you just get the required data from the DB with a proper SELECT statement? Something like
SELECT column
FROM t1, t2
WHERE t1.key = t2.fkey
AND t1.key = 'whatever you search for';
Then you should get the data you want.

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