データレイクハウスを構築するときは、外部プロジェクトを作成してHadoopクラスターに関連付けるか、外部プロジェクトをdata Lake Formation (DLF) およびObject Storage Service (OSS) に関連付ける必要があります。 このトピックでは、SQL文を使用して、さまざまなシナリオで外部プロジェクトと外部プロジェクトのテーブルを管理する方法について説明します。 たとえば、テーブルデータを照会または更新できます。
背景情報
DataWorksコンソールのLake and Warehouse Integration (Data Lakehouse) ページで外部プロジェクトを作成した後、次のシナリオでSQL文を使用して外部プロジェクトを管理できます。
DLFとOSSに基づいて外部プロジェクトを管理する
外部プロジェクトを作成したら、MaxComputeクライアントを使用して外部プロジェクトに接続し、外部プロジェクトがマップされているMaxComputeプロジェクトに移動する必要があります。 これにより、MaxComputeプロジェクトを使用して外部プロジェクトのデータまたはテーブルを管理できます。
外部プロジェクトでテーブルを参照する場合は、SQL文でexternal_project_name.table_name形式でテーブルを指定する必要があります。 外部プロジェクトにテーブルを作成することはできません。
テーブルにデータを挿入します。
パーティション分割されていないテーブルにデータを挿入します。
-- Go to the MaxCompute project to which the external project is mapped rather than the external project. use doc_test_prod; -- Insert data into a non-partitioned table. insert into table ext_mcdlf_ddl.ddl_test values(1,"ddl_test_1");パーティションテーブルにデータを挿入します。
-- Go to the MaxCompute project to which the external project is mapped. use doc_test_prod; -- Insert data into a partitioned table. insert overwrite table ext_mcdlf_ddl.range_ptstring_ptint partition (pt1 = 'ds1', pt2=2) values (4, 'val4'), (5, 'val5');
外部プロジェクトのテーブルに関する情報を表示します。
-- Go to the MaxCompute project to which the external project is mapped. use doc_test_prod; -- View the tables in the external project. show tables in doc_test_prod; -- View the schema of the ext_mcdlf_ddl.ddl_test table. desc extended ext_mcdlf_ddl.ddl_test;外部プロジェクトのテーブルデータを照会します。
-- Go to the MaxCompute project to which the external project is mapped. use doc_test_prod; -- Query the data in the non-partitioned table named ext_mcdlf_ddl.ddl_test. select * from ext_mcdlf_ddl.ddl_test limit 10; -- Query the data in the partitioned table named ext_mcdlf_ddl.range_ptstring_ptint. select * from ext_mcdlf_ddl.range_ptstring_ptint where pt1='ds1';
特殊なシナリオでは、SQL文を実行するときにパラメーターを追加する必要があります。 設定例:
-- If the Optimized Row Columnar (ORC) version is later than ORC-135, add the following configuration: set odps.ext.oss.orc.native=true; -- Enable the Hive-compatible data type edition. MaxCompute supports Hive syntax only after you enable the Hive-compatible data type edition. set odps.sql.hive.compatible=true; -- Write data to OSS by using the multipart upload feature. set odps.sql.unstructured.oss.commit.mode=true; -- Run the following command to enable the multipart upload feature for the entire project. setproject odps.sql.unstructured.oss.commit.mode=true;一般的なMaxCompute SQL構文の詳細については、「テーブル操作」および「テーブルまたは静的パーティションへのデータの挿入または更新 (Insert intoおよびINSERT OVERWRITE) 」をご参照ください。
SETコマンドをodps_config.iniファイルに追加できます。 この方法では、外部プロジェクトを管理するたびにコマンドを入力する必要はありません。
マルチパートアップロード機能の詳細については、「マルチパートアップロード」をご参照ください。
外部Hadoopデータソースに基づいて外部プロジェクトを管理する
次のステートメントを実行して、外部HadoopデータソースからHiveテーブルのデータを照会します。
-- Go to the MaxCompute project to which the external project is mapped. You cannot run jobs in an external project.
use <main_project_name>;
-- Set odps.sql.hive.compatible to true. If you want to access Alibaba Cloud E-MapReduce (EMR) tables, you must configure this property. If you want to access MaxCompute tables, you do not need to configure this property.
set odps.sql.hive.compatible=true;
-- Optional. Specify the username that is used to access the Hadoop data source.
set odps.external.hadoop.user=<hadoop_name>;
-- Read data from a Hive table.
select * from <my_hive_table> limit 10;外部プロジェクトがマップされているMaxComputeプロジェクトを使用して、外部プロジェクトのテーブルまたはデータに対してDDLステートメントを実行することはできません。 テーブルにパーティションを追加し、テーブルからパーティションを削除するために使用されるDDLステートメントは例外です。 SQL DDLステートメントの詳細については、「MaxCompute SQLの概要」をご参照ください。
SETコマンドset.odps.sql.hive.com patible=trueおよびset.odps.external.hadoop.us er=<hadoop_name> をodps_config.iniファイルに追加できます。 この方法では、外部プロジェクトを管理するたびにコマンドを入力する必要はありません。
外部データソースのテーブルと同じスキーマを持つテーブルを作成する
外部データソースのテーブルと同じスキーマを持つテーブルを作成する場合は、create table...likeコマンドを実行して、外部データソースのテーブルのスキーマをレプリケートできます。 サンプル文:
create table...likeコマンドの詳細については、「Table operations」をご参照ください。
-- Create a table.
create table from_exe_tbl like external_project.testtbl;
-- The from_exe_tbl table that you created has the same schema as the external_project.testtbl table.
The two tables have the same attributes, such as column names, column comments, and table comments, aside from the lifecycle. However, the data in the external_project.testtbl table is not replicated to the from_exe_tbl table.
-- Query the table data.
SELECT * from from_exe_tbl;
-- View the table schema.
desc from_exe_tbl;関連ドキュメント
外部プロジェクトまたは外部プロジェクト内のテーブルに対する権限を他のユーザーに付与する方法の詳細については、「他のユーザーに外部プロジェクトに対する権限を付与する」をご参照ください。