单表关联
"單表關聯"要求從給出的數據中尋找所關心的數據,它是對原始數據所包含信息的挖掘。
實例描述 給出child-parent(孩子——父母)表,要求輸出grandchild-grandparent(孫子——爺奶)表。
輸入 輸出:
?
問題分析:
這個實例需要進行單表連接,連接的是左表的parent列和右表的child列,且左表和右表是同一個表。連接結果中除去連接的兩列就是所需要的結果——"grandchild--grandparent"表。要用MapReduce解決這個實例,首先應該考慮如何實現表的自連接;其次就是連接列的設置;最后是結果的整理 MapReduce的shuffle過程會將相同的key會連接在一起,所以可以將map結果的key設置成待連接的列,然后列中相同的值就自然會連接在一起了。
實現步驟:
1.map階段將讀入數據分割成child和parent之后,將parent設置成key,child設置成value進行輸出,并作為左表;再將同一對child和parent中的child設置成key,parent設置成value進行輸出,作為右表。
2.為了區分輸出中的左右表,需要在輸出的value中再加上左右表的信息,比如在value的String最開始處加上字符1表示左表,加上字符2表示右表。
3. reduce接收到連接的結果,其中每個key的value-list就包含了"grandchild--grandparent"關系。取出每個key的value-list進行解析,將左表中的child放入一個數組,右表中的parent放入一個數組,然后對兩個數組求笛卡爾積就是最后的結果了。
代碼:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class connection {
static String INPUT_PATH = "hdfs://master:9000/qq";
static String OUTPUT_PATH="hdfs://master:9000/output";
static class MyMapper extends Mapper<Object,Object,Text,Text>{
Text output_key = new Text();
Text output_value = new Text();
protected void map(Object key,Object value,Context context) throws IOException,InterruptedException{
String[] tokens = value.toString().split(",");
output_key.set(tokens[0]);
output_value.set(2+","+value);
context.write(output_key, output_value);
output_key.set(tokens[1]);
output_value.set(1+","+value);
context.write(output_key,output_value);
}
}
static class MyReduce extends Reducer<Text,Text,Text,Text> {
Text output_key=new Text();
Text output_value=new Text();
protected void reduce(Text key, Iterable<Text> values,Context context) throws IOException,InterruptedException{
List<String> childs= new ArrayList<String>();
List<String> grands= new ArrayList<String>();
for(Text line:values){
String[] tokens=line.toString().split(",");
if(tokens[0].equals("1")){
childs.add(tokens[1]); //tom
//System.out.println(1+"=="+tokens[1]);
}
else if(tokens[0].equals("2")){
grands.add(tokens[2]);
// System.out.println(2+"=="+tokens[2]);
}
}
for(String c:childs)
for(String g:grands){
output_key.set(c);
output_value.set(g);
context.write(output_key, output_value);
}
}
}
public static void main(String[] args) throws Exception{
Path outputpath = new Path(OUTPUT_PATH);
Configuration conf = new Configuration();
FileSystem fs = outputpath.getFileSystem(conf);
if(fs.exists(outputpath)){
fs.delete(outputpath,true);
}
Job job=Job.getInstance(conf);
FileInputFormat.setInputPaths(job,INPUT_PATH);
FileOutputFormat.setOutputPath(job, outputpath);
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReduce.class);
// job.setMapOutputKeyClass(MyNewKey.class);
// job.setMapOutputValueClass(NullWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.waitForCompletion(true);
}
}
最后就可以得出結論。
?
轉載于:https://www.cnblogs.com/luminous1/p/8378713.html
總結
- 上一篇: 前端每周清单第 49 期:Webpack
- 下一篇: 码上公益首秀,让科技更有温度!