<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: How to create custom hashmap accumulator in spark using java? in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/How-to-create-custom-hashmap-accumulator-in-spark-using-java/m-p/308607#M223633</link>
    <description>&lt;P&gt;Here's a sample problem and a custom Accumulator solution in java, you could use this as a sample to your own use case.&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Input:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;HashMap&amp;lt;String, String&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Output:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;HashMap&amp;lt;String, Int&amp;gt; that will contain the count for each key in the input HashMaps,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Example:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;Input HashMaps:&lt;/P&gt;&lt;P&gt;1. {"key1", "Value1"}, {"key2", "Value2"}&lt;/P&gt;&lt;P&gt;2. {"key1", "Value2"}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Output:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;{"key1", 2}, {"key2", 1} //Since key1 is repeated 2 times.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Code:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;import org.apache.spark.util.AccumulatorV2;
import java.util.HashMap;

public class CustomAccumulator extends AccumulatorV2&amp;lt;HashMap&amp;lt;String, String&amp;gt;, HashMap&amp;lt;String, Integer&amp;gt;&amp;gt; {

    private HashMap&amp;lt;String, Integer&amp;gt; outputHashMap;

    public CustomAccumulator(){
        this.outputHashMap = new HashMap&amp;lt;&amp;gt;();
    }

    @Override
    public boolean isZero() {
        return outputHashMap.size() == 0;
    }

    @Override
    public AccumulatorV2&amp;lt;HashMap&amp;lt;String, String&amp;gt;, HashMap&amp;lt;String, Integer&amp;gt;&amp;gt; copy() {
        CustomAccumulator customAccumulatorCopy = new CustomAccumulator();
        customAccumulatorCopy.merge(this);
        return customAccumulatorCopy;
    }

    @Override
    public void reset() {
        this.outputHashMap = new HashMap&amp;lt;&amp;gt;();
    }

    @Override
    public void add(HashMap&amp;lt;String, String&amp;gt; v) {
        v.forEach((key, value) -&amp;gt; {
            this.outputHashMap.merge(key, 1, (oldValue, newValue) -&amp;gt; oldValue + newValue);
        });
    }

    @Override
    public void merge(AccumulatorV2&amp;lt;HashMap&amp;lt;String, String&amp;gt;, HashMap&amp;lt;String, Integer&amp;gt;&amp;gt; other) {
        other.value().forEach((key, value) -&amp;gt; {
            this.outputHashMap.merge(key, value, (oldValue, newValue) -&amp;gt; oldValue + newValue);
        });
    }

    @Override
    public HashMap&amp;lt;String, Integer&amp;gt; value() {
        return this.outputHashMap;
    }
}&lt;/LI-CODE&gt;</description>
    <pubDate>Sun, 03 Jan 2021 15:36:40 GMT</pubDate>
    <dc:creator>code_girl</dc:creator>
    <dc:date>2021-01-03T15:36:40Z</dc:date>
  </channel>
</rss>

