开发者

Performing Join Operation in Java, on a small sets of data retrieved from the database. (context: web application)

In context of a web application, Is it suitable to do some JOIN operation in Java, on the data retrieved from the database(from first query), and use that "JOIN"ed data for the second and final query to the database to get the required data.

Does Java provide any built in mechanism for such implementations or what could be the best possible way to do this ?

(I know, this is not the best thing to do, but the database is not a SQL database & does not allow for JOIN operations.)

The data for 开发者_高级运维the JOIN operation is: - One dataset contains about 50 integers(avg) and other contains about 150 integers(avg).

The 1st dataset would have list of only integers but the second will have a list of sets of integer & a 'data-integer'. If the two integers in the two datasets intersect then the corresponding 'data-integer' is passed in the results list. So the result would contain the data integers corresponding to the intersected integers.

For e.g. List A ={ 21, 65, 93} List B ={ (21, 42342), (53,73242), (93, 32312)}

Result ={ 42342, 32312 }

Both of the two sets data for the JOIN operation may be implemented in the form of a tree structure. Hence this JOIN operation could be made more efficient because only trees will need to be compared not the entire list (While performing JOIN, if the two (similar depth) nodes do not match at some node in the tree, then all its child nodes will be skipped, and the control will move to the next top node)

Thanks.


For sure, you can do a fast JOIN in Java. I know only about three reasons against doing it:

  • it's additional work
  • the non-matching rows get fetched needlessly
  • the amount of data may be too large to be handled in memory

Without an SQL DB you have no choice, anyway. So, assuming the simplest case, i.e., joining on a condition like A.x = B.x, you can do it easily by computing the intersection using Set.retainAll. For each member of the intersection you need to combine all corresponding rows from the first table with all corresponding rows from the second one.

Assuming there are no more interesting columns in the tables, you're done. Otherwise you need to attach them somehow. In case x is unique in both A and B you can use Maps, otherwise you need a Multiset (there are many implementations available, e.g., in Guava).


Joining data in Java is not a bad idea at all. In some situations it it may be faster and more sensible than doing joins in the database (it depends on many factors). Especially if you have no other choice :-)

If you can get the results from the database as standard jdbc resultsets, then you can use the family of intelligent rowsets: JoinRowSet, FilteredRowSet etc. They gather tabular data from the database and them join and filter, and serialize them on the Java side (bit like ado.net). Standard implementations are not part of the standard, but you will find them in com.sun.rowset package.

If you only need to make this single join, they might be an overkill, but since you are using some nosql db, you might want to standardize the way you filter and join.


Are you asking how to do a set intersection in Java? Given your definition of the data--two datasets, entirely integers--here is my suggestion:

Set a = new HashSet(the list of elements from the first dataset) Set b = new HashSet(the list of elements fro the second dataset)

Set joined = a.retainAll(b); // this is how to do set intersections with java.util.Set

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜