Following is my Cassandra table structure.
Advertisement
AdvertisementId | Ad_Language | Ad_Caption | Others
----------------------------------------------------------------------
A01(UUID) | EN_US (text)| englishCaption (text) | Other Info(text)
A01(UUID) | FR_CA (text)| French Caption (text) | Other Info (text)
Primary key is (AdvertisementId, Ad_Language);
I am using java to integrate with Cassandra. There is a Java API call to fetch List<advertisements>
Is there a possiblity to fetch the rows like
Query : select * from ad_details orderBy advertisementId; (Unfortunately I cannot specify a col_name that will be used in WHERE or In clause)
I cannot have advertisement Id as cluster key as I need to maintain the UUID as partition key of the composite primary key in Cassandra.
The following query works: Select * from ad_details where advertisementId=xxx orderBy language ASC;
Can someone please help me in carrying out the orderBy advertisementId?
You can not order by a partition key when using the MurMur3partitioner or RandomPartitioner. If you are using an ordered partitioner the results will be in the order of the type specified for the partition key when creating the table.
You can't order by primary key unless you are using IN.
If you are not limiting your search with "where" you probably need to redesign your table as it is not an efficient design, when table gets big you can't query it in efficient way.
Related
I am using postgres 9.4 and have a date range partitioned table. The primary key on the table is id. The tables is partitioned on a field called create_date. We are inserting into the partitions using an insert trigger which looks at the create_date field and writes it into the appropriate partition.
We are using Hibernate 4.3.11 for ORM. To update data in the table we are using the EntityManager::merge method. I would like to add the create_date to the WHERE clause of the update SQL that Hibernate generates.
For example, right now my update looks like:
UPDATE my_table SET col1 = $1 where id = $2
In order to get partition pruning in my update statement I would like my query to look like this:
UPDATE my_table SET col1 = $1 where id = $2 and create_date = $3
Is there any way to accomplish this with Hibernate?
I am storing file data. I read the file though java and when ever i am storing the file data in cassandra it gives me this error.
Exception in thread "main" com.datastax.driver.core.exceptions.SyntaxError: line 1:115 no viable alternative at input 'PRIMARY' (...* from sensorkeyspace.sensortable WHERE [PRIMARY]...)
Here my query is here
CREATE MATERIALIZED VIEW IF NOT EXISTS sensorkeyspace.texttable AS select * from sensorkeyspace.sensortable WHERE PRIMARY KEY (sensor_id) IS NOT NULL
Try altering your WHERE clause to this:
WHERE sensor_id IS NOT NULL PRIMARY KEY (sensor_id)
If you get an error indicating that:
No columns are defined for Materialized View other than primary key
Based on CASSANDRA-13564:
That error message implies you re-used only the partition key/primary key from the base as the partition key for your view (you had no extra clustering columns in your base primary key).
I get that message when I have a table with a simple PRIMARY KEY, and I try to create a view with that same, simple PRIMARY KEY.
For example, if I have this table:
CREATE TABLE stackoverflow.newtable (
name text PRIMARY KEY,
score float,
value float,
value2 blob);
This fails:
cassdba#cqlsh:stackoverflow> CREATE MATERIALIZED VIEW IF NOT EXISTS
stackoverflow.newtable_view AS SELECT * FROM stackoverflow.newtable
WHERE name IS NOT NULL PRIMARY KEY (name);
InvalidRequest: Error from server: code=2200 [Invalid query]
message="No columns are defined for Materialized View other than primary key"
But this works for the same table:
cassdba#cqlsh:stackoverflow> CREATE MATERIALIZED VIEW IF NOT EXISTS
stackoverflow.newtable_view AS SELECT * FROM stackoverflow.newtable
WHERE score IS NOT NULL AND name IS NOT NULL PRIMARY KEY (score,name);
Warnings :
Materialized views are experimental and are not recommended for production use.
Not really related, but do note that last part; about how using MVs in Cassandra really isn't a good idea, yet.
Given the following SQL structure of MY_TABLE:
GROUP_LABEL | FILE | TOPIC
-----------------------------
group A | 1.pdf | topic A
group A | 1.pdf | topic B
group A | 2.pdf | topic A
group B | 2.pdf | topic B
My task is to get this stuff grouped by GROUP_LABEL, while forgetting about the different TOPICs of a file. So my expected result is
GROUP_LABEL | COUNT(*)
----------------------
group A | 2 -- two different files 1.pdf and 2.pdf here
group B | 1 -- only one file here
In pure SQL I would do it like
SELECT GROUP_LABEL, COUNT(*) FROM (
SELECT DISTINCT GROUP_LABEL, FILE FROM MY_TABLE
);
Is it possible to transform it into a JPA Criteria API query? I don't have any idea to get my inner query into the from construct of the Criteria query, in 9.3.1 of https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/querycriteria.html it seems like this is not possible.
But I just can't believe it ;-) Has anyone done this before? The inner query would be enriched with various, well-tested, filter Predicates which I would want to reuse.
I'm using spring-boot-starter-data : 1.5.6.RELEASE with mainly standard configuration.
Try this,
Query: select label, count(distinct file) from tableName group by label;
Criteria: criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("label")).add(Projections.countDistinct("file")));
Firstly your sql query can be resumed to this :
Select distinct GLOBAL_LABEL ,count (distinct FILE) from MY_TABLE group by GLOBAL_LABEL
Secondly it's always good to not name your columns with primary names to avoid problems.
Finaly you can use this as your HQL query (with no magic) :
Select distinct ge.globalLabel,count (distinct ge.file) from GlobalEntity ge group by ge.globalLabel
Yes it is possible by using JPA javax.persistence.criteria API.
Take a look at this example in the official Documentation.
I have a table like this in Cassandra-
CREATE TABLE DATA_HOLDER (USER_ID TEXT, RECORD_NAME TEXT, RECORD_VALUE BLOB, PRIMARY KEY (USER_ID, RECORD_NAME));
I want to count distinct USER_ID in my above table? Is there any way I can do that?
My Cassandra version is:
[cqlsh 4.1.1 | Cassandra 2.0.10.71 | DSE 4.5.2 | CQL spec 3.1.1 | Thrift protocol 19.39.0]
The select expression is defined as:
selection_list
| DISTINCT selection_list
so you can:
SELECT DISTINCT USER_ID FROM DATA_HOLDER;
I would like to know how to create custom setups/teardown mostly to fix cyclyc refence issues where I can insert custom SQL commands with Spring Test Dbunit http://springtestdbunit.github.io/spring-test-dbunit/index.html.
Is there an annotation I can use or how can this be customized?
There isn't currently an annotation that you can use but you might be able to create a subclass of DbUnitTestExecutionListener and add custom logic in the beforeTestMethod. Alternatively you might get away with creating your own TestExecutionListener and just ordering it before DbUnitTestExecutionListener.
Another, potentially better solution would be to re-design your database to remove the cycle. You could probably drop the reference from company to company_config and add a unique index to company_id in the company_config table:
+------------+ 1 0..1 +--------------------------------+
| company |<---------| company_config |
+------------+ +--------------------------------+
| company_id | | config_id |
| ... | | company_id (fk, notnull, uniq) |
+------------+ +--------------------------------+
Rather than looking at company.config_id to get the config you would do select * from company_config where company_id = :id.
Dbunit needs the insert statements (xml lines) in order, because they are performed sequentially. There is no or magic parameter or annotation so dbunit can resolve your cyclyc refences or foreign keys automatically.
The most automate way I could achieve if you your data set contain many tables with foreign keys:
Populate your database with few records. In your example: Company, CompanyConfig and make it sure that the foreign keys are met.
Extract a sample of your database using dbunit Export tool.
This is an snippets you could use:
IDatabaseConnection connection = new DatabaseConnection(conn, schema);
configConnection((DatabaseConnection) connection);
// dependent tables database export: export table X and all tables that have a // PK which is a FK on X, in the right order for insertion
String[] depTableNames = TablesDependencyHelper.getAllDependentTables(connection, "company");
IDataSet depDataset = connection.createDataSet(depTableNames);
FlatXmlWriter datasetWriter = new FlatXmlWriter(new FileOutputStream("target/dependents.xml"));
datasetWriter.write(depDataset);
After running this code, you will have your dbunit data set in "dependents.xml", with all your cycle references fixed.
Here I pasted you the full code: also have a look on dbunit doc about how to export data.