SparkSQL - CassandraSqlContext query on Collection (Set) - java

Lets say I have a table like :
CREATE TABLE USER (
userid ascii,
books set<text>
PRIMARY KEY (userid)
);
and index :
create index on USER (books);
I want to query on books by using sql context. What I am doing is :
CassandraSQLContext cassandraContext = new CassandraSQLContext(sparkContext);
SchemaRDD userTable = cassandraContext.sql("SELECT * FROM keyspace.user");
userTable.registerTempTable("usertable");
Following query does not work for Cassandra :
SchemaRDD userTable = cassandraContext.sql("SELECT * FROM keyspace.user where books CONTAINS 'book1' and books CONTAINS 'book2'");
It returns users with only 'book1'. I've tried similar queries like books CONTAINS ('book1', 'book2') but none of them worked.
What I can do on registered table is :
SchemaRDD users = cassandraContext.sql("SELECT * FROM usertable where userid='some_user_id'");
What I want to do is query by books like :
SchemaRDD users = cassandraContext.sql("SELECT * FROM usertable where books IN ('book1', 'book2')");
or similar queries.
but it doesnt work. It returns 0 records. I tried to register index table named as user_books_idx but it didnt work either. Am I able to query on indexed collections? How can i do it?

It seems the CONTAINS clause only works with one value per statement, so you may not be able to 'and' two different CONTAINS clauses together at one shot.
So I would create one RDD using CONTAINS 'book1', then I'd create another RDD using CONTAINS 'book2', and then I'd do a join of the two RDD's on the userid field. That should give you a resulting RDD of the users with both book1 and book2 in their books set.

Related

How do I Join two tables together in SQLite that can be used in JAVA Sprint Boot?

I am using a Movie Database that will link to the front-end of my website. i am using Java Spring Boot to link the Database. My question is, How can i join a Movies table and Reviews table>]?
try {
String getAllActorsbyMoviesidQuery = "SELECT * FROM tblMovies JOIN tblReviews ON tblReviews.actor_id = tblMovies.actor_id";
statement = dbConnection.createStatement();
ResultSet resultSet = statement.executeQuery(getAllReviewsbyMoviesidQuery);
Joining the 2 tables on actor_id is not probably the suitable way.
I guess there exists inside tblReviews a column like movie_id that refers to the id column of the table tblMovies:
SELECT * FROM tblMovies JOIN tblReviews ON tblReviews.movie_id = tblMovies.id
change the column names to the actual ones.
If you want the reviews for a particular movie, then add a WHERE clause, like:
WHERE tblMovies.id = ?
or
WHERE tblMovies.title = ?

How to select a table by parameters in jasper report using java in eclipse?

I have a button. When I hit it. It creates a table in database with name+billno+date. and inserts all details like product name and etc into the database.
Now I want to when the new table is created, after that jasper report fetch that newly created table and show it in table of jasper report.
For that I created a parameter.
Hashmap param = new Hashmap();
param.put("TABLE" , name+bill+date);
After that I created a table in jasper report and tried to do this query.
select * from $P{TABLE}
But it throws an error.
You have to correct the query using $P!{} syntax (note the ! char between P and {):
SELECT * FROM $P!{TABLE}
Where $P!{TABLE} is replaced with the text value of the parameter (table name in this case).
$P{} syntax can be used in the case the parameter is used as standard SQL parameter, it means that the query can be executed using a prepared statement. For example:
SELECT * FROM bill WHERE id = $P{ID}
Where prepared statement is then:
SELECT * FROM bill WHERE id = ?
Side note: It looks like a little bit crazy scenario to create separate table for each combination of name + billno + date. Imagine that on some day you will need to implement search through all those records - how you will create SELECT query over all those tables?
It would be better to have one table (bill for example) with id column as a primary key, and other columns like name, billnum and date and pass that id into the Jasper report as a parameter. Products can be stored in the related table bill_item and relate them to bill by bill_id column. Then in Jasper report you can SELECT * FROM bill b LEFT JOIN bill_item i ON i.bill_id = b.id WHERE b.id = $P{ID}. But I'm just guessing how can look your data model.

How do I return rows with a specific value first and rest all the values of a table in a Alphabetic order

I need to get book_name and book_code order by book_name in alphabetic order.
I have three tables:
Table1:
Books (Independent Table which will have all the books)
Table2:
Authors (Independent Table which will have all the authors)
Table3:
Author_Books(This table will provide the information about which book is which author's primary book.)
Note: One Author can have many books.
Input to my query is Author_code.
If I pass Author_code I need to get the primary book of that
Author as first row one and rest all books in a table should come order by book_name in alphabetic
If I don't pass any input to my query I need all the books in
alphabetic order.
My query returns author's default book:
SELECT book_CD, book_NAM
from books
where Alpha_NUM_book_CD
= (SELECT Alpha_NUM_book_CD
from Author_Book
JOIN Author
on Author.NUM_Author_CD = Author_Book.NUM_Author_CD
where Author.NUM_Author_CD
= (SELECT NUM_Author_CD
from Author
where ALPHA_3_Author_CD = '')
AND Author_Book.PRIM_book_SW = 'Y'
);
How to write a query which satisfies both my cases?
We are using Oracle 11g.
The order by can take multiple keys. So, something like this:
order by (case when author.NUM_Author_CD = ? then 1 else 2 end),
book.title

Force ebean to not include an ID in a generated query

I'm building a select that has to get me all distinct values from a table.
The sql I would normally write would look like this: "SELECT DISTINCT ARTIST FROM MUSICLIB"
However, ebean is generating the following: "SELECT DISTINCT ID, ARTIST FROM MUSICLIB"
The finder is as such:
find.select("artist").setDistinct(true).findList();
I've found that ebean is generating this ID on every single query, no matter what options I set.
How do I accomplish what I'm looking for?
You can't do that, Ebean for objects mapping requires ID field, and if you won't include it you'll get some mysterious exceptions.
Instead you can query DB without mapping and then write your SQL statement yourself:
SqlQuery sqlQuery = Ebean.createSqlQuery("SELECT DISTINCT artist FROM musiclib");
List<SqlRow> rows = sqlQuery.findList();
for (SqlRow row : rows) {
debug("I got one: " + row.getString("artist"));
}
Of course if artist is a relation, you need to perform additional query using list of found IDs with in(...) expression.

How to map native query to one model class?

Hi I´m using Eclipselink and I did a native query to select some fields of 2 tables. I mapped my table Logins in a model class. I would not like to map my table "B" because I need only 2 fields of this table on my sql result.. can I map this 2 fields in my Logins table to my sql result ?
My sql is this:
select l.login_id, s.lugarcerto,s.vrum, l.username, l.first_name, l.last_name, l.phone, l.fax_number, l.address, l.zip,
l.address2 as 'birth_date', l.city as 'cpf_cnpj'
from Logins l
join (select se.login_id, lugarcerto = min(case when se.service = 'IM' then '1' end), vrum = min(case when se.service = 'VE' then '1' end)
from (select distinct ad.login_id, substring(ap.Rate_code,(CHARINDEX('-', ap.Rate_code)+1),2) as 'service'
from Ad_Data.dbo.ad ad
join Ad_Data.dbo.ad_pub ap on (ad.ad_id = ap.ad_id)
where ap.ad_type =1) se
group by se.login_id) s on (s.login_id = l.login_id)
I did map Logins table and I want to map s.lugarcerto and s.vrum to my SQL query result.
There´s anyway to just add it to my Logins model ?
Not without having mappings for the attributes you want those values put into, and not without causing problems with them being cached in the entity.
Why not just return the values beside the entity, much like you would with a JPQL query such as: "Select l, subquery1, subquery2 from Logins l" ie:
Query q = em.createNativeQuery(yourQueryString, "resultMappingName");
And in the entity, include the annotation:
#SqlResultSetMapping(name="resultMappingName",
entities={#EntityResult(entityClass=com.acme.Logins.class, )},
columns={#ColumnResult(name="LUGARCERTO"), #ColumnResult(name="VRUM")}
)
Best Regards,
Chris

Categories

Resources