Getting single property from multiple views in couch db using ektorp - java

I am a beginner with couch db and ektorp. I want to use ektorp to retrieve one property from all the existing documents in couch db.
Till now I have learnt that couch db gives us the result of the following equivalent SQL query:-
Select * from Customer;
Is there any ektorp way of retrieving the result set of the following SQL query, Apart from the normal Map/reduce solutions of couch db?
Select name from customer;
The result set of the above query could somehow be retrieved in List or Set<> in java.
Any help would be appreciated.

if you only want one field returned from couchdb you must define a view that only contains that field.
however, it is generally best not to create that fine grained views. load the whole doc instead and transform the result in the application layer instead.

Related

Query MySQL database without having to create models Spring boot

I am using Spring boot to create a web app. I need to store and query a list of patients however the table Patients has 30+ Columns so it is too big to use a java model. I was wondering if there is a way where I can query ( using sql script ) my Patient table and then display the list on html
I'm not sure what you mean by "big model".
I'd recommend using JdbcTemplate f you mean "I don't want all that JPA/Hibernate machinery". It's the lightest, simplest way to interact with relational databases using SQL.
You don't need a complex object model. Map each row into a Map of column name/value pairs and return a List of them for each query. Simple.
I'm not expert but I think you need a model but you can simplified this model. You can add properties as you will use. Incoming SQL query response assign to your simple model. Also I think it's good that you want to simplify the model. It will be useful.

Join Postgresql rows with Mongodb documents based on specific columns

I'm using MongoDB and PostgreSQL in my application. The need of using MongoDB is we might have any number of new fields that would get inserted for which we'll store data in MongoDB.
We are storing our fixed field values in PostgreSQL and custom field values in MongoDB.
E.g.
**Employee Table (RDBMS):**
id Name Salary
1 Krish 40000
**Employee Collection (MongoDB):**
{
<some autogenerated id of mongodb>
instanceId: 1 (The id of SQL: MANUALLY ASSIGNED),
employeeCode: A001
}
We get the records from SQL, and from their ids, we fetch related records from MongoDB. Then map the result to get the values of new fields and send on UI.
Now I'm searching for some optimized solution to get the MongoDB results in PostgreSQL POJO / Model so I don't have to fetch the data manually from MongoDB by passing ids of SQL and then mapping them again.
Is there any way through which I can connect MongoDB with PostgreSQL through columns (Here Id of RDBMS and instanceId of MongoDB) so that with one fetch, I can get related Mongo result too. Any kind of return type is acceptable but I need all of them at one call.
I'm using Hibernate and Spring in my application.
Using Spring Data might be the best solution for your use case, since it supports both:
JPA
MongoDB
You can still get all data in one request but that doesn't mean you have to use a single DB call. You can have one service call which spans to twp database calls. Because the PostgreSQL row is probably the primary entity, I advise you to share the PostgreSQL primary key with MongoDB too.
There's no need to have separate IDs. This way you can simply fetch the SQL and the Mongo document by the same ID. Sharing the same ID can give you the advantage of processing those requests concurrently and merging the result prior to returning from the service call. So the service method duration will not take the sum of the two Repositories calls, being the max of these to calls.
Astonishingly, yes, you potentially can. There's a foreign data wrapper named mongo_fdw that allows PostgreSQL to query MongoDB. I haven't used it and have no opinion as to its performance, utility or quality.
I would be very surprised if you could effectively use this via Hibernate, unless you can convince Hibernate that the FDW mapped "tables" are just views. You might have more luck with EclipseLink and their "NoSQL" support if you want to do it at the Java level.
Separately, this sounds like a monstrosity of a design. There are many sane ways to do what you want within a decent RDBMS, without going for a hybrid database platform. There's a time and a place for hybrid, but I really doubt your situation justifies the complexity.
Just use PostgreSQL's json / jsonb support to support dynamic mappings. Or use traditional options like storing json as text fields, storing XML, or even EAV mapping. Don't build a rube goldberg machine.

JPA Multilingual sorting

Recently I am working on a bilingual project and for some reference tables I need to sort the data. But because it is bilingual, the data is coming from different languages (in my case English and French) and I like to sort them all together, for example, Île comes before Inlet.
Ordinary Order By will put Île at the end of the list. I finally came up with using nativeQuery and sort the data using database engine's function (in oracle is about using NLS_SORT)
But I am tight with database engine and version, so for example if I change my database to postgres then the application will break. I was looking for native JPA solution (if exists) or any other solutions.
To archive this, without use native query JPA definition, I just can see two ways:
Create a DB view which includes escaped/translated columns based on DB functions. So, the DB differences will be on the create view sentence. You can define a OneToOne relation property to original entity.
Create extra column which stores the escaped values and sort by it. The application can perform the escape/translate before store data in DB using JPA Entity Listeners or in the persist/merge methods.
Good luck!

Can i use hibernate named query for large sql without creating entity class?

I have a very large sql query (multiple joins-multiple tables)using which i am retrieving data from multiple tables using hibernate.The sql is kept in a constant java interface as a final string and i am using that in hibernate native sql to execute the query.
Now the query can change.Can i use in as a named query in hibernate-mapping xml file without creating any entity class for that query.
List data= session.getNamedQuery("dummyQuery").list();
Is there any alternatine solution for this?I dont want to keep the large query in a java contant file.
Yes you can absolutely do that. However if you want to map the returned result to POJOs, you'll have to use result set transformer.

Get all the index information for a given schema -- db2

I am writing a JDBC program to fetch some database meta data information and as part of that I want to query all the indexes that are there in a given schema.
I had a look at some JDBC API and from DatabaseMetaData interface, can use methods like getTables to get all the tables for a given schema. I am trying to find something similar (or write using a combination of some API) to get information like all the indexes,views etc on a schema. Is there a way to get it? For ex, for index there is a method - getIndexInfo but for each of the table in a schema, I need to call this method. My database is db2.
I would use the DB2 Catalog Views to get the information.
As an example, if you want all of the indexes for a table, you'd use a query like this (I'm assuming you're using DB2 on Linux/Unix/Windows here):
SELECT *
FROM SYSCAT.INDEXES
WHERE tabname = #tablename
AND tabschema = #schema
ORDER BY indname
I did the following after trying some approaches
1. Wrote a wrapper around JDBC calls to simplify my work.
2. Queried the syscat schema like
select tabname from syscat.TABLES where tabschema = ?
Wrote some java utilities to compare the 2 sets of results returned by the 2 schema and also did some manual comparison.
If I find a better solution, will post it. Thanks a lot for all the help.
Getting data from Syscat schema is not correct. in ZOS environment Syscat may be or may not be present because while installation you have options not to install the Syscat schema. so better use Sysibm schema.

Categories

Resources