スクリプト:Parquetデータのロードとアンロード¶
このチュートリアルの注釈付きスクリプトでは、Parquetデータワークフローについて説明します。
スクリプト1。 ステージングされたデータファイルからサンプルParquetデータをリレーショナルテーブルの個別の列に直接ロードし、ステージングテーブルの必要性を回避します。この例では、 COPY INTO <テーブル> は個別のデータをVARCHAR列のペアと VARIANT 列にロードします。
スクリプトには PUT ステートメントが含まれているため、 classic web interface (または PUT をサポートしない他のインターフェイス)の Worksheets
ページでは実行できないことに注意してください。代わりに、Snowflake CLI クライアントである SnowSQLを使用してスクリプトを実行することをお勧めします。
スクリプト2。 リレーショナルSnowflakeテーブルデータをParquetファイルの個別の列にアンロードします。Parquetファイルタイプが指定されている場合、デフォルトで COPY INTO <場所> は単一の列にアンロードされます。
このトピックの内容:
前提条件¶
アクティブで実行中の仮想ウェアハウスです。
サンプルのParquetデータファイル(
cities.parquet
)。リンクをクリックしてもファイルがダウンロードされない場合は、リンクを右クリックして、リンク/ファイルをローカルファイルシステムに保存します。次に、ファイルを一時フォルダー/ディレクトリにコピーします。
macOS またはLinux:
//tmp
Windows: Explorerウィンドウを開き、アドレスバーに
%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;