SHOW:
|
|
- or go back to the newest paste.
| 1 | import java.util.ArrayList; | |
| 2 | import java.io.IOException; | |
| 3 | - | public class shitty project extends Reducer<Text, Text, Text, Text> {
|
| 3 | + | import java.util.Collection; |
| 4 | import java.util.Iterator; | |
| 5 | - | private Text friendList = new Text(); |
| 5 | + | import java.util.TreeMap; |
| 6 | import java.lang.*; | |
| 7 | - | @Override |
| 7 | + | |
| 8 | - | public void reduce(final Text key, final Iterable<Text> values, final Context context) throws IOException, InterruptedException {
|
| 8 | + | import org.apache.commons.lang.StringUtils; |
| 9 | import org.apache.hadoop.io.LongWritable; | |
| 10 | - | ArrayList commonFriends = new ArrayList(); |
| 10 | + | import org.apache.hadoop.io.NullWritable; |
| 11 | import org.apache.hadoop.io.Text; | |
| 12 | - | if(values.size()!=2) System.out.println("Error");
|
| 12 | + | import org.apache.hadoop.mapreduce.Reducer; |
| 13 | - | else{
|
| 13 | + | |
| 14 | - | StringTokenizer list1 = new StringTokenizer(values[0].toString()); |
| 14 | + | public void reduce(final Text key, final Iterable<Text> values, final Context context) throws IOException, InterruptedException {
|
| 15 | - | StringTokenizer list2 = new StringTokenizer(values[1].toString()); |
| 15 | + | |
| 16 | - | while (list1.hasMoreTokens()) {
|
| 16 | + | ArrayList commonFriends = new ArrayList(); |
| 17 | - | friend1=list1.nextToken(); |
| 17 | + | ArrayList allcommonFriends = new ArrayList(); |
| 18 | - | while (list2.hasMoreTokens()) {
|
| 18 | + | |
| 19 | - | friend2=list2.nextToken(); |
| 19 | + | for (Text word : values) {
|
| 20 | - | if(friend1==friend2) commonFriends(friend1); |
| 20 | + | //java.lang.String[] friends = word.toString().split(" ");//convert string to string array
|
| 21 | - | } |
| 21 | + | //commonFriends.add(friends); |
| 22 | - | } |
| 22 | + | allcommonFriends.add(word.toString()); |
| 23 | - | } |
| 23 | + | |
| 24 | ||
| 25 | ||
| 26 | //if(values.size()!=2) System.out.println("Error");
| |
| 27 | ||
| 28 | StringTokenizer list1 = new StringTokenizer((String[]) allcommonFriends[0].toArray(); | |
| 29 | StringTokenizer list2 = new StringTokenizer((String[]) allcommonFriends[1].toArray(); | |
| 30 | ||
| 31 | while (list1.hasMoreTokens()) {
| |
| 32 | friend1=list1.nextToken(); | |
| 33 | while (list2.hasMoreTokens()) {
| |
| 34 | friend2=list2.nextToken(); | |
| 35 | if(friend1 == friend2) commonFriends(friend1); | |
| 36 | } | |
| 37 | } | |
| 38 | } | |
| 39 | Collection<String> allValues= commonFriends.values(); | |
| 40 | java.lang.String entireText = StringUtils.join(allValues, ','); | |
| 41 | ||
| 42 | friendList.set(entireText); | |
| 43 | context.write(key, friendList); | |
| 44 | ||
| 45 | } |