1

1

1

1

1

CREATE EXTERNAL TABLE IF NOT EXISTS ventes (
date STRING,
produit STRING,
quantite INT,
prix_unitaire INT,
region STRING
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ‘,’
STORED AS TEXTFILE
LOCATION ‘/user/cloudera/tp_hive/data’
TBLPROPERTIES (‘skip.header.line.count’=’1’);

SELECT
region,
SUM(quantite * prix_unitaire) AS ca_total
FROM ventes
GROUP BY region
ORDER BY ca_total DESC;

Location: hdfs://…/user/cloudera/tp_hive/data

SELECT
region,
SUM(quantite * prix_unitaire) AS ca_total
FROM ventes
GROUP BY region
ORDER BY ca_total DESC;

SELECT
produit,
SUM(quantite) AS qte_totale
FROM ventes
GROUP BY produit
ORDER BY qte_totale DESC
LIMIT 1;

SELECT
produit,
AVG(quantite) AS qte_moy
FROM ventes
GROUP BY produit
ORDER BY qte_moy DESC;

SELECT
produit,
quantite,
(quantite * prix_unitaire) AS chiffre_affaires
FROM ventes
WHERE region = ‘Sud’;

— TP3 : Apache Pig (ventes.csv)
— Fichier HDFS: /user/cloudera/tp_hive/data/ventes.csv

— (1) Charger les données avec schéma
raw = LOAD ‘/user/cloudera/tp_hive/data/ventes.csv’
USING PigStorage(‘,’)
AS (date:chararray, produit:chararray, quantite:int, prix_unitaire:int, region:chararray);

— Ignorer l’entête
ventes = FILTER raw BY date != ‘date’;

— Ajouter chiffre_affaires
ventes2 = FOREACH ventes GENERATE
date, produit, quantite, prix_unitaire, region,
(quantite * prix_unitaire) AS chiffre_affaires;

— (2) CA total par région
grpR = GROUP ventes2 BY region;
ca_region = FOREACH grpR GENERATE group AS region, SUM(ventes2.chiffre_affaires) AS ca_total;
DUMP ca_region;

— (3) Produit le plus vendu (quantité totale)
grpP = GROUP ventes2 BY produit;
qte_prod = FOREACH grpP GENERATE group AS produit, SUM(ventes2.quantite) AS qte_totale;
qte_prod_sorted = ORDER qte_prod BY qte_totale DESC;
top1 = LIMIT qte_prod_sorted 1;
DUMP top1;

— (4) Moyenne des ventes (quantité) par produit
moy_prod = FOREACH grpP GENERATE group AS produit, AVG(ventes2.quantite) AS qte_moy;
DUMP moy_prod;

— TP3 : Apache Pig (ventes.csv)
— Fichier HDFS: /user/cloudera/tp_hive/data/ventes.csv

— (1) Charger les données avec schéma
raw = LOAD ‘/user/cloudera/tp_hive/data/ventes.csv’
USING PigStorage(‘,’)
AS (date:chararray, produit:chararray, quantite:int, prix_unitaire:int, region:chararray);

— Ignorer l’entête
ventes = FILTER raw BY date != ‘date’;

— Ajouter chiffre_affaires
ventes2 = FOREACH ventes GENERATE
date, produit, quantite, prix_unitaire, region,
(quantite * prix_unitaire) AS chiffre_affaires;

— (2) CA total par région
grpR = GROUP ventes2 BY region;
ca_region = FOREACH grpR GENERATE group AS region, SUM(ventes2.chiffre_affaires) AS ca_total;
DUMP ca_region;

— (3) Produit le plus vendu (quantité totale)
grpP = GROUP ventes2 BY produit;
qte_prod = FOREACH grpP GENERATE group AS produit, SUM(ventes2.quantite) AS qte_totale;
qte_prod_sorted = ORDER qte_prod BY qte_totale DESC;
top1 = LIMIT qte_prod_sorted 1;
DUMP top1;

— (4) Moyenne des ventes (quantité) par produit
moy_prod = FOREACH grpP GENERATE group AS produit, AVG(ventes2.quantite) AS qte_moy;
DUMP moy_prod;

— (5) Ventes (produit, quantité, CA) pour la région ‘Nord’
ventes_nord = FILTER ventes2 BY region == ‘Nord’;
proj_nord = FOREACH ventes_nord GENERATE produit, quantite, chiffre_affaires;
DUMP proj_nord;

AS (date:chararray, produit:chararray, quantite:int, prix_unitaire:int, region:chararray);

ventes_NV = FILTER ventes BY date != ‘date’;

ventes_2 = FOREACH ventes_NV GENERATE *, quantite * prix_unitaire AS chiffre_affaires;

ventes_nord = FILTER ventes_2 BY region == ‘Nord’;
DUMP ventes_nord;

groupe_par_produit = GROUP ventes_2 BY produit;

ca_produit = FOREACH groupe_par_produit GENERATE group AS produit,
SUM(ventes_2.chiffre_affaires) AS ca_total;

DUMP ca_produit;

— TP3 – Partie 2 : Apache Pig
— Source HDFS
— On ignore l’en-tête via un FILTER (ex: « date,produit,quantite,prix_unitaire,Region »)

A = LOAD ‘/user/cloudera/tp_hive/data/ventes.csv’
USING PigStorage(‘,’)
AS (date:chararray, produit:chararray, quantite:int, prix_unitaire:double, region:chararray);

— Ignorer l’en-tête
V = FILTER A BY date != ‘date’;

— Ajouter chiffre d’affaires (CA)
V2 = FOREACH V GENERATE
date,
produit,
quantite,
prix_unitaire,
region,
(quantite * prix_unitaire) AS ca;

— Q2: Chiffre d’affaires total par région
G_region = GROUP V2 BY region;
CA_region = FOREACH G_region GENERATE group AS region, SUM(V2.ca) AS ca_total;

— Q3: Produit le plus vendu en quantité
G_prod = GROUP V2 BY produit;
Q_prod = FOREACH G_prod GENERATE group AS produit, SUM(V2.quantite) AS qte_total;
Q_prod_sorted = ORDER Q_prod BY qte_total DESC;
Top1 = LIMIT Q_prod_sorted 1;

— Q4: Moyenne des ventes (quantité) par produit
Avg_prod = FOREACH G_prod GENERATE group AS produit, AVG(V2.quantite) AS qte_moy;

— Q5: Ventes (produit, quantité, chiffre d’affaires) pour la région ‘Nord’
Nord = FILTER V2 BY region == ‘Nord’;
Nord_view = FOREACH Nord GENERATE produit, quantite, ca;

— Affichages (DUMP)
DUMP CA_region;
DUMP Top1;
DUMP Avg_prod;
DUMP Nord_view;

111111111111111111111111111111111111111111111111111111111

— TP3 – Partie 2 : Apache Pig
— Source HDFS
— On ignore l’en-tête via un FILTER (ex: « date,produit,quantite,prix_unitaire,Region »)

A = LOAD ‘/user/cloudera/tp_hive/data/ventes.csv’
USING PigStorage(‘,’)
AS (date:chararray, produit:chararray, quantite:int, prix_unitaire:double, region:chararray);

— Ignorer l’en-tête
V = FILTER A BY date != ‘date’;

— Ajouter chiffre d’affaires (CA)
V2 = FOREACH V GENERATE
date,
produit,
quantite,
prix_unitaire,
region,
(quantite * prix_unitaire) AS ca;

— Q2: Chiffre d’affaires total par région
G_region = GROUP V2 BY region;
CA_region = FOREACH G_region GENERATE group AS region, SUM(V2.ca) AS ca_total;

— Q3: Produit le plus vendu en quantité
G_prod = GROUP V2 BY produit;
Q_prod = FOREACH G_prod GENERATE group AS produit, SUM(V2.quantite) AS qte_total;
Q_prod_sorted = ORDER Q_prod BY qte_total DESC;
Top1 = LIMIT Q_prod_sorted 1;

— Q4: Moyenne des ventes (quantité) par produit
Avg_prod = FOREACH G_prod GENERATE group AS produit, AVG(V2.quantite) AS qte_moy;

— Q5: Ventes (produit, quantité, chiffre d’affaires) pour la région ‘Nord’
Nord = FILTER V2 BY region == ‘Nord’;
Nord_view = FOREACH Nord GENERATE produit, quantite, ca;

— Affichages (DUMP)
DUMP CA_region;
DUMP Top1;
DUMP Avg_prod;
DUMP Nord_view;

Retour en haut