Verwenden von CTAS und INSERT INTO zum Umgehen des Limits von 100 Partitionen
Athena hat ein Limit von 100 Partitionen pro CREATE TABLE AS SELECT (CTAS)-Abfrage. Ebenso können Sie einer Zieltabelle mit einer INSERT INTO-Anweisung maximal 100 Partitionen hinzufügen.
Wenn Sie diese Einschränkung überschreiten, wird möglicherweise die Fehlermeldung HIVE_TOO_MANY_OPEN_PARTITIONS: Exceeded limit of 100 open writers for partitions/buckets angezeigt. Um diese Einschränkung zu umgehen, können Sie eine CTAS-Anweisung und eine Reihe von INSERT INTO-Anweisungen verwenden, die jeweils bis zu 100 Partitionen erstellen oder einfügen.
Das Beispiel in diesem Thema verwendet eine Datenbank mit dem Namen tpch100, deren Daten sich im Amazon-S3-Bucket-Speicherort „s3://amzn-s3-demo-bucket/“ befinden.
So erstellen Sie mithilfe von CTAS und INSERT INTO eine Tabelle mit mehr als 100 Partitionen
-
Erstellen Sie mit einer
CREATE EXTERNAL TABLE-Anweisung eine Tabelle, die für das gewünschte Feld partitioniert ist.Die folgende Beispielanweisung partitioniert die Daten durch die Spalte
l_shipdate. Der Tisch hat 2525 Partitionen.CREATE EXTERNAL TABLE `tpch100.lineitem_parq_partitioned`( `l_orderkey` int, `l_partkey` int, `l_suppkey` int, `l_linenumber` int, `l_quantity` double, `l_extendedprice` double, `l_discount` double, `l_tax` double, `l_returnflag` string, `l_linestatus` string, `l_commitdate` string, `l_receiptdate` string, `l_shipinstruct` string, `l_comment` string) PARTITIONED BY ( `l_shipdate` string) ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe' STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat' LOCATION 's3://amzn-s3-demo-bucket/lineitem/' -
Führen Sie einen
SHOW PARTITIONS-Befehl wie den folgenden aus, um die Partitionen aufzulisten.<table_name>SHOW PARTITIONS lineitem_parq_partitionedEs folgen teilweise Beispielergebnisse.
/* l_shipdate=1992-01-02 l_shipdate=1992-01-03 l_shipdate=1992-01-04 l_shipdate=1992-01-05 l_shipdate=1992-01-06 ... l_shipdate=1998-11-24 l_shipdate=1998-11-25 l_shipdate=1998-11-26 l_shipdate=1998-11-27 l_shipdate=1998-11-28 l_shipdate=1998-11-29 l_shipdate=1998-11-30 l_shipdate=1998-12-01 */ -
Führen Sie eine CTAS-Abfrage aus, um eine partitionierte Tabelle zu erstellen.
Im folgenden Beispiel wird eine Tabelle mit dem Namen erstellt
my_lineitem_parq_partitionedund dieWHERE-Klausel verwendet, umDATEauf früher als1992-02-01zu beschränken. Da das Beispiel-Dataset mit Januar 1992 beginnt, werden nur Partitionen für Januar 1992 erstellt.CREATE table my_lineitem_parq_partitioned WITH (partitioned_by = ARRAY['l_shipdate']) AS SELECT l_orderkey, l_partkey, l_suppkey, l_linenumber, l_quantity, l_extendedprice, l_discount, l_tax, l_returnflag, l_linestatus, l_commitdate, l_receiptdate, l_shipinstruct, l_comment, l_shipdate FROM tpch100.lineitem_parq_partitioned WHERE cast(l_shipdate as timestamp) < DATE ('1992-02-01'); -
Führen Sie den Befehl
SHOW PARTITIONSaus, um zu überprüfen, ob die Tabelle die gewünschten Partitionen enthält.SHOW PARTITIONS my_lineitem_parq_partitioned;Die Partitionen im Beispiel stammen vom Januar 1992.
/* l_shipdate=1992-01-02 l_shipdate=1992-01-03 l_shipdate=1992-01-04 l_shipdate=1992-01-05 l_shipdate=1992-01-06 l_shipdate=1992-01-07 l_shipdate=1992-01-08 l_shipdate=1992-01-09 l_shipdate=1992-01-10 l_shipdate=1992-01-11 l_shipdate=1992-01-12 l_shipdate=1992-01-13 l_shipdate=1992-01-14 l_shipdate=1992-01-15 l_shipdate=1992-01-16 l_shipdate=1992-01-17 l_shipdate=1992-01-18 l_shipdate=1992-01-19 l_shipdate=1992-01-20 l_shipdate=1992-01-21 l_shipdate=1992-01-22 l_shipdate=1992-01-23 l_shipdate=1992-01-24 l_shipdate=1992-01-25 l_shipdate=1992-01-26 l_shipdate=1992-01-27 l_shipdate=1992-01-28 l_shipdate=1992-01-29 l_shipdate=1992-01-30 l_shipdate=1992-01-31 */ -
Verwenden Sie eine
INSERT INTO-Anweisung, um Partitionen zur Tabelle hinzuzufügen.Im folgenden Beispiel werden Partitionen für die Datumsangaben des Monats Februar 1992 hinzugefügt.
INSERT INTO my_lineitem_parq_partitioned SELECT l_orderkey, l_partkey, l_suppkey, l_linenumber, l_quantity, l_extendedprice, l_discount, l_tax, l_returnflag, l_linestatus, l_commitdate, l_receiptdate, l_shipinstruct, l_comment, l_shipdate FROM tpch100.lineitem_parq_partitioned WHERE cast(l_shipdate as timestamp) >= DATE ('1992-02-01') AND cast(l_shipdate as timestamp) < DATE ('1992-03-01'); -
Führen Sie
SHOW PARTITIONSerneut aus.SHOW PARTITIONS my_lineitem_parq_partitioned;Die Beispieltabelle enthält nun Partitionen vom Januar und Februar 1992.
/* l_shipdate=1992-01-02 l_shipdate=1992-01-03 l_shipdate=1992-01-04 l_shipdate=1992-01-05 l_shipdate=1992-01-06 ... l_shipdate=1992-02-20 l_shipdate=1992-02-21 l_shipdate=1992-02-22 l_shipdate=1992-02-23 l_shipdate=1992-02-24 l_shipdate=1992-02-25 l_shipdate=1992-02-26 l_shipdate=1992-02-27 l_shipdate=1992-02-28 l_shipdate=1992-02-29 */ -
Verwenden Sie weiterhin
INSERT INTO-Anweisungen, die nicht mehr als 100 Partitionen lesen und hinzufügen. Fahren Sie fort, bis Sie die Anzahl der benötigten Partitionen erreicht haben.Wichtig
Achten Sie beim Festlegen der
WHERE-Bedingung darauf, dass sich die Abfragen nicht überschneiden. Andernfalls haben einige Partitionen möglicherweise doppelte Daten.