2020-04-03 13:23:32 +00:00
|
|
|
|
---
|
2022-08-26 19:07:59 +00:00
|
|
|
|
slug: /zh/sql-reference/table-functions/file
|
2022-04-10 23:08:18 +00:00
|
|
|
|
sidebar_position: 37
|
|
|
|
|
sidebar_label: file
|
2020-04-03 13:23:32 +00:00
|
|
|
|
---
|
|
|
|
|
|
2021-03-16 07:29:34 +00:00
|
|
|
|
# file {#file}
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-03-16 07:29:34 +00:00
|
|
|
|
从文件创建表。 此表函数类似于 [url](../../sql-reference/table-functions/url.md) 和 [hdfs](../../sql-reference/table-functions/hdfs.md)。
|
|
|
|
|
|
|
|
|
|
`file` 函数可用于对[File](../../engines/table-engines/special/file.md) 表中的数据进行 `SELECT` 和 `INSERT` 查询。
|
|
|
|
|
|
|
|
|
|
**语法**
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
file(path, format, structure)
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-16 07:29:34 +00:00
|
|
|
|
**参数**
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-03-16 07:29:34 +00:00
|
|
|
|
- `path` — [user_files_path](../../operations/server-configuration-parameters/settings.md#server_configuration_parameters-user_files_path)中文件的相对路径。在只读模式下,文件路径支持以下通配符: `*`, `?`, `{abc,def}` 和 `{N..M}`,其中 `N`, `M` 是数字, \``'abc', 'def'` 是字符串。
|
|
|
|
|
- `format` —文件的[格式](../../interfaces/formats.md#formats)。
|
|
|
|
|
- `structure` — 表的结构。格式 `'column1_name column1_type, column2_name column2_type, ...'`。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
**返回值**
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
具有指定结构的表,用于读取或写入指定文件中的数据。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
**示例**
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-03-16 07:29:34 +00:00
|
|
|
|
设置 `user_files_path` 和文件 `test.csv` 的内容:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` bash
|
|
|
|
|
$ grep user_files_path /etc/clickhouse-server/config.xml
|
|
|
|
|
<user_files_path>/var/lib/clickhouse/user_files/</user_files_path>
|
|
|
|
|
|
|
|
|
|
$ cat /var/lib/clickhouse/user_files/test.csv
|
|
|
|
|
1,2,3
|
|
|
|
|
3,2,1
|
|
|
|
|
78,43,45
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-16 07:29:34 +00:00
|
|
|
|
从 `test.csv` 中的表中获取数据,并从表中选择前两行:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
2021-03-16 07:29:34 +00:00
|
|
|
|
SELECT * FROM file('test.csv', 'CSV', 'column1 UInt32, column2 UInt32, column3 UInt32') LIMIT 2;
|
2020-04-03 13:23:32 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
``` text
|
|
|
|
|
┌─column1─┬─column2─┬─column3─┐
|
|
|
|
|
│ 1 │ 2 │ 3 │
|
|
|
|
|
│ 3 │ 2 │ 1 │
|
|
|
|
|
└─────────┴─────────┴─────────┘
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-16 07:29:34 +00:00
|
|
|
|
从CSV文件获取包含3列 [UInt32](../../sql-reference/data-types/int-uint.md) 类型的表的前10行:
|
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
SELECT * FROM file('test.csv', 'CSV', 'column1 UInt32, column2 UInt32, column3 UInt32') LIMIT 10;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
将文件中的数据插入表中:
|
|
|
|
|
|
2020-04-03 13:23:32 +00:00
|
|
|
|
``` sql
|
2021-03-16 07:29:34 +00:00
|
|
|
|
INSERT INTO FUNCTION file('test.csv', 'CSV', 'column1 UInt32, column2 UInt32, column3 UInt32') VALUES (1, 2, 3), (3, 2, 1);
|
|
|
|
|
SELECT * FROM file('test.csv', 'CSV', 'column1 UInt32, column2 UInt32, column3 UInt32');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
``` text
|
|
|
|
|
┌─column1─┬─column2─┬─column3─┐
|
|
|
|
|
│ 1 │ 2 │ 3 │
|
|
|
|
|
│ 3 │ 2 │ 1 │
|
|
|
|
|
└─────────┴─────────┴─────────┘
|
2020-04-03 13:23:32 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-16 07:29:34 +00:00
|
|
|
|
**路径中的通配符**
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-03-16 07:29:34 +00:00
|
|
|
|
多个路径组件可以具有通配符。 对于要处理的文件必须存在并与整个路径模式匹配(不仅后缀或前缀)。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-03-16 07:29:34 +00:00
|
|
|
|
- `*` — 替换任意数量的任何字符,除了 `/` 包括空字符串。
|
|
|
|
|
- `?` — 替换任何单个字符。
|
|
|
|
|
- `{some_string,another_string,yet_another_one}` — 替换任何字符串 `'some_string', 'another_string', 'yet_another_one'`。
|
|
|
|
|
- `{N..M}` — 替换范围从N到M的任何数字(包括两个边界)。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-03-19 02:32:20 +00:00
|
|
|
|
使用 `{}` 的构造类似于 [remote](../../sql-reference/table-functions/remote.md))表函数。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
**示例**
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-03-16 07:29:34 +00:00
|
|
|
|
假设我们有几个文件,这些文件具有以下相对路径:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-10-13 17:23:29 +00:00
|
|
|
|
- ‘some_dir/some_file_1’
|
|
|
|
|
- ‘some_dir/some_file_2’
|
|
|
|
|
- ‘some_dir/some_file_3’
|
|
|
|
|
- ‘another_dir/some_file_1’
|
|
|
|
|
- ‘another_dir/some_file_2’
|
|
|
|
|
- ‘another_dir/some_file_3’
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-03-16 07:29:34 +00:00
|
|
|
|
查询这些文件中的行数:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
SELECT count(*)
|
|
|
|
|
FROM file('{some,another}_dir/some_file_{1..3}', 'TSV', 'name String, value UInt32')
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-16 07:29:34 +00:00
|
|
|
|
查询这两个目录的所有文件中的行数:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
SELECT count(*)
|
|
|
|
|
FROM file('{some,another}_dir/*', 'TSV', 'name String, value UInt32')
|
|
|
|
|
```
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
!!! warning "警告"
|
2021-03-19 02:32:20 +00:00
|
|
|
|
如果您的文件列表包含带前导零的数字范围,请对每个数字分别使用带有大括号的结构或使用 `?`。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
**示例**
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2024-05-23 11:54:45 +00:00
|
|
|
|
从名为 `file000`, `file001`, ... , `file999`的文件中查询数据:
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
|
|
``` sql
|
|
|
|
|
SELECT count(*)
|
|
|
|
|
FROM file('big_dir/file{0..9}{0..9}{0..9}', 'CSV', 'name String, value UInt32')
|
|
|
|
|
```
|
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
## 虚拟列 {#virtual-columns}
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-03-16 07:29:34 +00:00
|
|
|
|
- `_path` — 文件路径。
|
|
|
|
|
- `_file` — 文件名称。
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2020-04-08 14:22:25 +00:00
|
|
|
|
**另请参阅**
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
2021-09-19 20:05:54 +00:00
|
|
|
|
- [虚拟列](https://clickhouse.com/docs/en/operations/table_engines/#table_engines-virtual_columns)
|