I have the json data in a PostgreSQL database column in the following form:
myData [{"key1": "value1", "key2": "value2"}, {"key1": "value1", "key2": "value2"} ]
where myData
is an array containing two objects.
I read the following links for solving my issue with only java but my question is slightly different.
Converting JSON to Java and
Parsing JSON Object in Java
Would it be more efficient to extract key/value pairs in database(postgresql) or should i get the whole string in java and use java libraries? Is doing it in data base even possible?
If doing this in database is an option, does JOOQ provides any support for this?
If parsing in Java is the better option, which of the two libraries given in the above links are more stable/efficient? org.json or Gson
You have a couple of interesting questions here.
It depends. First off, in PostgreSQL specifically, jsonb
has better index support. I'm no expert on the topic, but I'm pretty sure you will come to this conclusion once you benchmark 1-2 things. Here's an interesting blog post comparing different data types for performance in PostgreSQL:
http://www.databasesoup.com/2015/01/tag-all-things.html
... which brings me to the point of whether to put predicates into your SQL query, or execute them later in Java. In general: Put it in the database. If your predicate is very selective, then you can:
Both of which result in much lower latency of your queries. If your predicate is not very selective, then this is hardly ever a problem, except under extreme load. But still, if your system is under extreme load and your predicates are selective, you will still greatly reduce that load if you run the predicate in the database.
jOOQ currently doesn't offer any out-of-the-box support for JSON(B) predicates, but you can easily build a utility yourself using jOOQ's plain SQL support:
http://www.jooq.org/doc/latest/manual/sql-building/plain-sql
In essence, just write:
public static Condition someJsonPredicate(Field<?> someJsonColumn, String someValue) {
return DSL.condition("some_json_predicate({0}, {1})",
someJsonColumn, DSL.val(someValue));
}
I won't answer this part here as your benchmark may differ from mine.