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

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

 更新時(shí)間:2024年10月10日 11:26:58   作者:云游遍天下  
一想到要實(shí)現(xiàn)求共同好友的功能,很多人都會(huì)想到redis來(lái)實(shí)現(xiàn)。但是redis存儲(chǔ)和數(shù)據(jù)和計(jì)算時(shí)需要耗費(fèi)較多的內(nèi)存資源。所以文本將介紹另一種方法,即利用Hadoop中的MapReduce來(lái)實(shí)現(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好友,以此類推;

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

編碼思路:

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

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

具體代碼實(shí)現(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 {
			//通過(guò)過(guò)冒號(hào)分割
			String[] splits = value.toString().split(":");
			//獲取擁有好友的用戶名
			String name = splits[0];
			//獲取該用戶下的好友列表
			String[] friends = StringUtils.isNotBlank(splits[1])?  splits[1].split(","):null;
			if(friends != null) {
				//循環(huán)好友,好友當(dāng)作key,擁有好友的用戶名當(dāng)作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>();//實(shí)際生產(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去運(yùn)行
		//job.submit();無(wú)結(jié)果返回,建議不使用它
		boolean res = job.waitForCompletion(true);
		
		System.exit(res?0:1);
	}
}

結(jié)果:

第二步:

代碼實(shí)現(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(",");
			//進(jìn)行排序,防止計(jì)算數(shù)據(jù)重復(fù),例如:A-B和B-A其實(shí)一個(gè)對(duì)
			Arrays.sort(names);
			//進(jìn)行雙重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>();//實(shí)際生產(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去運(yùn)行
		//job.submit();無(wú)結(jié)果返回,建議不使用它
		boolean res = job.waitForCompletion(true);
		
		System.exit(res?0:1);
	}
}

結(jié)果:

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

總結(jié)

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

相關(guān)文章

  • redis中存儲(chǔ)list<map>,list<entity>的處理

    redis中存儲(chǔ)list<map>,list<entity>的處理

    本文主要介紹了redis中存儲(chǔ)list<map>,list<entity>的處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • 詳解Java的內(nèi)置異常以及創(chuàng)建自定義異常子類的方法

    詳解Java的內(nèi)置異常以及創(chuàng)建自定義異常子類的方法

    這篇文章主要介紹了詳解Java的內(nèi)置異常以及創(chuàng)建自定義異常子類的方法,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-09-09
  • Java將不同的List集合復(fù)制到另一個(gè)集合常見(jiàn)的方法

    Java將不同的List集合復(fù)制到另一個(gè)集合常見(jiàn)的方法

    在Java中,有時(shí)候我們需要將一個(gè)List對(duì)象的屬性值復(fù)制到另一個(gè)List對(duì)象中,使得兩個(gè)對(duì)象的屬性值相同,這篇文章主要介紹了Java將不同的List集合復(fù)制到另一個(gè)集合常見(jiàn)的方法,需要的朋友可以參考下
    2024-09-09
  • Mac配置 maven以及環(huán)境變量設(shè)置方式

    Mac配置 maven以及環(huán)境變量設(shè)置方式

    這篇文章主要介紹了Mac配置 maven以及環(huán)境變量設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • SpringMVC框架和SpringBoot項(xiàng)目中控制器的響應(yīng)結(jié)果深入分析

    SpringMVC框架和SpringBoot項(xiàng)目中控制器的響應(yīng)結(jié)果深入分析

    這篇文章主要介紹了SpringMVC框架和SpringBoot項(xiàng)目中控制器的響應(yīng)結(jié)果,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2022-12-12
  • Spring Boot 整合mybatis 與 swagger2

    Spring Boot 整合mybatis 與 swagger2

    之前使用springMVC+spring+mybatis,總是被一些繁瑣的xml配置,還經(jīng)常出錯(cuò),下面把以前的一些ssm項(xiàng)目改成了spring boot + mybatis,相對(duì)于來(lái)說(shuō)優(yōu)點(diǎn)太明顯了,具體內(nèi)容詳情大家通過(guò)本文學(xué)習(xí)吧
    2017-08-08
  • SpringBoot項(xiàng)目啟動(dòng)錯(cuò)誤:找不到或無(wú)法加載主類的幾種解決方法

    SpringBoot項(xiàng)目啟動(dòng)錯(cuò)誤:找不到或無(wú)法加載主類的幾種解決方法

    本文主要介紹了SpringBoot項(xiàng)目啟動(dòng)錯(cuò)誤:找不到或無(wú)法加載主類的幾種解決方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-03-03
  • spring-boot-maven-plugin:unknown的完美解決方法

    spring-boot-maven-plugin:unknown的完美解決方法

    這篇文章主要介紹了spring-boot-maven-plugin:unknown的完美解決方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • javamail 發(fā)送郵件的實(shí)例代碼分享

    javamail 發(fā)送郵件的實(shí)例代碼分享

    今天學(xué)習(xí)了一下JavaMail,javamail發(fā)送郵件確實(shí)是一個(gè)比較麻煩的問(wèn)題。為了以后使用方便,自己寫(xiě)了段代碼,打成jar包,以方便以后使用
    2013-08-08
  • spring boot發(fā)簡(jiǎn)單文本郵件案例

    spring boot發(fā)簡(jiǎn)單文本郵件案例

    這篇文章主要介紹了spring boot發(fā)簡(jiǎn)單文本郵件案例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10

最新評(píng)論

都安| 门源| 天门市| 兴仁县| 江阴市| 启东市| 汤阴县| 织金县| 兴海县| 金塔县| 天水市| 林西县| 北辰区| 丹巴县| 新建县| 汶上县| 邵武市| 澳门| 梁山县| 江山市| 山阴县| 前郭尔| 井陉县| 奉新县| 墨竹工卡县| 界首市| 铜山县| 年辖:市辖区| 诸暨市| 凉山| 深圳市| 白银市| 吕梁市| 石狮市| 云梦县| 会泽县| 项城市| 拜城县| 马边| 兰坪| 永济市|