Last active
April 10, 2020 03:36
-
-
Save Jxck/993388 to your computer and use it in GitHub Desktop.
Hadoop Streaming with Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var stdin = process.openStdin(); | |
var stdout = process.stdout; | |
var input = ''; | |
stdin.setEncoding('utf8'); | |
stdin.on('data', function(data) { | |
if (data) { | |
input += data; | |
while (input.match(/\r?\n/)) { | |
input = RegExp.rightContext; | |
proc(RegExp.leftContext); | |
} | |
} | |
}); | |
stdin.on('end', function() { | |
if (input) { | |
proc(input); | |
} | |
}); | |
function proc(line) { | |
var words = line.split(' '); | |
stdout.write(words[8] + ',1\n'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
ARGF.each do |line| | |
line.chomp! | |
words = line.split(' ') | |
printf("%s,1\n", words[8]) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var stdin = process.openStdin(); | |
var stdout = process.stdout; | |
var counter = {}; | |
var input = ''; | |
stdin.setEncoding('utf8'); | |
stdin.setEncoding('utf8'); | |
stdin.on('data', function(data) { | |
if (data) { | |
input += data; | |
while (input.match(/\r?\n/)) { | |
input = RegExp.rightContext; | |
proc(RegExp.leftContext); | |
} | |
} | |
}); | |
stdin.on('end', function() { | |
if (input) proc(input); | |
for (var k in counter) { | |
stdout.write(k + ':' + counter[k] + '\n'); | |
} | |
}); | |
function proc(line) { | |
var words = line.split(','); | |
var word = words[0]; | |
var count = parseInt(words[1]); | |
if (!counter[word]) counter[word] = 1; | |
else counter[word] += count; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
counter = Hash.new | |
ARGF.each do |line| | |
line.chomp! | |
words = line.split(',') | |
word = words[0] | |
count = words[1].to_i | |
if !counter.key?(word) | |
counter[word] = 1 | |
else | |
counter[word]+=count | |
end | |
end | |
counter.each do |k, v| | |
printf("%s:%d\n", k, v) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment