-
Notifications
You must be signed in to change notification settings - Fork 707
Intro to Scalding Jobs
Let's look at a simple WordCount job.
import com.twitter.scalding._
class WordCountJob(args : Args) extends Job(args) {
TypedPipe.from(TextLine(args("input")))
.flatMap { line => line.split("""\s+""") }
.groupBy { word => word }
.size
.write(TypedTsv(args("output")))
}
This job reads in a file, emits every word in a line, counts the occurrences of each word, and writes these word-count pairs to a tab-separated file.
To run the job, copy the source code above into a WordCountJob.scala
file, create a file named someInputfile.txt
containing some arbitrary text, and then enter the following command from the root of the Scalding repository:
scripts/scald.rb --local WordCountJob.scala --input someInputfile.txt --output ./someOutputFile.tsv
This runs the WordCount job in local mode (i.e., not on a Hadoop cluster). After a few seconds, your first Scalding job should be done!
Let's take a closer look at the job.
TextLine
is an example of a Scalding source that reads each line of a file into a field named line
.
TextLine(args("input")) // args("input") contains a filename to read from
Another common source is a TypedTsv
source that reads tab-delimited files. You can also create sources that read directly from LZO-compressed files on HDFS (possibly containing Protobuf- or Thrift-encoded objects!), or even database sources that read directly from a MySQL table. See the scalding-commons module for lzo, thrift and protobuf support. Also see scalding-avro if you use avro.
flatMap
is an example of a function that you can apply to a stream of tuples.
TypedPipe.from(TextLine(args("input")))
// flat map the "line" field to a new words separated by one or more space
.flatMap { line => line.split("""\s+""") }
The above works just like calling flatMap on a List in scala.
Our tuple stream now contains something like the following:
input output
this is a line this
line 2 is
a
line
line
2
See the Type-safe api reference for more examples of flatMap
(including how to flat map from and to multiple fields), as well as examples of other functions you can apply to a tuple stream.
Next, we group the same words together, and count the size of each group.
TypedPipe.from(TextLine(args("input")))
.flatMap { line => line.split("""\s+""") }
.groupBy { word => word }
.size
Here, we group the stream into groups of tuples with the same word
, and then we make the value for each keyed group the size of that group.
The tuple stream now looks like:
(line, 2)
(this, 1)
(is, 1)
...
Again, see the Type-safe api reference for more examples of grouping functions.
Finally, just as we read from a TextLine
source, we can also output our computations to a TypedTsv
source.
A TypedTsv can see the types it is putting in each column. We could write TypedTsv[(String, Long)]
below
to be sure we are writing what we intend, but scala can usually infer the types if we leave them off (though, when you get in trouble, try adding the types near the compilation error and see if you can get a better message as to what is going on).
TypedPipe.from(TextLine(args("input")))
.flatMap { line => line.split("""\s+""") }
.groupBy { word => word }
.size
.write(TypedTsv(args("output")))
The scald.rb
script in the scripts/
directory is a handy script that makes it easy to run jobs in both local mode or on a remote Hadoop cluster. It handles simple command-line parsing, and copies over necessary JAR files when running remote jobs.
If you're running many Scalding jobs, it can be useful to add scald.rb
to your path, so that you don't need to provide the absolute pathname every time. One way of doing this is via (something like):
ln -s scripts/scald.rb $HOME/bin/
This creates a symlink to the scald.rb
script in your $HOME/bin/
directory (which should already be included in your PATH).
See scald.rb for more information, including instructions on how to set up the script to run jobs remotely.
You now know the basics of Scalding! To learn more, check out the following resources:
- REPL Example: Try the Alice in Wonderland walkthrough which shows how to use Scalding step by step to learn about the book's text.
- tutorial/: this folder contains an introductory series of runnable jobs.
- API Reference: includes code snippets explaining different kinds of Scalding functions (e.g., map, filter, project, groupBy, join) and much more.
- Matrix API Reference: the API reference for the Type-safe Matrix library
- Cookbook: Short recipes for common tasks.
- 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)
- Scalding: Powerful & Concise MapReduce Programming
- Scalding lecture for UC Berkeley's Analyzing Big Data with Twitter class
- Scalding REPL with Eclipse Scala Worksheets
- 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
- 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