Почему я получил два разных значения при вызове df.count () и df.rdd.count () с помощью этого сценария sql - PullRequest
0 голосов
/ 20 сентября 2018

Используйте hiveContext.sql, чтобы выполнить скрипт ниже:

with nt as (
    select label, score from (
        select * from (select label, score, row_number() over (order by score desc) as position from t1)t_1  
        join 
        (select count(*) as countall from t1)t_2 
    )ta 
    where position <= countall * 0.4
) 

, и я получил этот результат.

val hiveContext = new org.apache.spark.sql.hive.HiveContext(sc)

val df = hiveContext.read.parquet("hdfs://m7-common-cdh02:8020/user/guyc/data/demoTable/data")

df.show()
+-----------+-----------+-----+--------------------+
|act_idn_sky|sample_date|label|               score|
+-----------+-----------+-----+--------------------+
|       1003| 2017-12-21|    0|  0.4891524644941294|
|       1000| 2017-12-21|    1|  0.7903595514185819|
|       1001| 2017-12-21|    1| 0.03421212604433099|
|       1004| 2017-12-21|    0|0.033869532272332914|
|       1005| 2017-12-21|    0|  0.8114563148864252|
|       1002| 2017-12-21|    1|  0.9779307907366478|
+-----------+-----------+-----+--------------------+

df.registerTempTable("t1")

val out = hiveContext.sql("with nt as (select label, score from (select * from (select label, score, row_number() over (order by score desc) as position from t1)t_1  join (select count(*) as countall from t1)t_2 )ta where position <= countall * 0.4) select count(*) as c_positive from nt where label = 1")
out: org.apache.spark.sql.DataFrame = [c_positive: bigint]

out.show()
+----------+
|c_positive|
+----------+
|         1|
|         0|
|         0|
|         0|
|         0|
+----------+

out.count()
res3: Long = 1

out.rdd.take(10)
res4: Array[org.apache.spark.sql.Row] = Array([1], [0], [0], [0], [0])

out.rdd.count()
res5: Long = 5

снимок экрана этого выполнения sql

это странно, когда вызывать функцию 'count ()' для rdd и dataframe,

, как говорит рис: другой вывод здесь ...

может кто-нибудь мне помочь?большое спасибо !!!!

...