来自当前日期目录的外部 table

External table from current date directory

从当前日期文件夹创建的外部 table -- Scala

您好,我的数据湖存储具有以下格式的目录 yyyy/mm/dd 并且每天都会创建当前日期的新目录,并将该日期的文件放入此处。我只需要指向当前日期目录即可创建外部 table。我怎样才能做到这一点?任何帮助表示赞赏。

我试过像这样为位置创建变量

val s = java.time.LocalDate.now.toString
val year = s.slice(0, 4)
val month = s.slice(5,7)
val date = s.slice(8,10)
val location = "/mnt/mountdatalake/test/" + year + "/" + month + "/" + date + "/" + "*.txt"

并且当我尝试在外部 table 中传递此位置变量时,它不会提取数据任何有关将变量传递到外部 table 中的位置参数的帮助也将有所帮助

我先尝试这样做

sqlContext.sql(s"""CREATE table est.TEST_DATA(
ASSET string,
AREA_NAME string,
CCCC string
)
USING CSV   
OPTIONS (header='true',
delimiter = '|',
nullvalue='NA',
inferschema = 'true',
timestampFormat="yyyy-MM-dd'T'HH:mm:ss")
LOCATION '$location'""")

the second way is this

sqlContext.sql(s"""CREATE table est.TEST_DATA(
    ASSET string,
    AREA_NAME string,
    CCCC string
  )
USING CSV   
OPTIONS (header='true',
delimiter = '|',
nullvalue='NA',
inferschema = 'true',
timestampFormat="yyyy-MM-dd'T'HH:mm:ss")
LOCATION 'location'""")

两者均无效

如果要在三重引号内使用变量,请将其用 ${location} 封住。

val location = "/tmp/path/to/dir"

val query = s"""
CREATE table est.TEST_DATA(
    ASSET string,
    AREA_NAME string,
    CCCC string
)
USING CSV   
OPTIONS (header='true',
delimiter = '|',
nullvalue='NA',
inferschema = 'true',
timestampFormat="yyyy-MM-dd'T'HH:mm:ss")
LOCATION ${location}"""

print(query)

结果如下

CREATE table est.TEST_DATA(
    ASSET string,
    AREA_NAME string,
    CCCC string
)
USING CSV
OPTIONS (header='true',
delimiter = '|',
nullvalue='NA',
inferschema = 'true',
timestampFormat="yyyy-MM-dd'T'HH:mm:ss")
LOCATION /tmp/path/to/dir