{"id":17166719,"url":"https://github.com/willf/scalding_cookbook","last_synced_at":"2025-04-10T20:44:09.886Z","repository":{"id":7120192,"uuid":"8414596","full_name":"willf/scalding_cookbook","owner":"willf","description":null,"archived":false,"fork":false,"pushed_at":"2014-11-05T18:07:53.000Z","size":121,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T20:13:02.905Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/willf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-02-25T17:57:09.000Z","updated_at":"2018-08-29T20:55:54.000Z","dependencies_parsed_at":"2022-08-19T12:11:35.451Z","dependency_job_id":null,"html_url":"https://github.com/willf/scalding_cookbook","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willf%2Fscalding_cookbook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willf%2Fscalding_cookbook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willf%2Fscalding_cookbook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willf%2Fscalding_cookbook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/willf","download_url":"https://codeload.github.com/willf/scalding_cookbook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248294254,"owners_count":21079816,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-14T23:06:28.911Z","updated_at":"2025-04-10T20:44:09.864Z","avatar_url":"https://github.com/willf.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"Scalding Cookbook\n=================\n\n[Scalding](https://github.com/twitter/scalding) is a Scala API for \n[Cascading](http://www.cascading.org/), which is a Java API for Hadoop. \n\"Hadoop is a distributed system for counting words.\"\n\nHere are some recipes. If you want to add a recipe, create a pull request, and I'll\nlikely add it.\n\n**Word Count** Splits on one or more whitespace\n\n```scala\nimport com.twitter.scalding._\n\nclass WordCountJob(args : Args) extends Job(args) {\n  TextLine(args(\"input\"))\n    .flatMap('line -\u003e 'word) { line : String =\u003e line.split(\"\\\\s+\") }\n    .groupBy('word) { _.size }\n    .write(Tsv(args(\"output\")))\n}\n```\n**Word Count, specialized tokenizer**\nFrom the [Scalding intro](https://github.com/twitter/scalding/blob/develop/README.md)\n\n```scala\nimport com.twitter.scalding._\n\nclass WordCountJob(args : Args) extends Job(args) {\n  TextLine( args(\"input\") )\n    .flatMap('line -\u003e 'word) { line : String =\u003e tokenize(line) }\n    .groupBy('word) { _.size }\n    .write( Tsv( args(\"output\") ) )\n\n  def tokenize(text : String) : Array[String] = {\n    // Lowercase each word and remove punctuation.\n    text.toLowerCase.replaceAll(\"[^a-zA-Z0-9\\\\s]\", \"\").split(\"\\\\s+\")\n  }\n}\n```\n\n**Convert TSV file to another TSV file format** Same number of lines, mapping some function over the lines, and\nprojecting out a certain set of columns. This could be written as map(fn).project(columns), but mapTo is ``more efficient.''\n\n```scala\nimport com.twitter.scalding._\n\n// input (tsv)\n// 0   1     2     3    4   5   6\n// 22  kinds of\t   love\tnn2 io  nn1\n// 12  large green eyes\tjj  jj  nn2\n//\n// output (tsv)\n// 22 of    kinds/nn2_love/nn1\n// 12 green large/jj_eyes/nn2\n\nclass contextCountJob(args : Args) extends Job(args) {\n\tval inSchema = ('count, 'w1 ,'w2, 'w3, 'pos1, 'pos2, 'pos3)\n\tval outSchema = ('count, 'word, 'context)\n  Tsv(args(\"input\"),inSchema)\n    .mapTo(inSchema -\u003e outSchema) { parts : (String, String, String, String, String, String, String) =\u003e {\n    \t  val (count, w1, w2, w3, pos1, pos2, pos3) = parts\n    \t\tval context = \"%s/%s_%s/%s\".format(w1,pos1,w3,pos3)\n    \t\t(count, w2, context)\n    \t}\n    }\n    .write(Tsv(args(\"output\")))\n  }\n```\n**Limit or sample the data**  Sometimes, you want to do limit the data you process (for example, in order to \ndo a quick check to see if the output is what you expect. Sometimes, you want to sample a percentage of the data\n(for example, to create an evaluation set). Use ```limit``` to limit the data (this is much like Scala's\n```take```), or ```sample``` to sample it. \n\nA couple of notes:\n\n1. You have to run this job using either hds, or hds-local (this is a limit of Cascading, the underlying system)\n2. Jobs with small limits will not take long to run, but jobs with a sample size set will take as long as processing the entire set\n3. Limit takes an integer (the number of items to take)\n4. Sample takes a double between 0.0 and 1.0, which is the percentage to take. If the number is greater than 1.0, it's treated as 100%\n5. Sample is currently only in the develop branch of Scalding\n\n***Limit*** example\n```scala\nimport com.twitter.scalding._\n\nclass WordCountJob(args : Args) extends Job(args) {\n  TextLine(args(\"input\"))\n    .limit(args(\"limit\").toInt)\n    .flatMap('line -\u003e 'word) { line : String =\u003e line.split(\"\\\\s+\") }\n    .groupBy('word) { _.size }\n    .write(Tsv(args(\"output\")))\n}\n```\n\n***Sample*** example\n```scala\nimport com.twitter.scalding._\n\nclass WordCountJob(args : Args) extends Job(args) {\n  TextLine(args(\"input\"))\n    .sample(args(\"sample\").toDouble)\n    .flatMap('line -\u003e 'word) { line : String =\u003e line.split(\"\\\\s+\") }\n    .groupBy('word) { _.size }\n    .write(Tsv(args(\"output\")))\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillf%2Fscalding_cookbook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillf%2Fscalding_cookbook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillf%2Fscalding_cookbook/lists"}