Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. import java.io.PrintWriter
  2.  
  3. import cascading.stats.CascadingStats
  4. import com.twitter.scalding._
  5.  
  6. /**
  7. * Writes all custom counters into a tsv file args("counters-file") if this property is set.
  8. *
  9. * Output format:
  10. * counter_name value
  11. */
  12. trait SaveCountersToHdfs extends Job {
  13. private lazy val countersFile = args.optional("counters-file")
  14.  
  15. override protected def handleStats(statsData: CascadingStats) = {
  16. super.handleStats(statsData)
  17. if (statsData.isSuccessful && countersFile.nonEmpty) {
  18. saveCounters(statsData, countersFile.get)
  19. }
  20. }
  21.  
  22. private def saveCounters(statsData: CascadingStats, outputPath: String) = {
  23. implicit val statProvider = statsData
  24. val jobStats = Stats.getAllCustomCounters
  25.  
  26. if (jobStats.nonEmpty) {
  27. writeToFile(outputPath) { writer =>
  28. jobStats.foreach {
  29. case (counter, value) =>
  30. writer.println("%s\t%s".format(counter, value))
  31. }
  32. }
  33. }
  34. }
  35.  
  36. private def writeToFile(location: String)(fn: (PrintWriter => Unit)) = {
  37. import org.apache.hadoop.conf.Configuration
  38. import org.apache.hadoop.fs.{Path, FileSystem}
  39.  
  40. val fs = FileSystem.get(new Configuration())
  41. val path = new Path(location)
  42.  
  43. val writer = new PrintWriter(fs.create(path))
  44. try {
  45. fn(writer)
  46. } finally {
  47. writer.flush()
  48. writer.close()
  49. }
  50. }
  51. }
  52.  
  53. class SampleJob(args: Args) extends Job(args) with SaveCountersToHdfs {
  54. val counterFoo = Stat("sample.foo")
  55. val counterBar = Stat("sample.bar")
  56.  
  57. //implement your job here
  58.  
  59. //when the job finishes SaveCountersToHdfs should write all custom counters to a file args("counters-file")
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement