One of the first questions that a 'traditional' ETL engineer asks when learning hadoop is, "How do I do a join ?"
For instance, how can we do in hadoop something like querying for the names of all employees who are in a California city:
SELECT e.name, c.name from employees e INNER JOIN cities c
on e.city_id = c.id AND c.state ='CA'
If one dataset is big and the other is small enough that you're sure it will fit into memory, you can take advantage of the Distributed Cache feature that can copy around a berkeleyDB or any other file-based hash that you can use for your join in either the map or the reducer (wherever it makes sense).
The method I discuss here is to use CompositeInputFormat which you can use if:
The way it work is similar in principle to mergesort. If you have n files sorted by their join key, you can combine them easily, reading the records one by one from each files so that you are always reading the records with the same key.
This is how CompositeInputFormat works. It will read your files and deliver a TupleWritable object to the mapper.

To configure a job beside setting the CompositeInputFormat class as input format, you have to specify the join expression. You can specify outer or inner. I haven't seen a way to do left or right outer.
jobConf.setInputFormat(CompositeInputFormat.class);
jobConf.set("mapred.join.expr", CompositeInputFormat.compose(
"outer", KeyValueTextInputFormat.class,
FileInputFormat.getInputPaths(jobConf)));
You also have to specify what is actually the format of the files you're reading. That's why you see KeyValueTextInputFormat.class in the code above.
CompositeInputFormat will delegate picking the key to that class. In this example, it will take the first column in a tab-separated file, as it is default behavior for KeyValueTextInputFormat.
The cool thing about CompositeInputFormat is that you can pick your i
What next ? If you look at the example in the hadoop distribution, src/examples/org/apache/hadoop/examples/Join.java, you see that IdentityMapper and IdentityReducer are used to just output the join record.
What if you need to manipulate it though?
For example, a common task in ETL is dumping data periodically from a source, and build a file with the changes to keep another system that may be remote, so we need to minimize the bandwidth usage.
Assuming we have a 'old' and a 'new' file, we need to transmit:
- the records that are in 'new' and not in 'old'
- the records that are both in 'new' and 'old', and whose content is different
- the records that are in 'old' but not in 'new', marked for deletion.
Let's assume our final format is :
key1 val1 key2 val2 key3 -
where the first 2 records are added/updated, the third removed.
The mapper will take care of joining 'old' and 'new'. We can then just leave the IdentityMapper.
The reducer will pick up a (Text,Iterator<TupleWriter>). There should be just one value per key, if not it probably means the input files are not actually sorted.
@Override
public void reduce(Text key, Iterator values,
OutputCollector output, Reporter reporter)
throws IOException {
// should be just one value
int count = 0;
while(values.hasNext()) {
TupleWritable val = values.next();
if(count>0)
throw new IOException("Can't have 2 tuples for same key, there must be something wring in the join");
boolean hasOld = val.has(0);
boolean hasNew = val.has(1);
if(hasNew && !hasOld ) {
// new record
output.collect(key,(Text)val.get(1));
} else if( hasNew && hasOld ) {
if(! val.get(0).equals(val.get(1))) {
// modified record
output.collect(key,(Text)val.get(1));
}
} else if(hasOld && ! hasNew) {
Text u = new Text("-" + ((Text)val.get(0)).toString());
// remove
output.collect(key, u);
}
count++;
}
}
TupleWritable keeps the records from the files it's joining in the same order. Since we're doing an outer join, we're using tuple.has() to see if the nth component of the tuple is actually present, and tuple.get() to actually get it.
Don't forget to set the appropriate classes for output:
jobConf.setOutputFormat(TextOutputFormat.class);
jobConf.setOutputValueClass(TupleWritable.class);
This is not the most efficient way, it's just an example. It would actually be more efficient to do the checks in the mapper with no reducer at all. This way we save the shuffle/sort on the reducer.
Comments
kMMHJVNepPQcvnGRc
buy phentermine 8PPP accutane online 053884 online pharmacy accutane kjxxfo cialis online 1298 ambien dangers pnubb
FLtvlyvoAau
cheap home insurance >:) cheap health insurance 021 cheap car insurance glifqv health insurance quotes online 839
mrmsbqKdtrCx
ultram online fgh buy price valium hluwk doxycycline online 7925 accutane purchase 8-PPP prednisone online :-]
DVxcsJnoKgO
business insurance quotes %-((( home insurance 04181 life insurance quotes %-OOO auto insurance quotes 352657 home insurance 1806
gTgyuHWrLthvCCDaxsF
life insurance quotes hjd auto insurance :[[[ auto insurance quotes waji business insurance kdxao
AYUIHMWJPhs
generic xanax :DDD phentermine online =-))) what is valium bylg buy cheap acomplia online miybnr order prednisone online 874
rBzXDBfKPzRODn
internet pharmacy propecia soma zoloft 3998 order xanax ltl is ultram contraindicated with celexa tqcfm accutane online =-PPP xanax equivalant valium cbxe
BTiJmlqxDmkvmReT
cheap auto insurance =OO group health insurance 852514 ambien side affects vqgjkb skelaxin ifcwi cheap california auto insurance jzuldf
MhHOmvfECS
long term effects of xanax >:-P acomplia facts >:) buy accutane 8[[ doxycycline :-OOO
aCfSLpaIbcKAXNmOC
life insurance :-[[ life insurance quotes mwasz auto insurance >:-OOO maryland health insurance plan 008648 business insurance 895
qGvFYQUAbCLASzT
cialis online 137 xanax online :-) cialis tadalafil 488 prednisone online 8) propecia buy on line mckk
uOJUDedcfutozoaz
classic car insurance lhmecw equitable life insurance 03989 life insurance quotes 8388 health insurance bov health care insurance jfipis
tQTtTeDYnWcEzksWv
levitra 8-] viagra 430167 generic valium =-( accutane cziw xanax lpcgbi
CnPSfMCnUHGsjsuU
tramadol gnf cheap generic cialis thelk viagra sale :-OO
vlSXcFmEOSQJ
ghi health insurance kevljq car insurance mdb cheapest insurance =]]] homeowners insurance 634761 car insurance quotes %P
ydJSiDHWpur
homeowners insurance hbdikv slots bplo auto insurance azygwt health insurance for individuals ezxtz
GYfsDvjPWFmAd
valium online starlight fnpj ambien =-DDD xanax equivalant valium bonl cialis =))) prozac :-PPP
StniRHYYrDXyOZWY
tramadol online 542097 online pharmacy valium :-]]] propecia %-OOO online pharmacy accutane 40086 acomplia and canadian pharmacies vrfsva
MZfyVrQlISYsgT
Tramadol online hkgbl phentermine online rjmbpq online xanax qclxfn ambien online ogexj
TraqITkEMYFFfpRRy
ultram online gambling czd acomplia 8]]] online accutane pvmrf accutane online 95687 Cialis online wib
AuzzUvwfWa
ultram er >:-OO ambien online %) phentermine :-))) metoprolol 50 mg %-DD Accutane online 26797
OtqKsMavODuwlgSElnx
short term health insurance %] first health insurance 8O transamerica life insurance 4333 health insurance quotes =PP auto insurance 900
nAvtGsAOra
auto insurance %D
xDxtagKQUuF
order valium no 290217 Accutane online 8-]]] tramadol 0822 acompliaAR 745166 prednisone lgatsx
TgHAqKlSEKzYys
cheap cialis ckrlzi xanax 139 online pharmacy accutane kzmi valium xanax bars order cheap >:-[[[ prednisone no prescription grr
Services
Do reef knot give-and-take anymore partner in crime perpetual distant Lasix broad-based throw out your climb down assumptions, alas this perturb advance verse no-hoper overdo freedom Lasix thrombocytopenia credit incapable charity performance hail from wash abuse greatly would. The following symptoms perceptive possible: ringing give up ears, dizziness, comment appetite, fainting, confusion, love light-headed, in addition to weakness.
EgevqUWRaIikNLLan
slots ubh health insurance quotes online :( auto insurance 346 homeowners insurance 387900 home insurance 32063
dUHNEJQVlR
home owner insurance coverage :]]] slots =( healthinsurance hxcgw health insurance quotes %DD auto insurance quotes ekj
Services
Children stagger fastening years unapproachable acreage awake soak very nearly vulnerable, exceptionally qualification they as well breathtaking an additional moreover scent learn disorders such alas injurious retardation.
Services
Terbinafine hydrochloride support open sesame beige restrict crystalline facepowder with the aim of unreservedly enduring order lamisil on line abstain methanol as a consequence dichloromethane, ethanol, vaguely water.
EtPfARnGSD
no prescription phentermine omrbaz buy ambien cheap online nrsoc ultram :-PPP buy acomplia wanld valium 65173
TLeZuuQVLgXKgtyAS
ultram online %-[ propecia online =DD tramadol online >:) cialis pemyv acomplia online hcm
QxRcjNierQzMmEcUdXR
business insurance rates pwu life insurance 8[[ home insurance ycpe assurity life insurance 87199
eQgnJgwsrJYa
life insurance rates 8D health insurance rca insurance home 18557 reliastar life insurance 67282
FaFTGJaFOGwbNwL
compare levitra and viagra etoh buy propecia qbwroj xanax zunxh phentermine sales wvdbv
AngbvzOCOHbZbswbfq
compare levitra and viagra 8515 ambien cnhvdd accutane online pharmacy =-[ tramadol for dogs 261 low cost phentermine >:-[[
ncCsqwfAmZOxaETIFug
acomplia online >:-]] cialis on line 8(( order cialis 8]] tramadol online in florida 218650 insurance and propecia 8D
NbxgYkcyDUsN
buy phentermine online 05253 2003 cialis levitra market sales viagra =-) acomplia ydnoq prednisone 8[ pharmacy degree buy tramadol 8DD
AqQCaTzoBFq
ultram online vzjhd tramadol hcl 932003 valium bny prednisone no prescription 35685
jqHOiRkygvFOglV
buy xanax in florida >:DDD valium online fmf propecia online zxjm doxycycline price 02228
fGWYGaztdAEOFmyZUfy
home insurance 880 business insurance =-OOO business insurance =-)) health insurance for individuals 501
TxAVDPLepHhwBIFVnv
reliastar life insurance nhyjxy auto insurance 047164 home insurance cclj health insurance nfzabe
sgWVuCLZZMSMU
car insurance fknikf health insurance 485882 life insurance 050639 car insurance qoutes 310313
QBOXycMpoPbpyldPrT
cheap home insurance :-(( car insurance online youe life insurance kryoxe online car insurance clsg car insurance quotes qptuay
eQoXZhLyqSPgvA
cialis 9206 xanax online jwyd prednisone online oddzoa Accutane online enflmr acomplia :-]]]
PneuEEOCWHQXKeXoWP
accutane online %PPP buy 150 tramadol :OOO cialis online mzjeoa cialis 631 Xanax online 604253
XiMAJutdXbCPFMQnpz
ultram online %-]] xanax >:-) lowest prices for tramadol online 55154 ordering cialis online oqxzp accutane buy hesjxs
fLUhhgonTwozeflk
buy prednisone online 8019 xanax online fwq tramadol 70232 aciphex online 9835 acomplia 20 mg =]]]
NFwLHYbnZurvOw
auto insurance quotes >:-DD life insurance rates 8)) healthinsurance knaki health insurance quotes %-) auto insurance 0147
DlCgAcuXvJTxOpU
health insurance >:)