Spark SQL为字符串列的最大值返回null

时间:2018-12-05 22:17:34

标签: apache-spark hive apache-spark-sql partitioning

我的Hive表如下进行多分区

hive -e "show partitions mydb.mytab;"

date_part=2018-10-11/att_name=asd
date_part=2018-10-12/att_name=qwe
date_part=2018-10-13/att_name=zxc
date_part=2018-10-14/att_name=asd
date_part=2018-10-15/att_name=qwe
date_part=2018-10-16/att_name=asd

请注意,date_partatt_name都存储为 string s

现在,我想知道att_name=zxc的最新分区是什么,并获取date_part的值。考虑到以上数据,我正在寻找2018-10-13以及其他日期,我没有对应于att_name=zxc的分区。因此,当我执行以下配置单元查询时,

hive -e "select max(date_part) from mydb.mytab where att_name = 'zxc'"

我明白了

2018-10-13

这是完美的。

接下来,我尝试使用spark sql做同样的事情

import org.apache.spark.sql.SparkSession

lazy val sparkSess = SparkSession.builder
    .appName("myApp")
    .enableHiveSupport()
    .getOrCreate()
sparkSess.sql("select max(date_part) from mydb.mytab where att_name = 'zxc'").show()

但是,我明白了

+--------------+
|max(date_part)|
+--------------+
|          null|
+--------------+

我认为是因为date_part是一个字符串列。但是为什么蜂巢查询没有问题?

我在做什么错?

这是从多分区表中有条件获取最新分区的正确方法吗?

表说明

Logging initialized using configuration in file:/etc/hive/conf.bfd-puppet/hive-log4j.properties
OK
# col_name              data_type               comment

id                      string
value                   string

# Partition Information
# col_name              data_type               comment

date_part               string
att_name                string

# Detailed Table Information
Database:               mydb
Owner:                  mytab
CreateTime:             some timestamp
LastAccessTime:         UNKNOWN
Protect Mode:           None
Retention:              0
Location:               maprfs:/path/to/hive/mydb.db/mytab
Table Type:             EXTERNAL_TABLE
Table Parameters:
    EXTERNAL                TRUE
    transient_lastDdlTime   136786171357

# Storage Information
SerDe Library:          org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
InputFormat:            org.apache.hadoop.mapred.SequenceFileInputFormat
OutputFormat:           org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat
Compressed:             No
Num Buckets:            -1
Bucket Columns:         []
Sort Columns:           []
Storage Desc Params:
    serialization.format    1
Time taken: 0.201 seconds, Fetched: 34 row(s)

打印架构

所以,对于我要执行的查询,

scala> val bsv1 = sess.sql(s"SELECT max(date_part) FROM mydb.mytab WHERE att_name='zxc'")
bsv1: org.apache.spark.sql.DataFrame = [max(date_part): string]

scala> bsv1.printSchema()
root
 |-- max(date_part): string (nullable = true)


scala>

但是,当我尝试其他查询

scala> val bsv1 = sess.sql(s"SELECT * FROM mydb.mytab WHERE date_part='2018-12-01' and att_name='zxc'")
18/12/12 10:02:28 WARN SparkSession$Builder: Using an existing SparkSession; some configuration may not take effect.
bsv1: org.apache.spark.sql.DataFrame = [id: string, value: string ... 2 more fields]

scala> bsv1.printSchema
   def printSchema(): Unit

scala> bsv1.printSchema()
root
 |-- id: string (nullable = true)
 |-- value: string (nullable = true)
 |-- date_part: string (nullable = true)
 |-- att_name: string (nullable = true)

0 个答案:

没有答案