Created
          December 20, 2015 20:39 
        
      - 
      
- 
        Save zezutom/f4d214e92e8867a8814b to your computer and use it in GitHub Desktop. 
    Accumulators as counters
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | class TextAnalyser(val sc: SparkContext, ...) { | |
| ... | |
| // Instance variables | |
| val _totalChars = sc.accumulator(0, "Total Characters") | |
| val _totalWords = sc.accumulator(0, "Total Words") | |
| ... | |
| // In a worker thread | |
| def analyse(rdd: RDD[String]): TextStats = { | |
| ... | |
| // This limits serialization to the reference variables only | |
| val totalChars = _totalChars | |
| val totalWords = _totalWords | |
| ... | |
| // Accumulators can be safely used in parallel computations | |
| .map(x => {totalWords += 1; x}) | |
| ... | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment