2020-04-03 13:23:32 +00:00
---
2020-07-06 17:18:37 +00:00
toc_priority: 4
2020-04-03 13:23:32 +00:00
toc_title: HDFS
---
2020-03-22 09:14:59 +00:00
# HDFS {#table_engines-hdfs}
2019-09-03 14:23:51 +00:00
2019-12-18 09:46:42 +00:00
This engine provides integration with [Apache Hadoop ](https://en.wikipedia.org/wiki/Apache_Hadoop ) ecosystem by allowing to manage data on [HDFS ](https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html )via ClickHouse. This engine is similar
2020-06-18 08:24:31 +00:00
to the [File ](../../../engines/table-engines/special/file.md#table_engines-file ) and [URL ](../../../engines/table-engines/special/url.md#table_engines-url ) engines, but provides Hadoop-specific features.
2019-09-03 14:23:51 +00:00
2020-03-20 10:10:48 +00:00
## Usage {#usage}
2019-09-03 14:23:51 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-09-03 14:23:51 +00:00
ENGINE = HDFS(URI, format)
```
2020-03-20 10:10:48 +00:00
2019-09-04 19:55:56 +00:00
The `URI` parameter is the whole file URI in HDFS.
2019-09-03 14:23:51 +00:00
The `format` parameter specifies one of the available file formats. To perform
`SELECT` queries, the format must be supported for input, and to perform
2020-03-20 10:10:48 +00:00
`INSERT` queries – for output. The available formats are listed in the
2020-04-03 13:23:32 +00:00
[Formats ](../../../interfaces/formats.md#formats ) section.
2019-09-20 11:26:00 +00:00
The path part of `URI` may contain globs. In this case the table would be readonly.
2019-09-03 14:23:51 +00:00
**Example:**
2019-09-04 13:26:52 +00:00
**1.** Set up the `hdfs_engine_table` table:
2019-09-03 14:23:51 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-09-03 14:23:51 +00:00
CREATE TABLE hdfs_engine_table (name String, value UInt32) ENGINE=HDFS('hdfs://hdfs1:9000/other_storage', 'TSV')
```
2019-09-04 19:55:56 +00:00
**2.** Fill file:
2020-03-20 10:10:48 +00:00
``` sql
2019-09-04 19:55:56 +00:00
INSERT INTO hdfs_engine_table VALUES ('one', 1), ('two', 2), ('three', 3)
```
**3.** Query the data:
2019-09-03 14:23:51 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-09-03 14:23:51 +00:00
SELECT * FROM hdfs_engine_table LIMIT 2
```
2020-03-20 10:10:48 +00:00
``` text
2019-09-03 14:23:51 +00:00
┌─name─┬─value─┐
│ one │ 1 │
│ two │ 2 │
└──────┴───────┘
```
2020-03-20 10:10:48 +00:00
## Implementation Details {#implementation-details}
2019-09-03 14:23:51 +00:00
2020-03-21 04:11:51 +00:00
- Reads and writes can be parallel
- Not supported:
- `ALTER` and `SELECT...SAMPLE` operations.
- Indexes.
- Replication.
2019-09-03 14:23:51 +00:00
2019-09-20 11:26:00 +00:00
**Globs in path**
Multiple path components can have globs. For being processed file should exists and matches to the whole path pattern. Listing of files determines during `SELECT` (not at `CREATE` moment).
2020-03-21 04:11:51 +00:00
- `*` — Substitutes any number of any characters except `/` including empty string.
- `?` — Substitutes any single character.
- `{some_string,another_string,yet_another_one}` — Substitutes any of strings `'some_string', 'another_string', 'yet_another_one'` .
- `{N..M}` — Substitutes any number in range from N to M including both borders.
2019-09-20 11:26:00 +00:00
2020-04-30 18:19:18 +00:00
Constructions with `{}` are similar to the [remote ](../../../sql-reference/table-functions/remote.md ) table function.
2019-09-20 11:26:00 +00:00
**Example**
2020-03-20 10:10:48 +00:00
1. Suppose we have several files in TSV format with the following URIs on HDFS:
2020-10-13 17:23:29 +00:00
- ‘ hdfs://hdfs1:9000/some_dir/some_file_1’
- ‘ hdfs://hdfs1:9000/some_dir/some_file_2’
- ‘ hdfs://hdfs1:9000/some_dir/some_file_3’
- ‘ hdfs://hdfs1:9000/another_dir/some_file_1’
- ‘ hdfs://hdfs1:9000/another_dir/some_file_2’
- ‘ hdfs://hdfs1:9000/another_dir/some_file_3’
2019-09-20 11:26:00 +00:00
2020-03-20 10:10:48 +00:00
1. There are several ways to make a table consisting of all six files:
2019-09-20 11:26:00 +00:00
2020-03-20 10:10:48 +00:00
<!-- -->
2019-09-20 11:26:00 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-09-20 11:26:00 +00:00
CREATE TABLE table_with_range (name String, value UInt32) ENGINE = HDFS('hdfs://hdfs1:9000/{some,another}_dir/some_file_{1..3}', 'TSV')
```
Another way:
2020-03-20 10:10:48 +00:00
``` sql
2019-09-20 11:26:00 +00:00
CREATE TABLE table_with_question_mark (name String, value UInt32) ENGINE = HDFS('hdfs://hdfs1:9000/{some,another}_dir/some_file_?', 'TSV')
```
Table consists of all the files in both directories (all files should satisfy format and schema described in query):
2020-03-20 10:10:48 +00:00
``` sql
2019-09-20 11:26:00 +00:00
CREATE TABLE table_with_asterisk (name String, value UInt32) ENGINE = HDFS('hdfs://hdfs1:9000/{some,another}_dir/*', 'TSV')
```
2020-03-20 10:10:48 +00:00
!!! warning "Warning"
2019-09-20 11:26:00 +00:00
If the listing of files contains number ranges with leading zeros, use the construction with braces for each digit separately or use `?` .
**Example**
2020-03-20 10:10:48 +00:00
Create table with files named `file000` , `file001` , … , `file999` :
2019-09-20 11:26:00 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2020-08-28 07:56:44 +00:00
CREATE TABLE big_table (name String, value UInt32) ENGINE = HDFS('hdfs://hdfs1:9000/big_dir/file{0..9}{0..9}{0..9}', 'CSV')
2019-09-20 11:26:00 +00:00
```
2020-03-20 10:10:48 +00:00
## Virtual Columns {#virtual-columns}
2020-01-15 07:52:45 +00:00
2020-03-21 04:11:51 +00:00
- `_path` — Path to the file.
- `_file` — Name of the file.
2020-01-15 07:52:45 +00:00
**See Also**
2020-06-18 08:24:31 +00:00
- [Virtual columns ](../../../engines/table-engines/index.md#table_engines-virtual_columns )
2020-01-15 07:52:45 +00:00
2020-01-30 10:34:55 +00:00
[Original article ](https://clickhouse.tech/docs/en/operations/table_engines/hdfs/ ) <!--hide-->