искра Scala для л oop внутри снова для цикла - PullRequest
0 голосов
/ 23 апреля 2020

У меня есть счет, который представляет собой список, как показано ниже

   My data in count as below
   (Focus,37)
   (Test,26)

, и мой код такой, как показано ниже.

         for (i <- count ) {
              for(x <- i {
                 if(x == "Focus"){
                     Focus_cnt=i(x) }
                        else if(x == "Test"){
                           Test_cnt=i(x) }
                           else {
            pass
        }
    }
}

Ошибка, с которой я сталкиваюсь, находится в строке - для (x <- i и ошибка i: (Любой, Любой) </p>

Любой лучший способ получить счет в искре Scala.

1 Ответ

0 голосов
/ 23 апреля 2020

Можете ли вы проверить это? дайте мне знать, если я неправильно понял. В этом я применяю фильтр и беру отсчет.

scala> Seq(("fa","fb","fc",5,"fe","ff","Focus"),("fba","fbb","fbc",16,"bd","be","Focus"),("fba","fbb","fbc",54,"bd","be","Focus"),("fca","fcb","fcc",135,"fcd","fef","Focus"),("a","b","c",5,"e","f","Test"),("aa","ba","ca",56,"ea","fa","Test"),("ab","cb","cc",35,"de","df","Test")).toDF("a","b","c","d","e","f","status")
res29: org.apache.spark.sql.DataFrame = [a: string, b: string ... 5 more fields]

scala> val df = Seq(("fa","fb","fc",5,"fe","ff","Focus"),("fba","fbb","fbc",16,"bd","be","Focus"),("fba","fbb","fbc",54,"bd","be","Focus"),("fca","fcb","fcc",135,"fcd","fef","Focus"),("a","b","c",5,"e","f","Test"),("aa","ba","ca",56,"ea","fa","Test"),("ab","cb","cc",35,"de","df","Test")).toDF("a","b","c","d","e","f","status")
df: org.apache.spark.sql.DataFrame = [a: string, b: string ... 5 more fields]

scala> val newDF = df.groupBy("status").agg(count("status").as("count"))
newDF: org.apache.spark.sql.DataFrame = [status: string, count: bigint]

scala> val focus_cnt = newDF.filter($"status" === "Focus").select("count").map(_.getAs[Long](0)).head
focus_cnt: Long = 4

scala> val test_cnt  = newDF.filter($"status" === "Test").select("count").map(_.getAs[Long](0)).head
test_cnt: Long = 3

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...