使用bicon获取ID?
Using bicond to get ID?
我有这个 table 的片段叫做 joinTbl
:
PRODUCT_ID PRODUCT_NAME ORDER_ID PRODUCT_ID CUSTOMER_ID SALESPERSON_ID UNIT_PRICE
11 CAKE 10946 11 83 1 31
11 CAKE 10949 11 10 2 31
11 CAKE 11020 11 56 2 31
14 CHICKEN 11076 14 9 4 23.25
11 CAKE 11077 11 65 1 31
14 CHICKEN 11077 14 65 1 23.25
在 Pig Apache 中,如果订单同时包含蛋糕和鸡肉,我将尝试获取 ORDER_ID
。预期结果是
11077
然而,我在尝试做一个 bicond 来获得这个 ORDER_ID
时遇到了问题。这是我使用的语法:
cakeChicken = FOREACH joinedTbl GENERATE ((PRODUCT_NAME == 'CAKE' AND PRODUCT_NAME == 'CHICKEN') ? ORDER_ID : 0) AS order_both;
return 就是 0
else 语句。
我做错了什么?
您的 bincond 工作正常 - 因为它在 FOREACH
内,它将单独检查每一行数据。因此,每一行只有一个 PRODUCT_NAME
的值,因此它不能同时是 'CAKE'
和 'CHICKEN'
.
根据你想做的,我会use GROUP BY
to group on ORDER_ID
and then filter the PRODUCT_NAME
bag to just those that contain 'CAKE'
and 'CHICKEN'
using a nested foreach。最后,将数据过滤到“不为空”的包中。像这样:
groupedData = GROUP joinTbl BY ORDER_ID;
/* Structure:
---------------------------------------------------------------------------------------------------------------------------------
| groupedData | group | joinTbl: bag({PRODUCT_ID, PRODUCT_NAME, ORDER_ID, PRODUCT_ID, CUSTOMER_ID, SALESPERSON_ID, UNIT_PRICE}) |
---------------------------------------------------------------------------------------------------------------------------------
| | 10946 | {(11, CAKE, 10946, 11, 83, 1, 31)} |
| | 11077 | {(11, CAKE, 11077, 11, 65, 1, 31), (14, CHICKEN, 11077, 14, 65, 1, 23.25)} |
---------------------------------------------------------------------------------------------------------------------------------
*/
cakeChickenIds = FOREACH groupedData {
cakes = FILTER joinTbl BY PRODUCT_NAME == 'CAKE';
chickens = FILTER joinTbl BY PRODUCT_NAME == 'CHICKEN';
GENERATE group AS ORDER_ID,
cakes,
chickens;
}
/* Structure:
------------------------------------------------------------------------------------------
| cakeChickenIds | ORDER_ID | cakes: bag({PRODUCT_NAME}) | chickens: bag({PRODUCT_NAME}) |
------------------------------------------------------------------------------------------
| | 10946 | {(CAKE)} | {()} |
| | 11077 | {(CAKE)} | {(CHICKEN)} |
------------------------------------------------------------------------------------------
*/
-- Both cakes and chickens bags will not be empty if ordered both
cakeChickenOrders = FILTER cakeChickenIds BY NOT IsEmpty(cakes) AND NOT IsEmpty(chickens);
我有这个 table 的片段叫做 joinTbl
:
PRODUCT_ID PRODUCT_NAME ORDER_ID PRODUCT_ID CUSTOMER_ID SALESPERSON_ID UNIT_PRICE
11 CAKE 10946 11 83 1 31
11 CAKE 10949 11 10 2 31
11 CAKE 11020 11 56 2 31
14 CHICKEN 11076 14 9 4 23.25
11 CAKE 11077 11 65 1 31
14 CHICKEN 11077 14 65 1 23.25
在 Pig Apache 中,如果订单同时包含蛋糕和鸡肉,我将尝试获取 ORDER_ID
。预期结果是
11077
然而,我在尝试做一个 bicond 来获得这个 ORDER_ID
时遇到了问题。这是我使用的语法:
cakeChicken = FOREACH joinedTbl GENERATE ((PRODUCT_NAME == 'CAKE' AND PRODUCT_NAME == 'CHICKEN') ? ORDER_ID : 0) AS order_both;
return 就是 0
else 语句。
我做错了什么?
您的 bincond 工作正常 - 因为它在 FOREACH
内,它将单独检查每一行数据。因此,每一行只有一个 PRODUCT_NAME
的值,因此它不能同时是 'CAKE'
和 'CHICKEN'
.
根据你想做的,我会use GROUP BY
to group on ORDER_ID
and then filter the PRODUCT_NAME
bag to just those that contain 'CAKE'
and 'CHICKEN'
using a nested foreach。最后,将数据过滤到“不为空”的包中。像这样:
groupedData = GROUP joinTbl BY ORDER_ID;
/* Structure:
---------------------------------------------------------------------------------------------------------------------------------
| groupedData | group | joinTbl: bag({PRODUCT_ID, PRODUCT_NAME, ORDER_ID, PRODUCT_ID, CUSTOMER_ID, SALESPERSON_ID, UNIT_PRICE}) |
---------------------------------------------------------------------------------------------------------------------------------
| | 10946 | {(11, CAKE, 10946, 11, 83, 1, 31)} |
| | 11077 | {(11, CAKE, 11077, 11, 65, 1, 31), (14, CHICKEN, 11077, 14, 65, 1, 23.25)} |
---------------------------------------------------------------------------------------------------------------------------------
*/
cakeChickenIds = FOREACH groupedData {
cakes = FILTER joinTbl BY PRODUCT_NAME == 'CAKE';
chickens = FILTER joinTbl BY PRODUCT_NAME == 'CHICKEN';
GENERATE group AS ORDER_ID,
cakes,
chickens;
}
/* Structure:
------------------------------------------------------------------------------------------
| cakeChickenIds | ORDER_ID | cakes: bag({PRODUCT_NAME}) | chickens: bag({PRODUCT_NAME}) |
------------------------------------------------------------------------------------------
| | 10946 | {(CAKE)} | {()} |
| | 11077 | {(CAKE)} | {(CHICKEN)} |
------------------------------------------------------------------------------------------
*/
-- Both cakes and chickens bags will not be empty if ordered both
cakeChickenOrders = FILTER cakeChickenIds BY NOT IsEmpty(cakes) AND NOT IsEmpty(chickens);