How to add multiple columns in cassandra table? - cassandra

I need to add some new columns to my existing column_family/table in cassandra.
I can add single column like this :
ALTER TABLE keyspace_name.table_name ADD column_name cql_type;
Can I add all new columns using a single query? If yes, how to do it using cql and datastax cassandra driver?

This is fixed in Cassandra 3.6
https://issues.apache.org/jira/browse/CASSANDRA-10411
ALTER TABLE foo ADD (colname1 int, colname2 int)

Related

How to add new column in to partition by clause in Hive External Table

I have external Hive Table which is filled by spark job and partitioned by(event_date date) now I have modified the spark code and added one extra column 'country'.In earlier written data country column will have null values as it is newly added. now I want to Alter 'partitioned by' clause as partition by(event_date date,country string) how can I achieve this.Thank you!!
Please try to alter the partition using below commnad-
ALTER TABLE table_name PARTITION part_spec SET LOCATION path
part_spec:
: (part_col_name1=val1, part_col_name2=val2, ...)
Try this databricks spark-sql language manual for alter command

Cassandra simple insert doesn't works

I'm trying to make simple insert in cassandra with Hector client.
I've created simple table:
create table customs (id uuid PRIMARY KEY, char_col_1 varchar);
And try to insert something:
UUID columnId = UUID.randomUUID();
HColumn<String, String> column =
HFactory.createColumn(
"char_col_1",
"test",
12345,
StringSerializer.get(),
StringSerializer.get()
);
Mutator<UUID> mutator = HFactory.createMutator(keyspace, UUIDSerializer.get());
mutator.insert(columnId, "customs", column);
But I always get error:
InvalidRequestException(why:Not enough bytes to read value of component 0)
I think this is because Hector is unable to insert data (through thrift) to a table with non-compact storage. I'm not a hector expert though.
If you create your column family as follows, it might work.
create table customs (id uuid PRIMARY KEY, char_col_1 varchar) with compact storage;
But, why do you need to create a column family in CQL and insert data through hector. That is not a recommended way. You should choose either CQL or thrift.
In your case if you are planning to use hector for all data operations, you can create column family using hector itself or cassandra-cli.
Or if you want CQL only to create column families you can use datastax java driver instead of hector.

How to alter cassandra table columns

I need to add additional columns to a table in cassandra. But the existing table is not empty. Is there any way to update it in a simple way? Otherwise what is the best approach to add additional columns to a non empty table? thx in advance.
There's a good example of adding table columns to an existing table in the CQL documentation on ALTER. The following statement will add the column gravesite (with type varchar) to to the table addamsFamily:
ALTER TABLE addamsFamily ADD gravesite varchar;

How to add columns dynamically in a column family in cassandra using cql

I want to add columns dynamically to this column family via code using cql.
CREATE COLUMN FAMILY blog_entry
WITH comparator = UTF8Type
AND key_validation_class=UTF8Type
AND default_validation_class = UTF8Type;
how shall I do it?
This is becoming something of a FAQ, so I wrote an in-depth explanation: http://www.datastax.com/dev/blog/does-cql-support-dynamic-columns-wide-rows
For this to work you have to first alter the table to add column and then insert will start working. I tried the above on cqlsh and it worked.
alter table newdata add column name varchar;
Please refer below link too:
How to define dynamic column families in cassandra
ALTER TABLE blog_entry ADD newcolumnname text;

Brisk cassandra TimeUUIDType

I used brisk. The cassandra column family automatically maps to Hive tables.
However, if data type is timeuuid in column family, it is unreadable in Hive tables.
For example, I used following command to create an external table in hive to map column family.
Hive > create external table A (rowkey string, column_name string, value string)
> STORED BY 'org.apache.hadoop.hive.cassandra.CassandraStorageHandler'
> WITH SERDEPROPERTIES (
> "cassandra.columns.mapping" = ":key,:column,:value");
If column name is TimeUUIDType in cassandra, it becomes unreadable in the Hive table.
For example, a row in cassandra column family looks like:
RowKey: 2d36a254bb04272b120aaf79d70a3578
=> (column=29139210-b6dc-11df-8c64-f315e3a329d6, value={"event_id":101},timestamp=1283464254261)
Where column name is TimeUUIDType.
In hive table, it looks like the following row:
2d36a254bb04272b120aaf79d70a3578 t��ߒ4��!�� {"event_id":101}
So, column name is unreadable in Hive table.
This is a known issue with the automatic table mapping. For best results with a timeUUIDType, turn the auto-mapping feature off in $brisk_home/resources/hive/hive-site.xml:
"cassandra.autoCreateHiveSchema"
and create the table in hive manually.

Resources