最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用hadoop查詢兩兩之間有共同好友及他倆的共同好友都是誰

 更新時間:2024年10月10日 11:26:58   作者:云游遍天下  
一想到要實現(xiàn)求共同好友的功能,很多人都會想到redis來實現(xiàn)。但是redis存儲和數(shù)據(jù)和計算時需要耗費較多的內(nèi)存資源。所以文本將介紹另一種方法,即利用Hadoop中的MapReduce來實現(xiàn),感興趣的可以了解一下

A:B,C,D,F,E,O
B:A,C,E,K
C:F,A,D,I
D:A,E,F,L
E:B,C,D,M,L
F:A,B,C,D,E,O,M
G:A,C,D,E,F
H:A,C,D,E,O
I:A,O
J:B,O
K:A,C,D
L:D,E,F
M:E,F,G
O:A,H,I,J

該數(shù)據(jù)可以看作好友,例如:A有B,C,D,F,E,O好友;B有A,C,E,K好友,以此類推;

求兩兩之間有共同好友,及他倆的共同好友都是誰,例如:A和B之間共同好友是:C、E

編碼思路:

       第一步是可以把好友當作key,value是擁有key好友的用戶,例如:擁有好友B的是:A,F,J,E用戶

       第二步在第一步結(jié)果后,雙重for循環(huán)進行兩兩之間進行拼接,這樣就可以得出正確結(jié)果

具體代碼實現(xiàn):

第一步:

package com.zsy.mr.commonfriend;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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 commonFriendStepOne {
	static class commonFriendStepOneMapper extends Mapper<LongWritable, Text, Text, Text>{
		Text k = new Text();
		Text v = new Text();
		@Override
		protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
				throws IOException, InterruptedException {
			//通過過冒號分割
			String[] splits = value.toString().split(":");
			//獲取擁有好友的用戶名
			String name = splits[0];
			//獲取該用戶下的好友列表
			String[] friends = StringUtils.isNotBlank(splits[1])?  splits[1].split(","):null;
			if(friends != null) {
				//循環(huán)好友,好友當作key,擁有好友的用戶名當作value
				for (String friend : friends) {
					k.set(friend);
					v.set(name);
					context.write(k, v);
				}
			}
		}
	}
	
	static class commonFriendStepOneReducer extends Reducer<Text, Text, Text, Text>{
		Text v = new Text();
		@Override
		protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)
				throws IOException, InterruptedException {
			List<String> resultList = new ArrayList<String>();//實際生產(chǎn)代碼不建議用list接收,應(yīng)該是直接處理掉
			//處理數(shù)據(jù),該數(shù)據(jù)是擁有key好友的所有用戶
			for (Text value : values) {
				resultList.add(value.toString());
			}
			v.set(StringUtils.join(resultList, ","));
			context.write(key, v);
		}
	}
	
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		/*conf.set("mapreduce.framework.name", "yarn");
        conf.set("yarn.resoucemanger.hostname", "hadoop01");*/
		Job job = Job.getInstance(conf);
		
		job.setJarByClass(commonFriendStepOne.class);
		
		//指定本業(yè)務(wù)job要使用的業(yè)務(wù)類
		job.setMapperClass(commonFriendStepOneMapper.class);
		job.setReducerClass(commonFriendStepOneReducer.class);
		
		//指定mapper輸出的k v類型  如果map的輸出和reduce的輸出一樣,只需要設(shè)置輸出即可
		//job.setMapOutputKeyClass(Text.class);
		//job.setMapOutputValueClass(IntWritable.class);
		
		//指定最終輸出kv類型(reduce輸出類型)
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		
		//指定job的輸入文件所在目錄
		FileInputFormat.setInputPaths(job, new Path(args[0]));
		//指定job的輸出結(jié)果目錄
		FileOutputFormat.setOutputPath(job, new Path(args[1]));

		//將job中配置的相關(guān)參數(shù),以及job所有的java類所在 的jar包,提交給yarn去運行
		//job.submit();無結(jié)果返回,建議不使用它
		boolean res = job.waitForCompletion(true);
		
		System.exit(res?0:1);
	}
}

結(jié)果:

第二步:

代碼實現(xiàn)

package com.zsy.mr.commonfriend;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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 commonFriendStepTwo {

	static class commonFriendStepTwoMapper extends Mapper<LongWritable, Text, Text, Text>{
		Text k = new Text();
		Text v = new Text();
		@Override
		protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
				throws IOException, InterruptedException {
			String[] splits = value.toString().split("\t");
			//獲取好友
			String friend = splits[0];
			//獲取擁有該好友所有的用戶信息
			String[] names = splits[1].split(",");
			//進行排序,防止計算數(shù)據(jù)重復,例如:A-B和B-A其實一個對
			Arrays.sort(names);
			//進行雙重for循環(huán)
			for (int i = 0; i < names.length-1; i++) {
				String string = names[i];
				for (int j = i+1; j < names.length; j++) {
					String string2 = names[j];
					k.set(string+"-"+string2);
					v.set(friend);
					context.write(k, v);
				}
			}
		}
	}
	
	static class commonFriendStepTwoReducer extends Reducer<Text, Text, Text, NullWritable>{
		Text k = new Text();
		@Override
		protected void reduce(Text key, Iterable<Text> value, Reducer<Text, Text, Text, NullWritable>.Context context)
				throws IOException, InterruptedException {
			List<String> resultList = new ArrayList<String>();//實際生產(chǎn)代碼不建議用list接收,應(yīng)該是直接處理掉
			for (Text text : value) {
				resultList.add(text.toString());
			}
			k.set(key.toString()+":"+ StringUtils.join(resultList,","));
			context.write(k, NullWritable.get());
		}
	}
	
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		/*conf.set("mapreduce.framework.name", "yarn");
        conf.set("yarn.resoucemanger.hostname", "hadoop01");*/
		Job job = Job.getInstance(conf);
		
		job.setJarByClass(commonFriendStepTwo.class);
		
		//指定本業(yè)務(wù)job要使用的業(yè)務(wù)類
		job.setMapperClass(commonFriendStepTwoMapper.class);
		job.setReducerClass(commonFriendStepTwoReducer.class);
		
		//指定mapper輸出的k v類型  如果map的輸出和reduce的輸出一樣,只需要設(shè)置輸出即可
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		
		//指定最終輸出kv類型(reduce輸出類型)
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(NullWritable.class);
		
		//指定job的輸入文件所在目錄
		FileInputFormat.setInputPaths(job, new Path(args[0]));
		//指定job的輸出結(jié)果目錄
		FileOutputFormat.setOutputPath(job, new Path(args[1]));

		//將job中配置的相關(guān)參數(shù),以及job所有的java類所在 的jar包,提交給yarn去運行
		//job.submit();無結(jié)果返回,建議不使用它
		boolean res = job.waitForCompletion(true);
		
		System.exit(res?0:1);
	}
}

結(jié)果:

這樣就可以找到正確結(jié)果

總結(jié)

到此這篇關(guān)于利用hadoop查詢兩兩之間有共同好友及他倆的共同好友都是誰的文章就介紹到這了,更多相關(guān)Hadoop求共同好友內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

牡丹江市| 永嘉县| 盘山县| 黄梅县| 壤塘县| 鞍山市| 临清市| 永福县| 牟定县| 大荔县| 湘西| 铁岭县| 荔浦县| 永城市| 华池县| 吉隆县| 长葛市| 南雄市| 依兰县| 嘉义县| 垫江县| 淮南市| 阿勒泰市| 宝山区| 昭觉县| 鹿邑县| 逊克县| 长顺县| 蒙城县| 米脂县| 汨罗市| 河北省| 平邑县| 靖边县| 敖汉旗| 苍溪县| 东乡| 鄄城县| 南充市| 阿瓦提县| 榆林市|