Amazon Athena 从指令中获取所有文件而不是一个文件

Amazon Athena takes all files from the direction instead of one file

我正在编写查询以在 Amazon Athena 上创建一个 table,它从 S3 目录中的文件中获取数据。但是,我想从一个文件而不是目录中的所有文件创建一个 table。

如果我输入 LOCATION 's3://influx-cold-storage/2018/file1.csv'

,我会收到位置错误

LOCATION keyword docs 是这样描述它的用法的:

Athena reads all files in an Amazon S3 location you specify in the CREATE TABLE statement, and cannot ignore any files included in the prefix. When you create tables, include in the Amazon S3 path only the files you want Athena to read. Use AWS Lambda functions to scan files in the source location, remove any empty files, and move unneeded files to another location.

并指定不能使用文件的确切路径和通配符(例如 *)..

您必须将您需要的任何确切文件放在它自己的目录中。

我写了一个快速的 bash 脚本,它将当前目录中的所有文件移动到同名目录中,没有扩展名的 hacky 解决方法:

#!/bin/bash

echo "INFO - Moving all files in current directory to directory of same name."

for file in ./*; do
    [[ -d "$file" ]] && continue 
    fname=`basename $file`
    dname=${fname%.*}
    echo "INFO - Creating directory $dname"
    mkdir "$dname"
    [[ $? -eq 0 ]] && echo "INFO - $dname created" || echo "WARN - $fname directory already exists."
    echo "INFO - Moving $fname to ${dname}/${fname}"
    mv "$file" "${dname}/${fname}"
    [[ $? -eq 0 ]] && echo "INFO - $fname moved successfully." || echo "WARN - Possibly failed. Checkfor $fname in $dname directory."
done