My column family needs two composite columns, the key data type is BytesType.
Here is the definition of table using CQL:
CREATE TABLE stats (
gid blob,
period int,
tid blob,
sum int,
uniques blob,
PRIMARY KEY(gid, period, tid)
);
What I want to do is to create the column family but with Cassandra CLI. Here is my shot.
The structure of the first composite is:
CompositeType(Int32Type, BytesType, AsciiType)
and it will holds an integer.
The structure of the second composite is:
CompositeType(Int32Type, BytesType)
and will holds BytesType.
create column family stats with comparator = 'CompositeType(Int32Type, BytesType, AsciiType)';
I'm not sure how to define the second composite column in create column family command.
Of course I'm assuming that the table created with CQL will generate two composite columns.
You can only have one comparator on a column family in cassandra. This means you can also only have one type of composite column in column family. The table created by the CQL statement you used would actually use the first composite type comparator that you mention:
CompositeType(Int32Type, BytesType, AsciiType)
That comparator can describe all of your schema because of the 'AsciiType' component at the end of your composite. This component of your column names will contain the literal string 'sum' or 'uniques', and the column value will match the type accordingly.
An example using a json style notation:
<bytes> : { # a row key in your stats column family
(100, <bytes>, "sum") : 100, # one column in your row
(100, <bytes>, "uniques") : <bytes>,
(200, <bytes>, "sum") : 200,
(200, <bytes>, "uniques") : <bytes>
}
<bytes> : { # another row in the cf
...
}
Related
how can I transpose a part of a table saving column names as a new column value and merge these columns values into one?
Are there any ways to do this without hardcoding?
For example, I have table with such structure:
I've tried to use crosstab but didn't get how to extract column names. Also have tried to iterate over columns names generating a set of records but main problem is that source table is enormous.
StoreHouse
Product
HasDiscount
IsOutOfStock
JohnStore
chair
False
True
SomeStore
table
True
False
As a result I need output of such structure:
StoreHouse
Product
Parameter
Status
JohnStore
chair
HasDiscount
False
JohnStore
chair
IsOutOfStock
True
SomeStore
table
HasDiscount
True
SomeStore
table
IsOutOfStock
False
Barring dynamic SQL, you need to know the column names and "hardcode" them in your table. Apart from that, you just need a union:
SELECT
"StoreHouse",
"Product",
'HasDiscount' AS "Parameter",
"HasDiscount" AS "Status"
FROM example
UNION ALL
SELECT
"StoreHouse",
"Product",
'IsOutOfStock' AS "Parameter",
"IsOutOfStock" AS "Status"
FROM example
Optionally order by StoreHouse and Product.
I have a table with values like: 123, 123A, 123B, 123C, etc.. I am creating a view and need to separate this column into two based on whether or not the value contains a letter.
EX:
Primary (Column 1)
123
Secondary (Column 2)
123A,
123B,
123C,
...
created a table named "collegetable":
create table collegetable (stid integer primary key not null,stname
varchar(50),department varchar(10),dateofjoin date);
provided values for each column:collegetable data
inserted a new column in it named "cgpa" and tried to add values for this column in one shot using the code:
WITH col(stid, cgpa) as
( VALUES((1121,8.01),
(1131,7.12),
(1141,9.86))
)
UPDATE collegetable as colldata
SET cgpa = col.cgpa
FROM col
WHERE colldata.stid = col.stid;
and got error :
ERROR:operator does not exist:integer=record
LINE9:where colldata.stid=col.stid;
HINT:No operator matches the given name and arguement type.you might need to add explicit type casts.
pls help in solving.thanks in advance.
The with clause only defines the names of the columns, not the data types:
with col (stid, cgpa) as (
...
)
update ...;
For details see the tutorial and the full reference
I would like to know whether it is possible to achieve something like the following in PostgreSQL:
I have a table named Dossier (means "folder" in English), with the the following table structure: name, taille,...
Now, what I'd like to have on my table is a condition such that each time a new instance of Dossier is created, the value in the name column is automatically supplemented with a self-incrementing identifier like so: ref00001, ref00002 etc. (To clarify, after the second insertion, the value for the name column should be ref00002 automatically...)
CREATE SEQUENCE seq_ref;
CREATE TABLE dossier
(
ref TEXT NOT NULL PRIMARY KEY DEFAULT 'ref' || NEXTVAL('seq_ref'),
value TEXT
);
If you want zero-padded numbers, use this:
CREATE SEQUENCE seq_ref;
CREATE TABLE dossier
(
ref TEXT NOT NULL PRIMARY KEY DEFAULT 'ref' || LPAD(NEXTVAL('seq_ref')::TEXT, 10, '0'),
value TEXT
);
In mysql, I can do the following query, UPDATE mytable SET price = '100' WHERE manufacturer='22'
Can this be done with cassandra?
Thanks!
For that you will need to maintain your own index in a separate column family (you need a way to find all products (product ids) that are manufactured by a certatin manufacturer. When you have all the product ids doing a batch mutation is simple).
E.g
ManufacturerToProducts = { // this is a ColumnFamily
'22': { // this is the key to this Row inside the CF
// now we have an infinite # of columns in this row
'prod_1': "prod_1", //for simplicity I'm only showing the value (but in reality the values in the map are the entire Column.)
'prod_1911' : null
}, // end row
'23': { // this is the key to another row in the CF
// now we have another infinite # of columns in this row
'prod_1492': null,
},
}
(disclaimer: thanks Arin for the annotation used above)