Skip to content

Commit

Permalink
Updating code
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruv3 committed Mar 2, 2018
1 parent d419bff commit 772a90b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
36 changes: 19 additions & 17 deletions src/StubMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,25 @@ public void map(LongWritable key, Text value, Context ctx) throws IOException, I
ArrayList<Long> outUsersList = new ArrayList<Long>();

if(inpLine.length == 2){
String[] subparts = inpLine[1].split(",");
for(int i = 0; i < subparts.length; i++){
Long myConsumer = Long.parseLong(subparts[i]);
outUsersList.add(myConsumer);
Text temp = new Text(myConsumer.toString() + "," + -1);
ctx.write(new LongWritable(inpUser), temp);
}

for (int i = 0; i < outUsersList.size(); i++) {
for (int j = i+1; j < outUsersList.size(); j++) {
Text tempJ = new Text((outUsersList.get(j)).toString() + "," + inpUser.toString());
ctx.write(new LongWritable(outUsersList.get(i)), tempJ);

Text tempI = new Text((outUsersList.get(i)).toString() + "," + inpUser.toString());
ctx.write(new LongWritable(outUsersList.get(j)), tempI);
}
}
String[] subparts = inpLine[1].split(",");
//adding delimiter between User and his direct friends
for(int i = 0; i < subparts.length; i++){
Long myConsumer = Long.parseLong(subparts[i]);
outUsersList.add(myConsumer);
Text temp = new Text(myConsumer.toString() + "," + -1);
ctx.write(new LongWritable(inpUser), temp);
}

//looping through direct friends of User and setting connection between them
for (int i = 0; i < outUsersList.size(); i++) {
for (int j = i+1; j < outUsersList.size(); j++) {
Text tempJ = new Text((outUsersList.get(j)).toString() + "," + inpUser.toString());
ctx.write(new LongWritable(outUsersList.get(i)), tempJ);

Text tempI = new Text((outUsersList.get(i)).toString() + "," + inpUser.toString());
ctx.write(new LongWritable(outUsersList.get(j)), tempI);
}
}
}
}
}
27 changes: 12 additions & 15 deletions src/StubReducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@ public void reduce(LongWritable key, Iterable<Text> values, Context ctx) throws
final HashMap<Long, List<Long>> commonFriendsMap = new HashMap<Long, List<Long>>();

for (Text val : values) {
final String valInp = val.toString();
final long user = Long.parseLong(valInp.split(",")[0]);
final long commonFriend = Long.parseLong(valInp.split(",")[1]);
final String valInp = val.toString();
final long user = Long.parseLong(valInp.split(",")[0]);
final long commonFriend = Long.parseLong(valInp.split(",")[1]);
final Boolean checkFriendFlag;
if(commonFriend == -1){
checkFriendFlag = true;
checkFriendFlag = true;
}
else{
checkFriendFlag = false;
checkFriendFlag = false;
}

if (commonFriendsMap.containsKey(user)) {
if (checkFriendFlag) {
commonFriendsMap.put(user, null);
commonFriendsMap.put(user, null);
}
else if (commonFriendsMap.get(user) != null) {
List<Long> tempList = commonFriendsMap.get(user);
tempList.add(commonFriend);
List<Long> tempList = commonFriendsMap.get(user);
tempList.add(commonFriend);
}
}
else {
if (!checkFriendFlag) {
ArrayList<Long> temp = new ArrayList<Long>();
temp.add(commonFriend);
ArrayList<Long> temp = new ArrayList<Long>();
temp.add(commonFriend);
commonFriendsMap.put(user, temp);
}
else {
Expand Down Expand Up @@ -75,12 +75,9 @@ else if (v1.equals(v2) && key1 < key2) {
}
}

int counter = 0; String output = "";
String output = "";
for (Map.Entry<Long, List<Long>> entry : sortedcommonFriendsMap.entrySet()) {
if(counter == 10)
break;
output += entry.getKey().toString() + ",";
counter++;
}
ctx.write(key, new Text(output));
}
Expand Down

0 comments on commit 772a90b

Please sign in to comment.