Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

java - Spark : get specific value of the column map if the key exists

I have a column "price_map" in my dataset and it's a HashMap that is like this :

(usd_price,100),
(eur_price,200),
(jpy_price,500)...

Now I want to retrieve the price value of usd_price (i.e. 100), when the key usd_price exists in the map, and put it as a new row in my dataset.

I tried something like this:

    dataset = dataset.withColumn("usd_price", when(
            size(map_keys(dataset.col(price_map))),
            map_values(dataset.col(price_map))
                    .getField("usd_price"))
            .otherwise(lit("no data"))
    );

But it tells me that

org.apache.spark.sql.AnalysisException: cannot resolve 'map_values(`price_map`)['usd_price']' due to data type mismatch: argument 2 requires integral type, however, ''usd_price'' is of string type.;;

I want to know what is the correct way to do this ?

question from:https://stackoverflow.com/questions/65920888/spark-get-specific-value-of-the-column-map-if-the-key-exists

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can just get the map value directly using getItem, and use coalesce to replace null in case the key is not found.

dataset2 = dataset.withColumn(
    "usd_price",
    coalesce(
        dataset.col("price_map").getItem("usd_price"),
        lit("no data")
    )
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...