Skip to main content
Skip to main content

Npy

InputOutputAlias

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

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

Saving an array in .npy format using 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

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

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:

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

Format Settings