Back to Clickhouse

Npy

docs/en/interfaces/formats/Npy.md

26.4.1.1-new3.0 KB
Original Source
InputOutputAlias

Description {#description}

The Npy format is designed to load a NumPy array from a .npy file into ClickHouse. The NumPy file format is a binary format used for efficiently storing arrays of numerical data. During import, ClickHouse treats the top level dimension as an array of rows with a single column.

The table below gives the supported Npy data types and their corresponding type in ClickHouse:

Data types matching {#data_types-matching}

Npy data type (INSERT)ClickHouse data typeNpy data type (SELECT)
i1Int8i1
i2Int16i2
i4Int32i4
i8Int64i8
u1, b1UInt8u1
u2UInt16u2
u4UInt32u4
u8UInt64u8
f2, f4Float32f4
f8Float64f8
S, UStringS
FixedStringS

Example usage {#example-usage}

Saving an array in .npy format using Python {#saving-an-array-in-npy-format-using-python}

Python
import numpy as np
arr = np.array([[[1],[2],[3]],[[4],[5],[6]]])
np.save('example_array.npy', arr)

Reading a NumPy file in ClickHouse {#reading-a-numpy-file-in-clickhouse}

sql
SELECT *
FROM file('example_array.npy', Npy)
response
┌─array─────────┐
│ [[1],[2],[3]] │
│ [[4],[5],[6]] │
└───────────────┘

Selecting data {#selecting-data}

You can select data from a ClickHouse table and save it into a file in the Npy format using the following command with clickhouse-client:

bash
$ clickhouse-client --query="SELECT {column} FROM {some_table} FORMAT Npy" > {filename.npy}

Format settings {#format-settings}