Regular expression search or LIKE type feature in cassandra

淺唱寂寞╮ 提交于 2019-12-11 19:47:04

问题


I am using datastax cassandra ver 2.0. How do we search in cassandra column a value using regular expression.Is there way to achieve 'LIKE' ( as in sQL) functionality ?

I have created table with below schema.

CREATE TABLE Mapping (
id timeuuid,
userid text,
createdDate timestamp,
createdBy text,
lastUpdateDate timestamp,
lastUpdateBy text,
PRIMARY KEY (id,userid)
);

I inserted few test records as below.

       id                                  | userid   | createdby
     -------------------------------------+----------+-----------
      30c78710-c00c-11e3-bb06-1553ee5e40dd |      Jon |     admin
      3e673aa0-c00c-11e3-bb06-1553ee5e40dd |     Jony |     admin
      441c4210-c00c-11e3-bb06-1553ee5e40dd | Jonathan |     admin

I need to search records, where userid contains the word 'jon'.So that in results, i get all records, containing jon,jony,jonathan.

I know,there is no sql LIKE functionality in cassandra. But is there any way to achieve it in cassandra ? (NOTE: I am using datastax-java driver as client api).


回答1:


Are you using DSE or the community version? In case of DSE, consider having a Solr node for these types of queries. If not, maybe use something like lucene / solr as an inverted index outside of cassandra for that particular functionality. That may be a hassle if all you have is cassandra set up, in which case, have a manual inverted index as Ananth suggested. One option is to keep rows of 2-3 character prefixes that hold indices to partitions. You could query those, find the appropriate partitions client side and then issue another query against the target data.




回答2:


You don't have regular expressions check in cql for now. The basic usage of cassandra is having it function like a big data storage. The kind of functionality you had asked for can be done in your code portion in an optimised manner. If you are still persisting on this usage, my suggestion would be this

Column family 1:

Id- an unique id for your userid Name - jonny(or any name you would like to use) combinations- j,jon,jon ,etc and all possible combinations you want

query this and get the appropriate id for your query

Use that id I you column family instead of name directly. Query using that id.

Try to normalise such operations as much as possible. Cassandra is like your base to control. It provides availability of crucial data . Not the flexibility of SQL .




回答3:


There is a lucene index for cassandra. You can use this on the community edition too and perform Regex searches



来源:https://stackoverflow.com/questions/22970427/regular-expression-search-or-like-type-feature-in-cassandra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!