LoginSignup
5
1

More than 5 years have passed since last update.

Athenaでパーティション分割しようとしたら怒られてできなかった

Posted at

エラーメッセージは下記

FAILED: SemanticException table is not partitioned but partition spec exists

上記のエラーに対しての対応

私の場合はパーティション情報を定義したテーブルを再作成しました。

どういう状況でパーティション分割を実行しようとしたのか

  • パーティションを分割したい対象テーブルのDDLは少なくとも1年前に記述していた。
  • データはEMRで整形した後にS3に s3://hogehoge/fugafuga/2018/12/ といった年、月でディレクトリをS3に切っていた。
    • この「年、月」でパーティションを区切ろうと思ったのが今回。

実際に怒られたパーティション分割DDL

ALTER TABLE '対象テーブル' ADD PARTITION (year='2018', month='12') location 's3://hogehoge/fugafuga/2018/12/'

上記を実行した結果、冒頭のエラーが発生した。

FAILED: SemanticException table is not partitioned but partition spec exists

上記のDDLにミスがあると思い、いろいろ試したがエラーは解決せず。
そもそも、このテーブルのCREATEはどのように行っていたのか(下記)。

CREATE EXTERNAL TABLE `テーブル名`(
 カラムの定義たくさん
 )
ROW FORMAT SERDE 
  'org.openx.data.jsonserde.JsonSerDe' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  's3://hogehoge/fugafuga'
TBLPROPERTIES (
  'has_encrypted_data'='false', 
  'transient_lastDdlTime'='1519292270')

エラーへの対応

テーブルの再作成。PARTITIONED BY句を追加。

CREATE EXTERNAL TABLE `テーブル名その2`(
 カラムの定義たくさん
 )
PARTITIONED BY ( 
  `year` string, 
  `month` string
 )
ROW FORMAT SERDE 
  'org.openx.data.jsonserde.JsonSerDe' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  's3://hogehoge/fugafuga'
TBLPROPERTIES (
  'has_encrypted_data'='false', 
  'transient_lastDdlTime'='1519292270')

これでパーティションが考慮されたテーブルが作成される。
この段階で show partitions テーブル名その2; と実行してもクエリは成功するが結果は空。

2018/12のパーティションを区切ってみる

ALTER TABLE テーブル名その2 ADD PARTITION (year='2018',month='12') location 's3://hogehoge/fugafuga/2018/12/'

再度 show partitions テーブル名その2; を実行すると1件パーティションが作成された。

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1