プロジェクトに追加され、適切な権限が付与されたユーザーは、MaxCompute サービスを利用できます。 MaxCompute の操作 (入力および出力) はテーブルに対して実行されるため、データ処理の前にテーブルとパーティションを作成する必要があります。

次の方法でテーブルの作成と削除を行えます。

DTplus コンソールからコマンドを使用してテーブルを作成、表示、削除する方法について、以下に説明します。 コンソールのインストールに関する詳細は、「コンソール (console)」をご参照ください。

テーブルの作成

コマンドの形式は次のとおりです。
CREATE TABLE [IF NOT EXISTS] table_name 
[(col_name data_type [COMMENT col_comment], ...)] 
[COMMENT table_comment] 
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)] 
[LIFECYCLE days] 
[As select_statement]
CREATE TABLE [IF NOT EXISTS] table_name 
LIKE existing_table_name
コマンドの説明:
  • テーブル名と列名は、大文字と小文字が区別されません。
  • テーブルの作成時に同じ名前のテーブルが存在する場合、IF NOT EXISTS オプションを指定しないと、 エラーが発生します。 このオプションを指定すれば、同じ名前のテーブルが存在する場合や、ソーステーブルとターゲットテーブルの構造が一致しない場合にも、コマンドは正常に終了します。 既存テーブルのメタ情報は変更されません。
  • データ型は、BIGINT、DOUBLE、BOOLEAN、DATETIME、および STRING にのみ対応しています。
  • テーブル名と列名は、同じ命名規則に従います。最長 128 バイトで、 英数字とアンダースコア '_' を使用できます。
  • パーティション を指定するには、PARTITIONED BY オプションを使用します。 STRING 型にのみ対応しています。 最長 128 バイトで、英数字と特殊文字のスペース ' '、コロン ':'、アンダースコア '_'、ドル記号 '$'、ハッシュ記号 '#'、ドット '.'、感嘆符 '!'、 およびアットマーク '@' を使用できます。 ‘\t’、‘\n’、‘/’ などの文字は、未定義の文字と認識されます 。 パーティションテーブルでパーティションフィールドを使用している場合、パーティションを追加する際や、パーティション内のデータを更新してから読み取る際に、フルテーブルスキャンは不要です。
  • コメントは 1024 バイト以内の有効な文字列にしてください。
  • LIFECYCLE は、テーブルのライフサイクルを示します。 単位は日数です。 CREATE TABLE Like という文では、ソーステーブルのライフサイクル属性はコピーされません。
  • パーティション階層は、6 レベル以下にする必要があります。 テーブルの最大パーティション数は、プロジェクト内で設定できます。 テーブルの最大数は 60,000 です。
次の例では、テーブルの作成方法を示します。
create table test1 (key string); -- create a no-partition table. table name is test 1,  field name is key, data type is string. 
create table test2 (key bigint) partitioned by (pt string, ds string);  --Create a partition table. 
create table test3 (key boolean) partitioned by (pt string, ds string) lifecycle 100;  -- Create a table with lifecycle. 
create table test4 like test3;  -- Except for the lifecycle property, other properties of test3 (field type, partition type) are completely consistent with test4. 
create table test5 as select * from test2;  -- This operation will create test5, but the partition and lifecycle information will not be copied to the object table. 
-- This operation will copy the data of test2 to the table test5.If test2 has data, the test2 in this example is an empty table. Subsequent chapters will introduce data import.

上記例では、インスタンスを使用してテーブルが作成されます。

以下の情報を含む user という名前のテーブルを作成するとします。
  • user_id: BIGINT 型、ユーザーを識別するためのユーザー ID。
  • gender: BIGINT 型、性別 (0: 不明、1: 男性、2: 女性)。
  • age: BIGINT 型、ユーザーの年齢。

リージョンと dt をキーにしてパーティションを作成し、ライフサイクルは 365 日に設定する必要があるとします。

テーブル作成の例は、次のとおりです。
CREATE TABLE user 
( user_id BIGINT, gender BIGINT COMMENT '0 unknow,1 male, 2 Female', age BIGINT) 
PARTITIONED BY (region string, dt string) LIFECYCLE 365;

パーティションの追加

複数のパーティションにデータをインポートする場合は、パーティションテーブルを作成した後、パーティションを作成する必要があります。 文の記述形式は、次のとおりです。
alter table table_name add [if not exists] partition partition_spec partition_spec: 
: (partition_col1 = partition_col_value1, partition_col2 = partiton_col_value2, ...)
前述した例の user テーブルにパーティション (リージョン: hangzhou (杭州)、dt: 20150923) を追加する必要があるとします。 次の文を記述します。
Alter table user add if not exists partition(region='hangzhou',dt='20150923');

テーブル情報の表示

テーブル情報を表示するには、desc <table_name>; コマンドを使用します。

たとえば、test3 から情報を取得する場合は、desc test3; と記述します。

結果が次のように表示されます。
odps@ $odps_project>desc test3;
+------------------------------------------------------------------------------------+
| Owner: ALIYUN$maojing.mj@alibaba-inc.com | Project: $odps_project
| TableComment: |
+------------------------------------------------------------------------------------+
| CreateTime: 2015-09-18 12:26:57 |
| LastDDLTime: 2015-09-18 12:26:57 |
| LastModifiedTime: 2015-09-18 12:26:57 |
| Lifecycle: 100 |
+------------------------------------------------------------------------------------+
| InternalTable: YES | Size: 0 |
+------------------------------------------------------------------------------------+
| Native Columns: |
+------------------------------------------------------------------------------------+
| Field | Type | Label | Comment |
+------------------------------------------------------------------------------------+
| key | boolean | | |
+------------------------------------------------------------------------------------+
| Partition Columns: |
+------------------------------------------------------------------------------------+
| pt | string | |
| ds | string | |
+------------------------------------------------------------------------------------+
test4 から情報を取得するには、以下のように記述します。desc test4;
odps@ $odps_project>desc test4;
+------------------------------------------------------------------------------------+
| Owner: ALIYUN$maojing.mj@alibaba-inc.com | Project: $odps_project
| TableComment: |
+------------------------------------------------------------------------------------+
| CreateTime: 2015-09-18 12:27:09 |
| LastDDLTime: 2015-09-18 12:27:09 |
| LastModifiedTime: 2015-09-18 12:27:09 |
+------------------------------------------------------------------------------------+
| InternalTable: YES | Size: 0 |
+------------------------------------------------------------------------------------+
| Native Columns: |
+------------------------------------------------------------------------------------+
| Field | Type | Label | Comment |
+------------------------------------------------------------------------------------+
| key | boolean | | |
+------------------------------------------------------------------------------------+
| Partition Columns: |
+------------------------------------------------------------------------------------+
| pt | string | |
| ds | string | |
+------------------------------------------------------------------------------------+

ライフサイクル属性を除いた test3 の他の属性 (フィールド タイプ、パーテイションタイプ) は、test4 と完全に同じです。 テーブルの照会に関する詳細は、「テーブルの照会 (Describe Table)」をご参照ください。

test5 の情報を表示すると、"pt" フィールドと "ds" フィールドが、テーブルパーティションとしてではなく、2 つの共有列としてのみ存在します。

パーティションの削除

パーティションの削除方法の例は、次のとおりです。
alter table table_name drop [if exists] partition_spec; partition_spec: 
: (partition_col1 = partition_col_value1, partition_col2 = partiton_col_value2, ...)
たとえば、リージョン hangzhou と dt 20150923 のパーティションを削除する文は、次のようになります。
Alter table user drop if exists partition(region='hangzhou',dt='20150923');

テーブルの削除

テーブルの削除方法の例は、次のとおりです。
DROP TABLE [IF EXISTS] table_name;
たとえば、テーブル test2 を削除する文は、次のようになります。
drop table test2;

詳細は、「テーブル操作」の「テーブル削除 (Drop Table)」をご参照ください。