Insert Large JSON in a Cassandra Table - cassandra

The image shows error detail- Please click Here
I am trying to Insert large JSON in a column of Cassandra Table.
Table Schema looks like :
Table1 (EmployeeName text, EmployeeID text, EmployeeJSON text )
INSERT INTO Table1 (EmployeeName, EmployeeID, EmployeeJSON)
VALUES ('Razzaq','234',"Jason String")
NOte : JSON string is huge one. It has size of 212k. How can I insert that into this table. Either Should I Use same method or something else?

You could insert it using fromJson() function for a single column value.
It may only be used in the VALUES clause of an INSERT statement or as one of the column values in an UPDATE, DELETE, or SELECT statement. For example, it cannot be used in the selection clause of a SELECT statement.
Example:
Table1 (EmployeeName text, EmployeeID text, EmployeeJSON text )
INSERT INTO Table1 (EmployeeName, EmployeeID, EmployeeJSON)
VALUES ('Razzaq','234',fromJson('{
"employeeCompany" : "Acme Corp",
"employeeCountry" : "Egypt",
"employeeSalary" : [{
"currency" : "Dollar",
"salaryVariance" : { "cashPay" : 90%, "equity" : 10% }
}]
}'))
Json Support in Cassandra

Related

How to find colon separated values from another Table in oracle sql query where clause

I want to return row value from a Table where another table contain Colon separated value.
Suppose
I have a Table name "Unit Name" that Contain unit_id, unit_name
and Table 2 is User_reg where contain User_id. user Id contain colon separator value. Like as 82:81:80
How can get unit name list from unit_name Table
SELECT
*
FROM
unit_name un
WHERE (select school from user_reg where user_mode = 4) is not null
and un.unit_id in
(SELECT regexp_substr( school, '[^:]+', 1, LEVEL ) FROM USER_REG
CONNECT BY regexp_substr( school, '[^:]+', 1, LEVEL ) IS NOT NULL );
If you run the following query, you'll have a delimited string converted to rows.
select * from table(apex_string.split('82:81:80',':'))

How to query '%' character using LIKE operator in Cassandra 3.7?

I use Cassandra 3.7 and have a text column with SASI index.
Let's assume that I want to find column values that contain '%' character somewhere in the middle.
The problem is that '%' is a command char for LIKE clauses.
How to escape '%' char in a query like LIKE '%%%'?
Here is a test script:
DROP keyspace if exists kmv;
CREATE keyspace if not exists kmv WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor':'1'} ;
USE kmv;
CREATE TABLE if not exists kmv (id int, c1 text, c2 text, PRIMARY KEY(id, c1));
CREATE CUSTOM INDEX ON kmv.kmv ( c2 ) USING 'org.apache.cassandra.index.sasi.SASIIndex' WITH OPTIONS = {
'analyzed' : 'true',
'analyzer_class' : 'org.apache.cassandra.index.sasi.analyzer.NonTokenizingAnalyzer',
'case_sensitive' : 'false',
'mode' : 'CONTAINS'
};
INSERT into kmv (id, c1, c2) values (1, 'f22', 'qwe%asd');
SELECT c2 from kmv.kmv where c2 like '%$$%$$%';
The select query returns nothing.
I think you can use the $$ syntax to achieve this. Your where clause would be:
LIKE '%$$%$$%'
Source: https://docs.datastax.com/en/cql/3.3/cql/cql_reference/escape_char_r.html
The percent sign is treated as special characters only at the beginning and the end. So LIKE '%%%' works fine for your case.
cqlsh:kmv> SELECT c2 from kmv.kmv where c2 like '%%%';
c2
----------
qwe%asd
Looking at the source, however, I don't think there is a way to escape the percent sign if it's the first or the last character, which means you can't do like queries to find values that start with %.

Insert data in map<text,text> in cassandra db

i have a column in cassandra database as map<text,text>
I insert the data in this table as :
INSERT INTO "Table1" (col1) VALUES ({'abc':'abc','hello':'world','flag':'true'});
So, in my code i can get the data as :
{
"abc":"abc",
"hello":"world",
"flag":"true"
}
But, now i want this like :
{
"abc":"abc",
"hello":"world",
"flag":{
"data":{ "hi":"cassandra"},
"working":"no"
}
}
For this, when I try the insert query, it says that it does not match the type map<text,text>
How can I make this work ?
The problem here (in your second example) is that the type of col1 is a map<text,text> but flag is a complex type and no longer matches that definition. One way to solve this would be to create individual TEXT columns for each property, as well as a user defined type for flag and the data it contains:
> CREATE TYPE flagtype (data map<text,text>,working text);
> CREATE TABLE table1 (abc text,
hello text,
flag frozen<flagtype>
PRIMARY KEY (abc));
Then INSERTing the JSON text from your second example works.
> INSERT INTO table1 JSON '{"abc":"abc",
"hello":"world",
"flag":{"data":{"hi":"cassandra"},
"working":"no"}}';
> SELECT * FROM table1;
abc | flag | hello
-----+--------------------------------------------+-------
abc | {data: {'hi': 'cassandra'}, working: 'no'} | world
(1 rows)
If you are stuck on using the map<text,text> type, and want the value JSON sub properties to be treated a large text string, you could try a simple table like this:
CREATE TABLE stackoverflow.table2 (
key1 text PRIMARY KEY,
col1 map<text, text>);
And on your INSERTs just escape out the inner quotes:
> INSERT INTO table2 JSON '{"key1":"1","col1":{"abc":"abc","hello":"world"}}';
> INSERT INTO table2 JSON '{"key1":"2","col1":{"abc":"abc","hello":"world",
"flag":"{\"data\":{\"hi\":\"cassandra\"},\"working\":\"no\"}"}}';
> SELECT * FROm table2;
key1 | col1
------+----------------------------------------------------------------------------------------
2 | {'abc': 'abc', 'flag': '{"data":{"hi":"cassandra"},"working":"no"}', 'hello': 'world'}
1 | {'abc': 'abc', 'hello': 'world'}
(2 rows)
That's a little hacky and will probably require some additional parsing on your application side. But it gets you around the problem of having to define each column.

Cassandra: Is there a limit to amount of data that a collection column can hold?

In the below table, what is the maximum size phone_numbers column can accommodate ?
Like normal columns, is it 2GB ?
Is it 64K*64K as mentioned here
CREATE TABLE d2.employee (
id int PRIMARY KEY,
doj timestamp,
name text,
phone_numbers map<text, text>
)
Collection types in Cassandra are represented as a set of distinct cells in the internal data model: you will have a cell for each key of your phone_numbers column. Therefore they are not normal columns, but a set of columns. You can verify this by executing the following command in cassandra-cli (1001 stands for a valid employee id):
use d2;
get employee[1001];
The good answer is your point 2.

Cassandra CQL: Filter the rows between a range of values

The structure of my column family is something like
CREATE TABLE product (
id UUID PRIMARY KEY,
product_name text,
product_code text,
status text,//in stock, out of stock
mfg_date timestamp,
exp_date timestamp
);
Secondary Index is created on status, mfg_date, product_code and exp_date fields.
I want to select the list of products whose status is IS (In Stock) and the manufactured date is between timestamp xxxx to xxxx.
So I tried the following query.
SELECT * FROM product where status='IS' and mfg_date>= xxxxxxxxx and mfg_date<= xxxxxxxxxx LIMIT 50 ALLOW FILTERING;
It throws error like No indexed columns present in by-columns clause with "equals" operator.
Is there anything I need to change in the structure? Please help me out. Thanks in Advance.
cassandra is not supporting >= so you have to change the value and have to use only >(greater then) and <(lessthen) for executing query.
You should have at least one "equals" operator on one of the indexed or primary key column fields in your where clause, i.e. "mfg_date = xxxxx"

Resources