如何获取(键,值)对中相应键的值

时间:2019-07-20 00:50:40

标签: scala apache-spark

我通过读取文件创建了一个包含(键,值)对的RDD,我想读取指定键的值并将其分配给字符串。 RDD是这样的:

scala> minValue.foreach(println)
(http://subdom0003.example.com,100B)
(http://subdom0001.example.com,333B)
(http://subdom0002.example.com,8704B)

首先,我尝试使用val a = minValue("http://subdom0003.example.com") 这没用。

我试图通过get来获取价值,但这没有用。

scala> val a = minValue.get("http://subdom0003.example.com").get
<console>:25: error: value get is not a member of org.apache.spark.rdd.RDD[(String, String)]
       val a = minValue.get("http://subdom0003.example.com").get

然后我想知道是否应该先map()然后再get()。但这仍然没有成功

scala> val a = minValue.map(_.get("http://subdom0003.example.com").get)
<console>:25: error: value get is not a member of (String, String)
       val a = minValue.map(_.get("http://subdom0003.example.com").get)

我想要的结果是,如果我得到一个键,我想将其值存储在一个变量中。例如

if key = "http://subdom0003.example.com"
then val minString = 100B

1 个答案:

答案 0 :(得分:1)

我认为您最好的办法是通过密钥过滤import React from 'react'; import {List, Issue, SortBy} from './components'; function App() { return ( <div> <SortBy onChange={() => {}}> <SortBy.Option> id </SortBy.Option> <SortBy.Option> state </SortBy.Option> {/* this one should throw a warning saying that option is not an instance of Option, but it doesn't */} <option>---</option> </SortBy> <div/> ); } export default App; ,然后使用RDD方法获得第一个结果。像这样:

first()

在您的情况下,您应该具有:

minValue.filter(_._1 == "your key").first();