从spark.sql.Row获取第一个值



我有以下json格式:

{"Request": {"TrancheList": {"Tranche": [{"TrancheId": "500192163","OwnedAmt": "26500000",    "Curr": "USD" }, {  "TrancheId": "500213369", "OwnedAmt": "41000000","Curr": "USD"}]},"FxRatesList": {"FxRatesContract": [{"Currency": "CHF","FxRate": "0.97919983706115"},{"Currency": "AUD", "FxRate": "1.2966804979253"},{ "Currency": "USD","FxRate": "1"},{"Currency": "SEK","FxRate": "8.1561012531034"},{"Currency": "NOK", "FxRate": "8.2454981641398"},{"Currency": "JPY","FxRate": "111.79999785344"},{"Currency": "HKD","FxRate": "7.7568025218916"},{"Currency": "GBP","FxRate": "0.69425159677867"}, {"Currency": "EUR","FxRate": "0.88991723769689"},{"Currency": "DKK", "FxRate": "6.629598372301"}]},"isExcludeDeals": "true","baseCurrency": "USD"}}

从hdfs读取json:

val hdfsRequest = spark.read.json("hdfs://localhost/user/request.json")
val baseCurrency = hdfsRequest.select("Request.baseCurrency").map(_.getString(0)).collect.headOption
var fxRates = hdfsRequest.select("Request.FxRatesList.FxRatesContract")
val fxRatesDF = fxRates.select(explode(fxRates("FxRatesContract"))).toDF("FxRatesContract").select("FxRatesContract.Currency", "FxRatesContract.FxRate").filter($"Currency"===baseCurrency.get)
fxRatesDF.show()

我得到的fxRatesDF的输出是:

fxRatesDF: org.apache.spark.sql.Dataset[org.apache.spark.sql.Row] = [Currency: string, FxRate: string]
+--------+------+
|Currency|FxRate|
+--------+------+
|     USD|     1|

我怎么能抓住第一行的值的Fxrate列?

可以使用

fxRatesDF.select(col("FxRate")).first().FxRate

这是您需要使用的函数

像这样使用:

fxRatesDF.first().FxRate

也许这样:

fxRatesDF.take(1)[0][1]

fxRatesDF.collect()[0][1]

fxRatesDF.first()[1]

我知道这是一个旧的帖子,但我得到了它的工作方式fxRatesDF.first()[0]

一种简单的方法是使用索引选择行和列。输入Dataframe:

+-----+
|count|
+-----+
|    0|
+-----+
代码:

count = df.collect()[0][0]
print(count)
if count == 0:
    print("First row and First column value is 0")
输出:

0
First row and First column value is 0

更新其中一个答案。

from pyspark.sql.functions import col
fxRatesDF.select(col("FxRate")).first()[0]

应该像这样简单:

display(fxRatesDF.select($"FxRate").limit(1))

只需要一行字就可以解决这个问题。

fxRates.first()(1)

一行两个字

fxRates.first().getString(1)

你可以试试这个方法:

fxRatesDF.select("FxRate").rdd.map{case Row(i:Int)=> i}.first()

相关内容

  • 没有找到相关文章