REST client that can consume any REST API run-time and persist the data creating new tables dynamically - spring

I'm looking for a solution with Spring / camel to consume multiple REST services during runtime and create tables to store the data from REST API and compare the data dynamically. I don't know the schema for JSON API in advance to generate the JAVA client classes to create JPA persistent entity classes during run time.

You'll need to think through this differently. Id forget about Java class POJOs that you don't have and can't create since the class structure isn't known in advance. So anything with POJO->Entity binding would be pretty useless.
One solution is to simply parse the xml or json body manually with en event-based parser (like SAX for XML) and simply build an SQL create string as you go through the document. Your field and table names would correspond to the tags in the document. Without access to an XSD or other structure description, no meta data is available for field lengths or types. Make everything really long VARCHAR? Also perhaps an XML or other kind of database might suite your problem domain better. In any case, you could include such a thing right in your Camel route as a Processor that will process the body and create the necessary tables if they don't already exist. You could even alter a table for lengths in the process when you have a field value that is longer than what's currently defined.

Related

How to fetch the data from database using spring boot without mapping

I have a database and in that database there are many tables of data. I want to fetch the data from any one of those tables by entering a query from the front-end application. I'm not doing any manipulation to the data, doing just retrieving the data from database.
Also, mapping the data requires writing so many entity or POJO classes, so I don't want to map the data to any object. How can I achieve this?
In this case, assuming the mapping of tables if not relevant, you don't need to use JPA/Hibernate at all.
You can use an old, battle tested jdbc template that can execute a query of your choice (that you'll pass from client), will serialize the response to JSONObject and return it as a response in your controller.
The client side will be responsible to rendering the result.
You might also query the database metadata to obtain the information about column names, types, etc. so that the client side will also get this information and will be able to show the results in a more convenient / "advanced" way.
Beware of security implications, though. Basically it means that the client will be able to delete all the records from the database by a simple query and you won't be able to avoid it :)

Kafka Connect Jdbc Source Data Schema

Does the connector generate a generic Schema such as record with the map of column->value in it, or each table get its own schema that would map to a different class trough binding. That is in the first scenario, trough binding all record across all table would bind to a record class, that contain a map.
I implemented some similar functionality in the past, and what I did was creating a generic record class containing a map with the field and value, which is consistent with what the underlying JDBC API returns.
Although this seems to defy the purpose of schema, hence i wonder how it works.
If anyone could give me some hint and documentation to investigate that, that would be much appreciated

Generic Entity in Spring Data for Couchbase Documents

One advantage of Document DBs like Couchbase is schemaless entities. It gives me freedom to add new attributes within the document without any schema change.
Using Couchbase JsonObject and JsonDocument my code remains generic to perform CRUD operations without any need to modify it whenever new attribute is added to the document. Refer this example where no Entities are created.
However if I follow the usual Spring Data approach of creating Entity classes, I do not take full advantage of this flexibility. I will end up in code change whenever I add new attribute into my document.
Is there a approach to have generic entity using Spring Data? Or Spring Data is not really suitable for schemaless DBs? Or is my understanding is incorrect?
I would argue the opposite is true.
One way or another if you introduce a new field you have to handle the existing data that doesn't have that field.
Either you update all your documents to include that field. That is what schema based stores basically force you to do.
Or you leave your store as it is and let your application handle that issue. With Spring Data you have some nice and obvious ways to handle that in a consistent fashion, e.g. by having a default value in the entity or handling that in a listener.

How to save spring integration message header into database

What would be the easiest and best solution for saving spring header information into database?
I know outbound component that's not the question.
The question is how I can do it without specify-ing header elements in the sql one by one?
Are there any builtin solution for that problem which could adopt the changes when new header elements are added to the message header?
To be honest, there is no such a solution, because with RDBMS we have schema bottleneck.
Even if we could do something like this:
insert into headers value (:headers)
We have to specify any new column in the target DB table.
That's why any NOSQL DB is better for that case. E.g. MongoDb can simply accepts whole Message and stores it to the Document with all headers.
Even if we can Spring Integration scenario make with JPA adapters just to use <jpa:outbound-channel-adapter>, where the message payload is an entity for our header, we need to add a new property to the entity to allow JPA to get deal with them.
However, I think that the new columns in DB isn't en issue for your, and from other side MessageHeader is a Map, so you can iterate over its entries and build INSERT INTO... statement at runtime for each message.
Hope I haven't missed something...

linq to sql classes no dbml

I was wondering if it's possible to get the generated classes like a linq-to-sql class generates classes for every table without using the actual dbml. my situation is as following:
I have an api which gets all data from database and returns them as json.
on the other hand I have an application retrieving that data but it needs to be casted to the right classes again to work with it properly. Ofcourse I could write all those classes myself with the needed properties. But it would be handy if they could be generated. The application does not need any connection to the database itself. This is why I want to protect the database by not using a dbml. with a dbml, a connection is possible. So I just need those classes to cast the json to.
Can I do this or is it impossible?
You can use T4 to generate the classes if that's what you need.
Oleg Sych has a good series of tutorials for this.
Hope it helps!

Resources