Verbinden von zwei Tabellen in Hive mit HiveQL (Hadoop) [duplizieren]
Mögliche Duplikate:
SQL-Abfrage JOIN mit Tabelle
CREATE EXTERNAL TABLE IF NOT EXISTS TestingTable1 (This is the MAIN table through which comparisons need to be made)
(
BUYER_ID BIGINT,
ITEM_ID BIGINT,
CREATED_TIME STRING
)
Und dies sind die Daten in der obigen ersten Tabelle
**BUYER_ID** | **ITEM_ID** | **CREATED_TIME**
--------------+------------------+-------------------------
1015826235 220003038067 *2001-11-03 19:40:21*
1015826235 300003861266 2001-11-08 18:19:59
1015826235 140002997245 2003-08-22 09:23:17
1015826235 *210002448035* 2001-11-11 22:21:11
Dies ist die zweite Tabelle in Hive. Sie enthält auch Informationen zu den Artikeln, die wir kaufen.
CREATE EXTERNAL TABLE IF NOT EXISTS TestingTable2
(
USER_ID BIGINT,
PURCHASED_ITEM ARRAY<STRUCT<PRODUCT_ID: BIGINT,TIMESTAMPS:STRING>>
)
Und dies sind die Daten in der obigen zweiten Tabelle (TestingTable2
) -
**USER_ID** **PURCHASED_ITEM**
1015826235 [{"product_id":220003038067,"timestamps":"1004941621"}, {"product_id":300003861266,"timestamps":"1005268799"}, {"product_id":140002997245,"timestamps":"1061569397"},{"product_id":200002448035,"timestamps":"1005542471"}]
Vergleichen SieTestingTable2
mitTestingTable1
damit ist das unten stehende szenario erfüllt.
Finden Sie diePRODUCT_ID
UNDTIMESTAMPS
vonTestingTable2
WAS NICHT PASSTITEM_ID
UNDCREATED_TIME
von TestingTable1 ENTSPRECHENBUYER_ID(USER_ID)
nach dem Vergleich vonTestingTable1
.
Also wenn du schaustTestingTable2
Daten diese (letzte)ITEM_ID 210002448035
vonTestingTable1
passt nicht zuTestingTable2
PRODUCT_ID- 200002448035
Daten und ähnlich mit Zeitstempeln. Daher möchte ich das folgende Ergebnis mit der HiveQL-Abfrage anzeigen.
**BUYER_ID** | **ITEM_ID** | **CREATED_TIME** | **PRODUCT_ID** | **TIMESTAMPS**
--------------+------------------+--------------------------------+------------------------+----------------------
1015826235 *210002448035* 2001-11-11 22:21:11 200002448035 1005542471
1015826235 220003038067 *2001-11-03 19:40:21* 220003038067 1004941621
Kann mir jemand dabei helfen. Da ich neu bei HiveQL bin, habe ich viele Probleme.
Aktualisierte:-
Ich habe diese Abfrage geschrieben, aber sie funktioniert nicht so, wie ich es wollte.
select * from
(select * from
(select user_id, prod_and_ts.product_id as product_id, prod_and_ts.timestamps as timestamps
from testingtable2 LATERAL VIEW
explode(purchased_item) exploded_table as prod_and_ts)
prod_and_ts
LEFT OUTER JOIN testingtable1
ON ( prod_and_ts.user_id = testingtable1.buyer_id AND testingtable1.item_id = prod_and_ts.product_id
AND prod_and_ts.timestamps = UNIX_TIMESTAMP (testingtable1.created_time)
)
where testingtable1.buyer_id IS NULL)
set_a LEFT OUTER JOIN testingtable1
ON (set_a.user_id = testingtable1.buyer_id AND
( set_a.product_id = testingtable1.item_id OR set_a.timestamps = UNIX_TIMESTAMP(testingtable1.created_time) )
);
Noch ein Update
Wie prouser1166147
Bemerkungen. Ich habe meine Anfrage gemäß seiner Anfrage geschrieben. Im Bienenstock, denke ichINNER JOIN
werden einfach von geschriebenJOIN
.
Dies ist meine Frage unten.
select * from (select t2.buyer_id, t2.item_id, t2.created_time as created_time, subq.user_id, subq.product_id, subq.timestamps as timestamps
from
(select user_id, prod_and_ts.product_id as product_id, prod_and_ts.timestamps as timestamps from testingtable2 lateral view explode(purchased_item) exploded_table as prod_and_ts) subq JOIN testingtable1 t2 on t2.buyer_id = subq.user_id
AND subq.timestamps = unix_timestamp(t2.created_time)
WHERE (subq.product_id <> t2.item_id)
union all
select t2.buyer_id, t2.item_id as item_id, t2.created_time, subq.user_id, subq.product_id as product_id, subq.timestamps
from
(select user_id, prod_and_ts.product_id as product_id, prod_and_ts.timestamps as timestamps from testingtable2 lateral view explode(purchased_item) exploded_table as prod_and_ts) subq JOIN testingtable1 t2 on t2.buyer_id = subq.user_id
and subq.product_id = t2.item_id
WHERE (subq.timestamps <> unix_timestamp(t2.created_time))) unionall;
Und nachdem ich die obige Abfrage ausgeführt habe, erhalte ich das Ergebnis Null zurück.
Ein letztes UPDATE: -
Leider hatte ich nicht die richtigen Daten in den Tabellen, deshalb habe ich das Ergebnis nicht zurückbekommen. Ja, es funktioniert die obige Abfrage.