日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

单表关联

發布時間:2025/7/14 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 单表关联 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

"單表關聯"要求從給出的數據中尋找所關心的數據,它是對原始數據所包含信息的挖掘。

實例描述 給出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

總結

以上是生活随笔為你收集整理的单表关联的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。