Join jash

I have two relationships A and B, as with all integer attributes (A {a1, a2, a3, ...} B {b1, b2, b3, ...}. How would I hash the concatenation of these two in java "The user will select two join attributes. Can I create two hash tables and then join them?"

+2


a source to share


2 answers


Well, what form does your relationship take? Are they in a relational database? If so, just use a SQL JOIN - the DBMS will probably make a hash join, but you don't need to worry about that.

If they are not in a relational database, why not?



If some weird constraint prevents you from forming the best tool for the job, then yes, doing a hash join is as simple as putting each tuple in a hash table bound to the join attribute, then iterating over the elements of one and finding matches in friend. If all your data fits into main memory, that is.

+3


a source


Here is his way to make a hash join in Java Your best bet is to hash one table A with a hashmap.

HashMap<Sting, Object[]> hash = new HashMap<String, Object[]>();
for (Object[] a : as) {
    hash.put(a.a1, a);
}

      

Then loop into B using a hash and regroup the matched one.



ArrayList joined = new ArrayList();
for(Objec[] b : bs){
    A a = hash.get(b.b1);
    joined.add(new Object[]{a, b});
}

      

This will only work if each element of table A has a unique a1.

0


a source







All Articles