001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package org.apache.hadoop.mapred.join; 020 021 import java.io.IOException; 022 import java.util.PriorityQueue; 023 024 import org.apache.hadoop.classification.InterfaceAudience; 025 import org.apache.hadoop.classification.InterfaceStability; 026 import org.apache.hadoop.io.Writable; 027 import org.apache.hadoop.io.WritableComparable; 028 import org.apache.hadoop.io.WritableComparator; 029 import org.apache.hadoop.io.WritableUtils; 030 import org.apache.hadoop.util.ReflectionUtils; 031 import org.apache.hadoop.mapred.JobConf; 032 import org.apache.hadoop.mapred.RecordReader; 033 034 /** 035 * Base class for Composite join returning values derived from multiple 036 * sources, but generally not tuples. 037 * @deprecated Use 038 * {@link org.apache.hadoop.mapreduce.lib.join.MultiFilterRecordReader} instead 039 */ 040 @Deprecated 041 @InterfaceAudience.Public 042 @InterfaceStability.Stable 043 public abstract class MultiFilterRecordReader<K extends WritableComparable, 044 V extends Writable> 045 extends CompositeRecordReader<K,V,V> 046 implements ComposableRecordReader<K,V> { 047 048 private Class<? extends Writable> valueclass; 049 private TupleWritable ivalue; 050 051 public MultiFilterRecordReader(int id, JobConf conf, int capacity, 052 Class<? extends WritableComparator> cmpcl) throws IOException { 053 super(id, capacity, cmpcl); 054 setConf(conf); 055 } 056 057 /** 058 * For each tuple emitted, return a value (typically one of the values 059 * in the tuple). 060 * Modifying the Writables in the tuple is permitted and unlikely to affect 061 * join behavior in most cases, but it is not recommended. It's safer to 062 * clone first. 063 */ 064 protected abstract V emit(TupleWritable dst) throws IOException; 065 066 /** 067 * Default implementation offers {@link #emit} every Tuple from the 068 * collector (the outer join of child RRs). 069 */ 070 protected boolean combine(Object[] srcs, TupleWritable dst) { 071 return true; 072 } 073 074 /** {@inheritDoc} */ 075 public boolean next(K key, V value) throws IOException { 076 if (jc.flush(ivalue)) { 077 WritableUtils.cloneInto(key, jc.key()); 078 WritableUtils.cloneInto(value, emit(ivalue)); 079 return true; 080 } 081 jc.clear(); 082 K iterkey = createKey(); 083 final PriorityQueue<ComposableRecordReader<K,?>> q = getRecordReaderQueue(); 084 while (!q.isEmpty()) { 085 fillJoinCollector(iterkey); 086 jc.reset(iterkey); 087 if (jc.flush(ivalue)) { 088 WritableUtils.cloneInto(key, jc.key()); 089 WritableUtils.cloneInto(value, emit(ivalue)); 090 return true; 091 } 092 jc.clear(); 093 } 094 return false; 095 } 096 097 /** {@inheritDoc} */ 098 @SuppressWarnings("unchecked") // Explicit check for value class agreement 099 public V createValue() { 100 if (null == valueclass) { 101 final Class<?> cls = kids[0].createValue().getClass(); 102 for (RecordReader<K,? extends V> rr : kids) { 103 if (!cls.equals(rr.createValue().getClass())) { 104 throw new ClassCastException("Child value classes fail to agree"); 105 } 106 } 107 valueclass = cls.asSubclass(Writable.class); 108 ivalue = createInternalValue(); 109 } 110 return (V) ReflectionUtils.newInstance(valueclass, null); 111 } 112 113 /** 114 * Return an iterator returning a single value from the tuple. 115 * @see MultiFilterDelegationIterator 116 */ 117 protected ResetableIterator<V> getDelegate() { 118 return new MultiFilterDelegationIterator(); 119 } 120 121 /** 122 * Proxy the JoinCollector, but include callback to emit. 123 */ 124 protected class MultiFilterDelegationIterator 125 implements ResetableIterator<V> { 126 127 public boolean hasNext() { 128 return jc.hasNext(); 129 } 130 131 public boolean next(V val) throws IOException { 132 boolean ret; 133 if (ret = jc.flush(ivalue)) { 134 WritableUtils.cloneInto(val, emit(ivalue)); 135 } 136 return ret; 137 } 138 139 public boolean replay(V val) throws IOException { 140 WritableUtils.cloneInto(val, emit(ivalue)); 141 return true; 142 } 143 144 public void reset() { 145 jc.reset(jc.key()); 146 } 147 148 public void add(V item) throws IOException { 149 throw new UnsupportedOperationException(); 150 } 151 152 public void close() throws IOException { 153 jc.close(); 154 } 155 156 public void clear() { 157 jc.clear(); 158 } 159 } 160 161 }