스크립트: Parquet 데이터 로드 및 언로드하기¶
이 자습서의 주석이 포함된 스크립트는 Parquet 데이터 워크플로를 설명합니다.
스크립트 1. 샘플 Parquet 데이터를 스테이징된 데이터 파일에서 직접 관계형 테이블의 별도 열에 로드합니다. 그러므로 스테이징 테이블이 필요하지 않습니다. 이 예에서 COPY INTO <테이블> 은 별도의 데이터를 VARCHAR 열 및 VARIANT 열 페어로 로드합니다.
스크립트에 PUT 문이 포함됩니다. SnowSQL 또는 PUT 문을 지원하는 다른 클라이언트에서 스크립트를 실행하는 것이 좋습니다. Snowsight 및 classic web interface 와 같은 클라이언트는 PUT 명령을 지원하지 않습니다.
스크립트 2. 관계형 Snowflake 테이블 데이터를 Parquet 파일의 별도 열로 언로드합니다. Parquet 파일의 타입이 지정되면 COPY INTO <위치> 은 기본적으로 단일 열로 언로드합니다.
이 항목의 내용:
전제 조건¶
작동 중인 활성 가상 웨어하우스.
샘플 Parquet 데이터 파일(
cities.parquet
). 링크를 클릭해도 파일이 다운로드되지 않으면, 링크를 마우스 오른쪽 버튼으로 클릭하고 링크/파일을 로컬 파일 시스템에 저장합니다.그리고 파일을 임시 폴더/디렉터리에 복사합니다.
macOS 또는 Linux:
//tmp
Windows: 탐색기 창을 열고 주소 표시줄에
%TEMP%
를 입력합니다.
샘플 데이터의 예¶
샘플 Parquet 파일의 대표적인 행은 다음과 같습니다.
{
"continent": "Europe",
"country": {
"city": [
"Paris",
"Nice",
"Marseilles",
"Cannes"
],
"name": "France"
}
}
SQL 스크립트 1: Parquet 데이터 로드¶
참고
이 스크립트의 PUT 문에서는 macOS 또는 Linux 환경을 사용하고 있는 것으로 가정합니다. Windows를 사용 중인 경우에는 설명에서와 같이 문을 수정하십시오.
/* Create a target relational table for the Parquet data. The table is temporary, meaning it persists only */
/* for the duration of the user session and is not visible to other users. */
create or replace temporary table cities (
continent varchar default NULL,
country varchar default NULL,
city variant default NULL
);
/* Create a file format object that specifies the Parquet file format type. */
/* Accepts the default values for other options. */
create or replace file format sf_tut_parquet_format
type = 'parquet';
/* Create a temporary internal stage that references the file format object. */
/* Similar to temporary tables, temporary stages are automatically dropped at the end of the session. */
create or replace temporary stage sf_tut_stage
file_format = sf_tut_parquet_format;
/* Stage the data file. */
/* */
/* Note that the example PUT statement references the macOS or Linux location of the data file. */
/* If you are using Windows, execute the following statement instead: */
-- put file://%TEMP%/cities.parquet @sf_tut_stage;
put file:///tmp/cities.parquet @sf_tut_stage;
/* Load the Parquet data into the relational table. */
/* */
/* A SELECT query in the COPY statement identifies a numbered set of columns in the data files you are */
/* loading from. Note that all Parquet data is stored in a single column ($1). */
/* */
/* Cast element values to the target column data type. */
copy into cities
from (select
$1:continent::varchar,
$1:country:name::varchar,
$1:country:city::variant
from @sf_tut_stage/cities.parquet);
/* Query the relational table */
SELECT * from cities;
SQL 스크립트 2: Parquet 데이터 언로드¶
참고
기본적으로 Snowflake는 모든 값을 허용하는 가장 작은 전체 자릿수를 설정하여 언로드된 Parquet 데이터 파일의 테이블 열을 최적화합니다. 《논리적》 열 데이터 타입(즉, 언로드 SQL 쿼리 또는 소스 테이블의 타입)에 의해 결정되는 일정한 출력 파일 스키마를 원하는 경우에는 ENABLE_UNLOAD_PHYSICAL_TYPE_OPTIMIZATION 세션 매개 변수를 FALSE로 설정하십시오.
/* Unload the CITIES table columns into a Parquet file. Optionally flatten the CITY column array and unload */
/* the child elements to a separate column. */
/* */
/* To retain the column names in the output file, use the HEADER = TRUE copy option. */
copy into @sf_tut_stage/out/parquet_ from (
select
continent
, country
, c.value::string as city
from
cities
, lateral flatten(input => city) c)
file_format = (type = 'parquet')
header = true;
/* Query the staged Parquet file. */
select t.$1 from @sf_tut_stage/out/ t;