In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Train of thought & demand
Refer to the implementation idea of IntAccumulatorParam (as mentioned in the above article):
Trait AccumulatorParam [T] extends AccumulableParam [T, T] {def addAccumulator (T1: t, T2: t): t = {/ / addInPlace has many specific implementation classes / / if you want to implement customization, you have to implement this method addInPlace (T1, T2)}}
Customization can also be implemented in this way, which is compatible with our custom accumulator.
Requirements: here to implement a simple case, using a distributed method to achieve random numbers * * custom AccumulatorParam * * Created by lemon on 2018-7-28. * / object UniqueKeyAccumulator extends AccumulatorParam [Map [Int, Int]] {override def addInPlace (R1: Map [Int, Int], R2: Map [Int, Int]): Map [Int, Int] = {/ / + + for adding r1++r2} override def zero (initialValue: Map [Int, Int]): Map [Int, Int] = {var data: Map [Int] Int] = Map () data}} / * use a custom accumulator Realize the random number * * Created by lemon on 2018-7-28. * / object CustomAccumulator {def main (args: Array [String]): Unit = {val sparkConf = new SparkConf () .setAppName ("CustomAccumulator") .setMaster ("local [2]") val sc = new SparkContext (sparkConf) val uniqueKeyAccumulator = sc.accumulable (Map [int]) Int] () (UniqueKeyAccumulator) val distData = sc.parallelize (1 to 10) val mapCount = distData.map (x = > {val randomNum = new Random (). NextInt (20) / construct a Kmurv pair val map: Map [Int, Int] = Map [Int, Int] (randomNum-> randomNum) uniqueKeyAccumulator + = map}) println (mapCount.count ()) / / gets the key value in the value of the accumulator And print uniqueKeyAccumulator.value.keys.foreach (println) sc.stop ()}}
The running result is as follows: # # ideas & requirements
Refer to the implementation idea of IntAccumulatorParam (as mentioned in the above article):
Trait AccumulatorParam [T] extends AccumulableParam [T, T] {def addAccumulator (T1: t, T2: t): t = {/ / addInPlace has many specific implementation classes / / if you want to implement customization, you have to implement this method addInPlace (T1, T2)}}
Customization can also be implemented in this way, which is compatible with our custom accumulator.
Requirements: here to implement a simple case, using a distributed method to achieve random numbers * * custom AccumulatorParam * * Created by lemon on 2018-7-28. * / object UniqueKeyAccumulator extends AccumulatorParam [Map [Int, Int]] {override def addInPlace (R1: Map [Int, Int], R2: Map [Int, Int]): Map [Int, Int] = {/ / + + for adding r1++r2} override def zero (initialValue: Map [Int, Int]): Map [Int, Int] = {var data: Map [Int] Int] = Map () data}} / * use a custom accumulator Realize the random number * * Created by lemon on 2018-7-28. * / object CustomAccumulator {def main (args: Array [String]): Unit = {val sparkConf = new SparkConf () .setAppName ("CustomAccumulator") .setMaster ("local [2]") val sc = new SparkContext (sparkConf) val uniqueKeyAccumulator = sc.accumulable (Map [int]) Int] () (UniqueKeyAccumulator) val distData = sc.parallelize (1 to 10) val mapCount = distData.map (x = > {val randomNum = new Random (). NextInt (20) / construct a Kmurv pair val map: Map [Int, Int] = Map [Int, Int] (randomNum-> randomNum) uniqueKeyAccumulator + = map}) println (mapCount.count ()) / / gets the key value in the value of the accumulator And print uniqueKeyAccumulator.value.keys.foreach (println) sc.stop ()}}
The running result is as follows:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.