Cassandra CQL 3 - Prefix Select - cassandra

is there a way to perform a select based on a string prefix using CQL3?
For example, consider the following table:
Key | Value
------------
ABC | 0x01
ABD | 0x02
BBB | 0x03
I want to select all the keys with the prefix 'AB'.
The database will be used to store spacial information, using a geohash approach.

That is not possible "out of the box"... However, there are some "tricks" people came up with, see these two posts:
Cassandra (Pycassa/CQL) Return Partial Match
Is there any query for Cassandra as same as SQL:LIKE Condition?
...another (somehow similar) approach could be to define "composite key", where you define some prefixes as "partition key", e.g.: {key1,key2}, where key1 = ABand key2 = ABC... in these situations you could query by "key1" only and get a set of rows (like you want to do), or by "key1" and "key2" (in case you want a specific entry). You can also query only by "key2" (if you add "allow filtering" to your "select" query, however this can lead to "problems" if you have too many rows). Not sure if you can do this with your data...
HTH.

Not built-in in C* but possible with cassandra-lucene-index C* plugin. You can create a lucene index on the column and search the text using prefix search.
UPDATE: Since v3.4 Cassandra introduced SASI indices that offer the required functionality.

Related

Moved from sql server to snowflake finding case sensitive collation issue

in snowflake it searches data with case sensitiveness while in sql server it used to search with case insensitiveness i changed database level collation with below command
ALTER DATABASE IF EXISTS powerdb SET COLLATION = 'en-ci'
but it did not help is there any other way to achive case insensitiveness
There's a number of ways really.
one of them is using ILIKE for your string comparison: https://docs.snowflake.net/manuals/sql-reference/functions/ilike.html
another one is setting up collation at column level:
https://docs.snowflake.net/manuals/sql-reference/collation.html
- but please note that not all of the string functions are supported on collated columns
also you can use COLLATION functions (also described in the link below) or set it on database level with account-level parameter of DEFAULT_DDL_COLLATION = 'en-ci'
everything depends on what you want to achieve really...

select string literal in cassandra cql

I am new to Cassandra and I am trying to run a simple query in CQL:
select aggregate_name as name, 'test' as test from aggregates;
and I get an error: Line 1: no viable alternative at input ''test''
The question is: how could I select string literal in Apache Cassandra?
I found an ugly workaround, if you really want to print a text value as a column:
cqlsh> select aggregate_name as name, blobAsText(textAsBlob('test')) as test from aggregates;
name | test
------+------
dude | test
CQL supports native Cassandra functions as a select_expression, so you can convert your string literal to a blob and back again as shown above. (source)

Cassandra - Do we have any thing similar to MySQL %like%? [duplicate]

This question already has answers here:
Wildcard search in cassandra database
(2 answers)
Closed 8 years ago.
I have a table and data as shown below, I want to query records containing owner. Any help please ?
select name from team where blob like %owner%
CREATE TABLE ipl.team(
name text,
captain text,
blob text,
PRIMARY KEY (name, captain)
)
name | captain | blob
------------+-------------------+----------------------------------------
KKR | SRK | {'owner': 'ABC', 'win': '10'}
DD | ME | {'owner': 'XYZ', 'win': '8'}
Although I'm hoping you are not really storing JSON in a text field (it's a bit of a waste), I will try to answer.
In short, no, it does not. It is a key/value store, although with a more complicated storage model than, for example, Riak.
If you want to do something like that, you should implement a text search engine like elastic search, which is a separate topic altogether. You might want to check this quesiton: Elasticsearch vs Cassandra vs Elasticsearch with Cassandra

conditional query is not working in cql 3.0.0

i am trying to execute conditional query in cassandra CQL but it is giving me error like
cqlsh:events> select * from standardevents where name=ActivityPg_view;
i am executing above query, it is giving me below error
Bad Request: line 1:55 no viable alternative at input ';'
Perhaps you meant to use CQL 2? Try using the -2 option when starting cqlsh.
cqlsh:events> select * from standardevents where name='ActivityPg_view';
i am executing above query, it is giving me below error
Bad Request: No indexed columns present in by-columns clause with
Equal operator
when i am trying to execute using CQL 2
cqlsh:events> select * from standardevents where name=ActivityPg_view;
it is giving me below error,
Bad Request: No indexed columns present in by-columns clause with "equals" opera
tor
cqlsh:events> select * from standardevents where name='ActivityPg_view';
it is giving me below error,
Bad Request: No indexed columns present in by-columns clause with "equals" opera
tor
Perhaps you meant to use CQL 3? Try using the -3 option when starting cqlsh.
so can somebody suggest the problem and how to solve this thing and execute the conditional query?
cqlsh> DESCRIBE COLUMNFAMILY events.standardevents;
CREATE TABLE standardevents (
uuid timeuuid PRIMARY KEY,
data text,
name text,
time text,
tracker text,
type text,
userid text
) WITH bloom_filter_fp_chance=0.010000
AND caching='KEYS_ONLY'
AND comment=''
AND dclocal_read_repair_chance=0.000000
AND gc_grace_seconds=864000
AND read_repair_chance=0.100000
AND replicate_on_write='true'
AND populate_io_cache_on_flush='false'
AND compaction={'class': 'SizeTieredCompactionStrategy'}
AND compression={'sstable_compression': 'SnappyCompressor'};
Your table (CF) has no field called courseid (hence the error "Undefined name courseid in where clause"). You can't query on something that doesn't exist.
Were you expecting that field there? (Your PK for that table is called 'uuid' if that helps) Are you querying the right table? Not much else to suggest.
Edit: After update
Your CQL3 attempt is missing the quotes around the name, but that aside ... In Cassandra you can't randomly query by non key columns. Cassandra is a partitioned row store, and it is not really designed to do query's in the manner you are trying.
You could add a secondary index to fix this, but you should be aware it not like in traditional SQL. Having a 2ndary index will need to hit all nodes in a cluster do to the query. It's also not ideal if your data has high cardinality.
In Cassandra the general premise is that storage is cheap, and you should base your model on your query rather than your data. Denormalise everything. For example, if you need to pull events by name, then you should make a table that is key'd by name, and includes all the event data you need. That way reading them is (essentially) and O(1) operation.
To be able to restrict by your name column you would need to add a secondary index to it.
Example:
CREATE INDEX standardevents_index ON standardevents (name);

Syntax error at position 7: unexpected "*" for `Select * FROM mytable;`

I write because I've a problem with cassandra; after have imported the data from pentaho as show here
http://wiki.pentaho.com/display/BAD/Write+Data+To+Cassandra
when I try to execute the query
Select * FROM mytable;
cassandre give me an error message
Syntax error at position 7: unexpected "*" for Select * FROM mytable;.
and don't show the results of query.Why? what does it mean that error?
the step that i make are the follow:
start cassandra cli utility;
use keyspace added from pentaho; (use tpc_h);
select to show the data added (Select * FROM mytable;)
The cassandra-cli does not support any CQL version. It has its own syntax which you can find on datastax's website.
Just for clarity, in cql to select everything from a table (aka column-family) called mytable stored in a keyspace called myks you would use:
SELECT * FROM myks.mytable;
The equivalent in cassandra-cli would *roughly be :
USE myks;
LIST mytable;
***** In the cli you are limited to selecting the first 100 rows. If this is a problem you can use the limit clause to specify how many rows you want:
LIST mytable limit 10000;
As for this:
in cassandra i have read that isn't possible make the join such as sql, ther isn't a shortcut to issue this disadvantage
There is a reason why joins don't exist in Cassandra, its for the same reason that C* isn't ACID compliant, it sacrifices that functionality for it's amazing performance and scalability, so it's not a disadvantage, you just need to re-think your model if you need joins. Also take a look at this question / answer.

Resources