Not able to alter cassandra db from bigint to text - cassandra

I have column toor_0_sup with the bigint in cassandra db I want to change datatype to text.
How I can do that??
As we know for cassandra we cant change datatype from bigint to text.
If any solution for that will help me.
Tried to alter table but not working.

By this thread discussion Altering column types is no longer supported.
REF: https://dba.stackexchange.com/questions/314933/how-do-we-handle-alter-table-column-type-in-cassandra-in-actual-scenarios
Possible approach:
unload the table data
drop the table
create the table with structure
load the data

Related

How to delete all rows in Cassandra Keyspace

I need to delete all rows in Cassandra but with Amazon Keyspace isn't possible to execute TRUNCATE tbl_name because the TRUNCATE api isn't supported yet.
Now the few ideas that come in my mind are a little bit tricky:
Solution A
select all the rows
cycle all the rows and delete it (one by one or in a batch)
Solution B
DROP TABLE
CREATE TABLE with the structure of the old table
Do you have any idea to keep the process simplest?
Tnx in advance
If the data is not required. Option B - drop the table and recreate. You can pass in the capacity on create table statment using custom table properties.
CREATE TABLE my_keyspace.my_table (
id text,
division text,
project text,
role text,
manager_id text,
PRIMARY KEY (id,division))
WITH CUSTOM_PROPERTIES=
{'capacity_mode':
{'throughput_mode' : 'PROVISIONED',
'read_capacity_units' : 10,
'write_capacity_units' : 20},
'point_in_time_recovery': {'status': 'enabled'}}
AND TAGS={'pii' :'true',
'prod':'true'
};
Option C. If you require the data you can also leverage on-demand capacity mode which is pay-per request mode. With no request you only have to pay for storage. You can change modes once a day.
ALTER TABLE my_keyspace.my_table
WITH CUSTOM_PROPERTIES=
{'capacity_mode': {'throughput_mode': 'PAY_PER_REQUEST'}}
Solution B should be fine in absence of TRUNCATE. In older versions (version prior to 2.1) of Cassandra recreating table with the same name was a problem. Refer article Datastax FAQ Blog. But since then issue has been resolved via CASSANDRA-5202.
If data in table is not required anymore it is better to drop the table and recreate it. Moreover it will be very tedious task if table contains big amount of data.

Saving an existing item in table from dataFrame

I have a dataframes which have few rows among them some already exists in db. I want to update few columns of existing rows. How can we do that?
I see we have SaveModes:
append and override which might serve the purpose but there is a limitation in both the cases.
With append, I am getting primary key error, as this option tries to create a new row in db
With ovverride, I will loose values for the unchanged attributes in the tuple.
Can someone please suggest how can I update few attributes(Columns values) of a row(tuple).?
This can be handled in MySql level, The concept is known as upsert.
case when : primary key is new
The SQL will insert into MySQL DB as new row
Case when : primary key is existing
You can use
INSERT
ON DUPLICATE KEY UPDATE
Which will update the key with the new entries/changes.
Read More here and here.
The ideal way to such use case is, insert your data into a temporary table first in your MySQL DB and post that use a trigger in order to load that data into original table. Call that trigger from spark itself.
In spark, dataframes are immutable. So you cannot change a value in place. One way would be to read the complete table, make the modification and write back the complete table in overwrite mode. This will take time.
If your modifications are always for a particular group, say user id based or date based, then you can write the data based on that column using partitionBy(). Then you can read that partition using .filter() do the modifications and overwrite only that partition using insertInto() - from pyspark 2.3.0
Refer this answer for other versions for pyspark :Overwrite specific partitions in spark dataframe write method

Cassandra : Delete the data without removing the table structure

I'm running some tests on cassandra, that need the data in the table to be removed after every run. So I wrote a script that TRUNCATES the data. My question is, does it leave the table unconfigured(removes all columns) after I TRUNCATE the table? I'm a cassandra beginner. Need some insights on this.
TRUNCATE command remove all data not schema
TRUNCATE table_name;
Removes all data from the specified table immediately and irreversibly, and removes all data from any materialized views derived from that table.
Source : https://docs.datastax.com/en/cql/3.3/cql/cql_reference/cqlTruncate.html

Can we create TABLE datatype in Sybase?

As we have in SQL Server, is it possible to create a TABLE datatype in Sybase? I checked a couple of online forums but was not able to find any answer.
TABLE is a T-SQL reserved word, so is not usable as the name of a datatype.
The full list of reserved words can be found in the Sybase ASE Quick Reference Guide
I had a similar question recently. The table data type does exist in T-SQL as illustrated by MSDN library https://msdn.microsoft.com/en-us/library/ms175010.aspx. HOWEVER, It is NOT SUPPPORTED in Sybase. In T-SQL the table data type can be declared AS
DECLARE #tableVariable table(Column1 int, Column2 int)
I had intended on using the table variable to store a result set for use later to be UNION'd for each value in an array, but I was disappointed to find out that Sybase ASE DOES NOT support the table data type variable. A list of supported data types can be found here
To create a temporary table in Sybase you will use the code:
CREATE TABLE #table (Column1 int, Column2 int)
Example of select:
SELECT * FROM #table
The table exists until the current session or procedure ends, or until its owner drops it using drop table.

Adding columns to a sybase table with unique auto_identity index option

I've inherited a Sybase database that has the 'unique auto_identity index' option enabled on it. As part of an upgrade process I need to add a few extra columns to the tables in this database i.e.
alter table mytable add <newcol> float default -1 not null
When I try to do this I get the follow error:
Column names in each table must be unique, column name SYB_IDENTITY_COL in table #syb__altab....... is specifed more than once
Is it possible to add columns to a table with this property enabled?
Update 1:
I created the following test that replicates the problem:
use master
sp_dboption 'esmdb', 'unique auto_identity indexoption',true
use esmdb
create table test_unique_ids (test_col char)
alter table test_unique_ids add new_col float default -1 not null
The alter table command here produces the error. (Have tried this on ASE 15/Solaris and 15.5/Windows)
Update 2:
This is a bug in the Sybase dbisql interface, which the client tools Sybase Central and Interactive SQL use to access the database and it only appears to affect tables with the 'unique auto_identity index' option enabled.
To work around the problem use a different SQL client (via JDBC for example) to connect to the database or use isql on the command line.
Should be no problem to ALTER TABLE with such columns; the err msg indicates the problem regards something else. I need to see the CREATE TABLE DDL.
Even if we can't ALTER TABLE, which we will try first, there are several work-arounds.
Responses
Hah! Internal Sybase error. Open a TechSupport case.
Workaround:
Make sure you get jthe the exact DDL. sp_help . Note the IDENTITY columns and indices.
Create a staging table, exactly the same. Use the DDL from (1). Exclude the Indices.
INSERT new_table SELECT old_table. If the table is large, break it into batches of 1000 rows per batch.
Now create the Indices.
If the table is very large, AND time is an issue, then use bcp. You need to research that first, I am happy to answer questions afterwards.
When I ran your sample code I first get the error:
The 'select into' database option is not enabled for database 'mydb'. ALTER TABLE with data copy cannot be done. Set the 'select into' database option and re-run
This is no doubt because the data within your table needs copying out because the new column is not null. This will use tempdb I think, and the error message you've posted refers to a temp table. Is it possible that this dboption has been accidentally enabled for the tempdb?
It's a bit of a shot in the dark, as I only have 12.5 to test on here, and it works for me. Or it could be a bug.

Resources