简体   繁体   中英

how to put HashMap in BroadCast variable?

I'm trying to put HashMap in BroadCast Variable, but getting below error

The method broadcast(T, ClassTag<T>) in the type SparkContext is not applicable for the arguments (Map<String,String>, ClassTag<HashMap>)

on below code snippet

Broadcast<HashMap<String, String>> br = ss.sparkContext().broadcast(mp, classTag(HashMap.class));

complete code

        private static <T> ClassTag<T> classTag(Class<T> clazz) {
                return scala.reflect.ClassManifestFactory.fromClass(clazz);
    }

        Dataset<Row> schema = ss.read().format("csv").option("delimeter", ",").option("header", "true")
                .option("quote", "").load("D:\\Data\\schema.csv");
        
        Map<String, String> mp = new HashMap<String, String>();
        
        schema.foreach((ForeachFunction<Row>) row -> {
            String[] names = row.schema().fieldNames();
            
            for (int i = 0; i< names.length; i++) {
                mp.put(names[i], row.getAs(names[i]));
            }
                
        });
        
        Broadcast<HashMap<String, String>> br = ss.sparkContext().broadcast(mp, classTag(HashMap.class));

Am literally stuck on this. Can anyone suggest what am doing wrong?

You are using the Scala version of the SparkContext, you can get the Java version like this:

JavaSparkContext.fromSparkContext(sc).broadcast(new HashMap<String, String>());

No need for a class tag !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM