Rosetta Code
samthebest edited this page Jun 26, 2014
·
27 revisions
Pages 37
- Home
- Aggregation using Algebird Aggregators
- All about reducers count
- API Reference
- Automatic Orderings, Monoids and Arbitraries
- Building bigger platforms with scalding
- Calling Scalding from inside your application
- Common Exceptions and possible reasons
- Comparison to Scrunch and Scoobi
- Field rules
- Fields API: reduce functions of GroupBuilder
- Fields based API Reference
- Frequently asked questions
- Getting Started
- Intro to Scalding Jobs
- Introduction to Matrix Library
- Matrix API Reference
- Pig to Scalding
- Powered By
- REPL Reference
- Rosetta Code
- Run in Intellij IDEA
- Scala and sbt for Homebrew users
- Scala and sbt for MacPorts users
- Scald.rb
- Scalding Commons
- Scalding HBase
- Scalding on amazon elastic mapreduce
- Scalding REPL
- Scalding Sources
- Scalding with CDH3U2 in a Maven project
- SQL to Scalding
- Type safe api reference
- Upgrading to 0.9.0
- Using scalding with other versions of scala
- Using the distributed cache
- Why pack unpack and not toList[]
- Show 22 more pages…
Contents
Getting help
Documentation
- Scaladocs
- Getting Started
- Type-safe API Reference
- SQL to Scalding
- Building Bigger Platforms With Scalding
- Scalding Sources
- Scalding-Commons
- Rosetta Code
- Fields-based API Reference (deprecated)
Matrix API
Third Party Modules
Videos
- Scalding: Powerful & Concise MapReduce Programming
- Scalding lecture for UC Berkeley's Analyzing Big Data with Twitter class
- Scalding REPL with Eclipse Scala Worksheets
How-tos
- Scalding with CDH3U2 in a Maven project
- Running your Scalding jobs in Eclipse
- Running your Scalding jobs in IDEA intellij
- Running Scalding jobs on EMR
- Running Scalding with HBase support: Scalding HBase wiki
- Using the distributed cache
- Unit Testing Scalding Jobs
- TDD for Scalding
- Using counters
Tutorials
- Scalding for the impatient
- Movie Recommendations and more in MapReduce and Scalding
- Generating Recommendations with MapReduce and Scalding
- Poker collusion detection with Mahout and Scalding
- Portfolio Management in Scalding
- Find the Fastest Growing County in US, 1969-2011, using Scalding
- Mod-4 matrix arithmetic with Scalding and Algebird
- Dean Wampler's Scalding Workshop
- Typesafe's Activator for Scalding
Articles
- Hive, Pig, Scalding, Scoobi, Scrunch and Spark: A Comparison of Hadoop Frameworks
- Why Hadoop MapReduce needs Scala
- How Twitter is doing its part to democratize big data
- Meet the combo powering Hadoop at Etsy, Airbnb and Climate Corp.
- Scalding wins a Bossie award from InfoWorld
Other
Clone this wiki locally
A collection of MapReduce tasks translated (from Pig, Hive, MapReduce streaming, etc.) into Scalding. For fully runnable code, see the repository here.
Word Count
Hadoop Streaming (Ruby)
# Emit (word, count) pairs.
def mapper
STDIN.each_line do |line|
line.split.each do |word|
puts [word, 1].join("\t")
end
end
end
# Aggregate all (word, count) pairs for a particular word.
#
# In Hadoop Streaming (unlike standard Hadoop), the reducer receives
# rows from the mapper *one at a time*, though the rows are guaranteed
# to be sorted by key (and every row associated to a particular key
# will be sent to the same reducer).
def reducer
curr_word = nil
curr_count = 0
STDIN.each_line do |line|
word, count = line.strip.split("\t")
if word != curr_word
puts [curr_word, curr_count].join("\t")
curr_word = word
curr_count = 0
end
curr_count += count.to_i
end
puts [curr_word, curr_count].join("\t") unless curr_word.nil?
endHive
# tokenizer.py
import sys
for line in sys.stdin:
for word in line.split():
print wordCREATE TABLE tweets (text STRING);
LOAD DATA LOCAL INPATH 'tweets.tsv' OVERWRITE INTO TABLE tweets;
SELECT word, COUNT(*) AS count
FROM (
SELECT TRANSFORM(text) USING 'python tokenizer.py' AS word
FROM tweets
) t
GROUP BY word;Pig
tweets = LOAD 'tweets.tsv' AS (text:chararray);
words = FOREACH tweets GENERATE FLATTEN(TOKENIZE(text)) AS word;
word_groups = GROUP words BY word;
word_counts = FOREACH word_groups GENERATE group AS word, COUNT(words) AS count;
STORE word_counts INTO 'word_counts.tsv';Cascalog 2.0
(cascalog.repl/bootstrap)
(?<- (hfs-textline "word_counts.tsv") [?word ?count]
((hfs-textline "tweets.tsv") ?text)
((mapcatfn [text] (.split text "\\s+")) ?text :> ?word)
(c/count ?count)))Scalding
import com.twitter.scalding._
class ScaldingTestJob(args: Args) extends Job(args) {
Tsv("tweets.tsv", 'text)
.flatMap('text -> 'word)[String, String](_.split("\\s+"))
.groupBy('word)(_.size)
.write(Tsv("word_counts.tsv"))
}Distributed Grep
Hadoop Streaming (Ruby)
PATTERN = /.*hello.*/
# Emit words that match the pattern.
def mapper
STDIN.each_line do |line|
puts line if line =~ PATTERN
end
end
# Identity reducer.
def reducer
STDIN.each_line do |line|
puts line
end
endPig
%declare PATTERN '.*hello.*';
tweets = LOAD 'tweets.tsv' AS (text:chararray);
results = FILTER tweets BY (text MATCHES '$PATTERN');Cascalog
(def pattern #".*hello.*")
(deffilterop matches-pattern? [text pattern]
(re-matches pattern text))
(defn distributed-grep [input pattern]
(<- [?textline]
(input ?textline)
(matches-pattern? ?textline pattern)))
(?- (stdout) (distributed-grep (hfs-textline "tweets.tsv") pattern))Scalding
val Pattern = ".*hello.*";
Tsv("tweets.tsv", 'text).filter('text)[String](_.matches(Pattern))Inverted Index
Hadoop Streaming (Ruby)
# Emit (word, tweet_id) pairs.
def mapper
STDIN.each_line do |line|
tweet_id, text = line.strip.split("\t")
text.split.each do |word|
puts [word, tweet_id].join("\t")
end
end
end
# Aggregate all (word, tweet_id) pairs for a particular word.
#
# In Hadoop Streaming (unlike standard Hadoop), the reducer receives
# rows from the mapper *one at a time*, though the rows are guaranteed
# to be sorted by key (and every row associated to a particular key
# will be sent to the same reducer).
def reducer
curr_word = nil
curr_inv_index = []
STDIN.each_line do |line|
word, tweet_id = line.strip.split("\t")
if word != curr_word
# New key.
puts [curr_word, curr_inv_index.join(",")].join("\t")
curr_word = word
curr_inv_index = []
end
curr_inv_index << tweet_id
end
unless curr_word.nil?
puts [curr_word, curr_inv_index.join(", ")].join("\t")
end
endPig
tweets = LOAD 'tweets.tsv' AS (tweet_id:int, text:chararray);
words = FOREACH tweets GENERATE tweet_id, FLATTEN(TOKENIZE(text)) AS word;
word_groups = GROUP words BY word;
inverted_index = FOREACH word_groups GENERATE group AS word, words.tweet_id;Cascalog
;; define the data
(def index [
[0 "Hello World"]
[101 "The quick brown fox jumps over the lazy dog"]
[42 "Answer to the Ultimate Question of Life, the Universe, and Everything"]
])
;; the tokenize function
(defmapcatop tokenize [text]
(seq (.split text "\\s+")))
;; ensure inverted index is distinct per word
(defbufferop distinct-vals [tuples]
(list (set (map first tuples))))
;; run the query on data
(?<- (stdout) [?word ?ids]
(index ?id ?text)
(tokenize ?text :> ?word)
(distinct-vals ?id :> ?ids))Scalding
val invertedIndex =
Tsv("tweets.tsv", ('id, 'text))
.flatMap(('id, 'text) -> ('word, 'tweetId))[(String, String), (String, String)] {
case (tweetId, text) => text.split("\\s+").map((_, tweetId))
}
.groupBy('word)(_.toList[Long]('tweetId -> 'tweetIds))