bd-lab-1339 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,80 @@
1
+ Metadata-Version: 2.4
2
+ Name: bd_lab_1339
3
+ Version: 0.1.0
4
+ Summary: Nine ISL75 Big Data Lab exercises for Hadoop, Spark, and Pig
5
+ Requires-Python: >=3.9
6
+ Description-Content-Type: text/markdown
7
+
8
+ # ISL75 Big Data Lab
9
+
10
+ Nine complete exercise programs with small synthetic samples. The wheel
11
+ exports Java MapReduce, PySpark and Pig Latin sources to any writable
12
+ directory. See RUN.md in the exported folder for the Ubuntu run commands.
13
+
14
+ ## 1. Build on Windows
15
+
16
+ Open PowerShell in this project folder and run:
17
+
18
+ ~~~powershell
19
+ py -3.13 -m venv .venv
20
+ .\.venv\Scripts\python.exe -m pip install --upgrade build twine
21
+ .\.venv\Scripts\python.exe -m build
22
+ .\.venv\Scripts\python.exe -m twine check dist/*
23
+ ~~~
24
+
25
+ On Ubuntu, use python3 -m venv .venv and .venv/bin/python in place of the
26
+ Windows paths. If Python 3.13 is unavailable on Windows, use py -3.
27
+
28
+ ## 2. Try the local wheel
29
+
30
+ ~~~powershell
31
+ .\.venv\Scripts\python.exe -m pip install .\dist\bd_lab_1339-0.1.0-py3-none-any.whl
32
+ .\.venv\Scripts\python.exe -m isl75_big_data_lab list
33
+ .\.venv\Scripts\python.exe -m isl75_big_data_lab export .\lab-programs
34
+ ~~~
35
+
36
+ Use a fresh directory name each time you export. The installed
37
+ big-data-lab command does the same thing when the environment is active.
38
+
39
+ ## 3. Publish
40
+
41
+ First confirm you can claim the distribution name. If it has already been
42
+ claimed by someone else, change project.name in pyproject.toml and rebuild
43
+ into an empty dist directory. For later releases, increment the version.
44
+
45
+ Create a TestPyPI account and token. Upload with:
46
+
47
+ ~~~powershell
48
+ .\.venv\Scripts\python.exe -m twine upload --repository testpypi dist/*
49
+ ~~~
50
+
51
+ Enter __token__ if prompted for a username and use the TestPyPI token when
52
+ prompted. Test with this command in a fresh virtual environment:
53
+
54
+ ~~~bash
55
+ python -m pip install --index-url https://test.pypi.org/simple/ --no-deps bd_lab_1339
56
+ ~~~
57
+
58
+ Create a separate account and token for the real PyPI. Publish with:
59
+
60
+ ~~~powershell
61
+ .\.venv\Scripts\python.exe -m twine upload dist/*
62
+ ~~~
63
+
64
+ Keep tokens out of the project and command history. See the
65
+ [Python Packaging User Guide](https://packaging.python.org/en/latest/tutorials/packaging-projects/).
66
+
67
+ ## 4. Install from PyPI after publishing
68
+
69
+ In an active virtual environment:
70
+
71
+ ~~~bash
72
+ python -m pip install bd_lab_1339
73
+ big-data-lab list
74
+ big-data-lab export ./lab-programs
75
+ cd lab-programs
76
+ bash run.sh A1
77
+ ~~~
78
+
79
+ On Ubuntu use python3 in place of python if that is the interpreter name
80
+ in your environment. See RUN.md for all program commands and sample outputs.
@@ -0,0 +1,29 @@
1
+ isl75_big_data_lab/__init__.py,sha256=riaggydITyTyqjsvPke7bH3Ikvom9o8SGLQXhawpRCU,39
2
+ isl75_big_data_lab/__main__.py,sha256=WnPCVGP1VLu9veo5R24ux0TS5iR6j6GDmOkn7yWeA9A,1559
3
+ isl75_big_data_lab/lab/RUN.md,sha256=fAV1q73yao2j4j1rLrP-idA0k9mTL9wuXsBG2CK7im8,2105
4
+ isl75_big_data_lab/lab/run.sh,sha256=1r4U8Img3Vn2kDThP-B1_LT3x_9pQN_6nw0wiiYosGs,1386
5
+ isl75_big_data_lab/lab/Dataset/A3. EmpDataset.csv,sha256=zlp7eEjAJEUgUfytSc9smeE8qA7Td-DSvLPRt6YGbDY,66
6
+ isl75_big_data_lab/lab/Dataset/A4. Titanic.csv,sha256=H5QS1uJLQzUCxLl-1j08tQjyHoDgblXMDebThLiVXC8,210
7
+ isl75_big_data_lab/lab/Dataset/B2. Weather.txt,sha256=Z8xJdmQ6W0ObHsh3-4byE9R0mtMaUkcAKq2G1jpojMQ,282
8
+ isl75_big_data_lab/lab/Dataset/B4. Crop_Production.csv,sha256=1WMLNgd7bsFUWJP4FpEJipfuPo2EA_yvTC28mQlkC2E,258
9
+ isl75_big_data_lab/lab/Dataset/B5. olympic_athletes.csv,sha256=SYfXGyksz2ZnarquWf3hW1bnGbNulTyVLRo7drrUdO4,242
10
+ isl75_big_data_lab/lab/Dataset/B5. olympic_host.csv,sha256=lYp0YXJ1l0_TDXE5_ccdA80bkfROHxeml8vAmuze9jU,224
11
+ isl75_big_data_lab/lab/Dataset/B6. online-retail.csv,sha256=k8wnPCSQQTN8iGqwR68n2bPQFpbzm61yC0J0HnC_eko,267
12
+ isl75_big_data_lab/lab/Dataset/numbers.txt,sha256=HFhCej1Wiznk82roo3noU6wPG3Ph1l0V_McgonWjETo,10
13
+ isl75_big_data_lab/lab/Dataset/paragraph.txt,sha256=72CAk7iZmewYT_HXE_V7EV8NK4S7g8OY9UvEyRKC2pA,29
14
+ isl75_big_data_lab/lab/Dataset/words.txt,sha256=fv4FG8kyed1oMjsa4P9Fl7etRShxpvgnuXMPxd3gGOQ,22
15
+ isl75_big_data_lab/lab/hadoop/A1/WordCount.java,sha256=SqNNaE42_059i49leXCXA_JmQDH2rudYMjwuFl4uTTI,1533
16
+ isl75_big_data_lab/lab/hadoop/A2/OddEven.java,sha256=YAx9WgSYr05J_XE3uznaYJ3WjZ_GUiVjNR3Q37b6yUo,1776
17
+ isl75_big_data_lab/lab/hadoop/A3/Employee.java,sha256=bFHQxfzylMvIH33lWEMrz8hUPhxPN58cnbq_A3XHMoE,1632
18
+ isl75_big_data_lab/lab/hadoop/A4/Titanic.java,sha256=JrXxfXfR9G-viOT7CMOZZNVg7hB6xa2wojWSuWSa8LE,1924
19
+ isl75_big_data_lab/lab/pig/B4/crops.pig,sha256=sybncXU22vvwCl3l-8DFMhxOWQKXPqgicUV90j5SwaY,916
20
+ isl75_big_data_lab/lab/pig/B5/olympics.pig,sha256=CJGvu_ujiPz1qtxoxhPl428tE5f6WJaXPzbToZ_Fl4w,477
21
+ isl75_big_data_lab/lab/pig/B5/prepare.py,sha256=vujvh_5IWwH5KrMZbeYYMsJSrG2XBdWFiww4eK0bt8E,505
22
+ isl75_big_data_lab/lab/pig/B6/retail.pig,sha256=jDNbqZTRdylxz1Uo3JvtYqucuiFO9MbITAMF00694fk,1187
23
+ isl75_big_data_lab/lab/spark/B1/lengths.py,sha256=L4x6aK-3qCf3_Lzry7K-m-RtSHUVq7e25lW6yrsP9i0,349
24
+ isl75_big_data_lab/lab/spark/B2/weather.py,sha256=rNSNVvZOFEodgQN-Lgx54KZ4Vy7hAqc4-xuVrNRjmxM,718
25
+ bd_lab_1339-0.1.0.dist-info/METADATA,sha256=kggGJPQC4CI2gLNfPOM0ZorRhF-wN-2h6uTXpxM1gQs,2586
26
+ bd_lab_1339-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
27
+ bd_lab_1339-0.1.0.dist-info/entry_points.txt,sha256=1xt2InIehBbYY0DzbdRtZoJafH-QOVstRQ8zvLuzCSU,66
28
+ bd_lab_1339-0.1.0.dist-info/top_level.txt,sha256=glGSPpRe4wojOl4esSX94KSw65N5eexT7WlOkaxxNIE,19
29
+ bd_lab_1339-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ big-data-lab = isl75_big_data_lab.__main__:main
@@ -0,0 +1 @@
1
+ isl75_big_data_lab
@@ -0,0 +1 @@
1
+ """Installable ISL75 lab exercises."""
@@ -0,0 +1,44 @@
1
+ import argparse
2
+ from importlib.resources import files
3
+ from pathlib import Path
4
+
5
+
6
+ def copy_tree(source, target):
7
+ target.mkdir()
8
+ for entry in source.iterdir():
9
+ if entry.name == '__pycache__':
10
+ continue
11
+ dest = target / entry.name
12
+ if entry.is_dir():
13
+ copy_tree(entry, dest)
14
+ else:
15
+ dest.write_bytes(entry.read_bytes())
16
+
17
+
18
+ def main():
19
+ parser = argparse.ArgumentParser(description="Access the ISL75 lab programs.")
20
+ commands = parser.add_subparsers(dest="command", required=True)
21
+ commands.add_parser("list", help="List the nine programs")
22
+ export = commands.add_parser("export", help="Copy programs and samples to a new folder")
23
+ export.add_argument("destination", type=Path)
24
+ args = parser.parse_args()
25
+ source = files("isl75_big_data_lab") / "lab"
26
+
27
+ if args.command == "list":
28
+ for folder in ("hadoop", "spark", "pig"):
29
+ for program in sorted((source / folder).iterdir(), key=lambda p: p.name):
30
+ names = ", ".join(sorted(entry.name for entry in program.iterdir() if entry.is_file()))
31
+ print(f"{program.name} {folder} {names}")
32
+ return
33
+
34
+ destination = args.destination.expanduser().resolve()
35
+ destination.parent.mkdir(parents=True, exist_ok=True)
36
+ try:
37
+ copy_tree(source, destination)
38
+ except FileExistsError:
39
+ parser.error(f"Destination already exists: {destination}")
40
+ print(f"Exported programs, sample data, and RUN.md to {destination}")
41
+
42
+
43
+ if __name__ == "__main__":
44
+ main()
@@ -0,0 +1,3 @@
1
+ 901,1,32,Female,240000
2
+ 902,1,31,Male,60000
3
+ 903,2,33,Female,180000
@@ -0,0 +1,6 @@
1
+ PassengerId,Survived,Pclass,Name,Sex,Age
2
+ 1,0,3,Sample Passenger A,male,22
3
+ 2,0,3,Sample Passenger B,female,34
4
+ 3,1,1,Sample Passenger C,male,25
5
+ 4,1,2,Sample Passenger D,female,35
6
+ 5,1,3,Sample Passenger E,male,19
@@ -0,0 +1,3 @@
1
+ 000000000199999194200000000000000000000000000000000000000000000000000000000000000000000-00551
2
+ 000000000299999194200000000000000000000000000000000000000000000000000000000000000000000+01111
3
+ 000000000299999194200000000000000000000000000000000000000000000000000000000000000000000+00781
@@ -0,0 +1,5 @@
1
+ State_Name,District_Name,Crop_Year,Season,Crop,Area,Production
2
+ Karnataka,Sample District,2010,Kharif,Rice,10,20
3
+ Karnataka,Sample District,2010,Rabi,Rice,5,10
4
+ Karnataka,Sample District,2011,Kharif,Rice,12,36
5
+ Other State,Sample District,2010,Kharif,Wheat,8,16
@@ -0,0 +1,3 @@
1
+ athlete_url,athlete_full_name,games_participations,first_game,athlete_year_birth,athlete_medals,bio
2
+ https://example.invalid/a,Sample Athlete A,1,Tokyo 2020,2000,,Example
3
+ https://example.invalid/b,Sample Athlete B,1,Beijing 2022,2001,,Example
@@ -0,0 +1,4 @@
1
+ game_slug,game_end_date,game_start_date,game_location,game_name,game_season,game_year
2
+ tokyo-2020,,,Japan,Tokyo 2020,Summer,2020
3
+ beijing-2022,,,China,Beijing 2022,Winter,2022
4
+ sample-1998,,,Exampleland,Sample 1998,Winter,1998
@@ -0,0 +1,5 @@
1
+ InvoiceNo,StockCode,Description,Quantity,InvoiceDate,UnitPrice,CustomerID,Country
2
+ 100,A,Sample item,10,2009,2.50,C1,United Kingdom
3
+ 100,B,Sample item,3,2009,4.00,C1,United Kingdom
4
+ 101,A,Sample item,2,2009,3.50,C2,France
5
+ 102,C,Sample item,4,2010,2.00,C3,United Kingdom
@@ -0,0 +1,2 @@
1
+ 1 2 3
2
+ 4 5
@@ -0,0 +1,2 @@
1
+ Big data lab.
2
+ Spark is fast!
@@ -0,0 +1,2 @@
1
+ big data big
2
+ lab data
@@ -0,0 +1,55 @@
1
+ # Run the lab programs
2
+
3
+ Install the bundle with Python 3.9 or newer on Windows, macOS or Linux.
4
+ The commands below run the exercises from Bash on Ubuntu or WSL.
5
+ Use the Java/JDK matched to your course Hadoop, Spark and Pig installations.
6
+ Make these tools available on PATH as needed: javac, jar, hadoop,
7
+ spark-submit, python3 and pig. pip installs the source files and sample inputs.
8
+
9
+ From the exported folder:
10
+
11
+ ~~~bash
12
+ bash run.sh A1
13
+ bash run.sh A2
14
+ bash run.sh A3
15
+ bash run.sh A4
16
+ bash run.sh B1
17
+ bash run.sh B2 1942
18
+ bash run.sh B4
19
+ bash run.sh B5
20
+ bash run.sh B6
21
+ ~~~
22
+
23
+ The Hadoop runner uses local mode with file URLs. The Pig runner uses
24
+ local mode. Each run creates a fresh run-* directory.
25
+
26
+ ## Use your course inputs
27
+
28
+ The bundled Dataset directory contains small invented samples. To use
29
+ the original course files, give the full path to your Dataset directory.
30
+
31
+ ~~~bash
32
+ LAB_DATA_DIR="$HOME/Big_data_Lab_programs/Dataset" bash run.sh A3
33
+ LAB_DATA_DIR="$HOME/Big_data_Lab_programs/Dataset" bash run.sh B5
34
+ ~~~
35
+
36
+ The override applies to every input for that command. For A1, A2 and B1
37
+ use the bundled inputs, or put custom words.txt, numbers.txt and
38
+ paragraph.txt in a directory of your choice and point LAB_DATA_DIR there.
39
+
40
+ ## Sample results
41
+
42
+ | ID | Result to check |
43
+ |---|---|
44
+ | A1 | big 2, data 2, lab 1 |
45
+ | A2 | Sum 15, Odd 3, Even 2 |
46
+ | A3 | Female 2, average 210000.0. Male 1, average 60000.0. |
47
+ | A4 | Average death ages 22.0 male, 34.0 female, 28.0 overall. Each class has one survivor. |
48
+ | B1 | 2 lines. Word lengths in order are 3, 4, 3, 5, 2, 4. |
49
+ | B2 | 1942 minimum -5.5 C at 000001-99999 and maximum 11.1 C at 000002-99999. |
50
+ | B4 | Rice production 66, annual average 33, and 2010 area 15. Rice is the only Karnataka crop in the sample. |
51
+ | B5 | One Tokyo first-Games athlete, Beijing 2022 in China, one Summer and two Winter games, and two games after 2000. |
52
+ | B6 | 2 unique UK customers, 2 distinct 2009 invoices, France revenue 7.0, and United Kingdom 17 items. |
53
+
54
+ The Olympic query uses the first Games field provided by the athlete CSV.
55
+ The weather output identifies locations by station ID.
@@ -0,0 +1,38 @@
1
+ import java.io.IOException;
2
+ import org.apache.hadoop.fs.Path;
3
+ import org.apache.hadoop.io.*;
4
+ import org.apache.hadoop.mapreduce.*;
5
+ import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
6
+ import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
7
+ import org.apache.hadoop.util.GenericOptionsParser;
8
+ public class WordCount {
9
+ public static class M extends Mapper<LongWritable, Text, Text, Text> {
10
+ public void map(LongWritable offset, Text row, Context c)
11
+ throws IOException, InterruptedException {
12
+ for (String word : row.toString().split("\\s+"))
13
+ if (!word.isEmpty()) c.write(new Text(word), new Text("1"));
14
+ }
15
+ }
16
+ public static class R extends Reducer<Text, Text, Text, Text> {
17
+ public void reduce(Text key, Iterable<Text> values, Context c)
18
+ throws IOException, InterruptedException {
19
+ int count = 0;
20
+ for (Text one : values) count++;
21
+ c.write(key, new Text("" + count));
22
+ }
23
+ }
24
+ public static void main(String[] args) throws Exception {
25
+ Job job = Job.getInstance();
26
+ args = new GenericOptionsParser(job.getConfiguration(), args)
27
+ .getRemainingArgs();
28
+ job.setJarByClass(WordCount.class);
29
+ job.setMapperClass(M.class);
30
+ job.setReducerClass(R.class);
31
+ job.setNumReduceTasks(1);
32
+ job.setOutputKeyClass(Text.class);
33
+ job.setOutputValueClass(Text.class);
34
+ FileInputFormat.addInputPath(job, new Path(args[0]));
35
+ FileOutputFormat.setOutputPath(job, new Path(args[1]));
36
+ System.exit(job.waitForCompletion(true) ? 0 : 1);
37
+ }
38
+ }
@@ -0,0 +1,44 @@
1
+ import java.io.IOException;
2
+ import org.apache.hadoop.fs.Path;
3
+ import org.apache.hadoop.io.*;
4
+ import org.apache.hadoop.mapreduce.*;
5
+ import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
6
+ import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
7
+ import org.apache.hadoop.util.GenericOptionsParser;
8
+ public class OddEven {
9
+ public static class M extends Mapper<LongWritable, Text, Text, Text> {
10
+ public void map(LongWritable offset, Text row, Context c)
11
+ throws IOException, InterruptedException {
12
+ for (String n : row.toString().split("\\s+"))
13
+ if (!n.isEmpty()) c.write(new Text("numbers"), new Text(n));
14
+ }
15
+ }
16
+ public static class R extends Reducer<Text, Text, Text, Text> {
17
+ public void reduce(Text key, Iterable<Text> values, Context c)
18
+ throws IOException, InterruptedException {
19
+ long sum = 0, odd = 0, even = 0;
20
+ for (Text value : values) {
21
+ long n = Long.parseLong(value.toString());
22
+ sum += n;
23
+ if (n % 2 == 0) even++; else odd++;
24
+ }
25
+ c.write(new Text("Sum"), new Text("" + sum));
26
+ c.write(new Text("Odd"), new Text("" + odd));
27
+ c.write(new Text("Even"), new Text("" + even));
28
+ }
29
+ }
30
+ public static void main(String[] args) throws Exception {
31
+ Job job = Job.getInstance();
32
+ args = new GenericOptionsParser(job.getConfiguration(), args)
33
+ .getRemainingArgs();
34
+ job.setJarByClass(OddEven.class);
35
+ job.setMapperClass(M.class);
36
+ job.setReducerClass(R.class);
37
+ job.setNumReduceTasks(1);
38
+ job.setOutputKeyClass(Text.class);
39
+ job.setOutputValueClass(Text.class);
40
+ FileInputFormat.addInputPath(job, new Path(args[0]));
41
+ FileOutputFormat.setOutputPath(job, new Path(args[1]));
42
+ System.exit(job.waitForCompletion(true) ? 0 : 1);
43
+ }
44
+ }
@@ -0,0 +1,42 @@
1
+ import java.io.IOException;
2
+ import org.apache.hadoop.fs.Path;
3
+ import org.apache.hadoop.io.*;
4
+ import org.apache.hadoop.mapreduce.*;
5
+ import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
6
+ import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
7
+ import org.apache.hadoop.util.GenericOptionsParser;
8
+ public class Employee {
9
+ public static class M extends Mapper<LongWritable, Text, Text, Text> {
10
+ public void map(LongWritable offset, Text row, Context c)
11
+ throws IOException, InterruptedException {
12
+ String[] f = row.toString().split(",");
13
+ if (f.length >= 5) c.write(new Text(f[3]), new Text(f[4]));
14
+ }
15
+ }
16
+ public static class R extends Reducer<Text, Text, Text, Text> {
17
+ public void reduce(Text key, Iterable<Text> values, Context c)
18
+ throws IOException, InterruptedException {
19
+ double sum = 0;
20
+ int count = 0;
21
+ for (Text salary : values) {
22
+ sum += Double.parseDouble(salary.toString());
23
+ count++;
24
+ }
25
+ c.write(key, new Text(count + "\t" + sum / count));
26
+ }
27
+ }
28
+ public static void main(String[] args) throws Exception {
29
+ Job job = Job.getInstance();
30
+ args = new GenericOptionsParser(job.getConfiguration(), args)
31
+ .getRemainingArgs();
32
+ job.setJarByClass(Employee.class);
33
+ job.setMapperClass(M.class);
34
+ job.setReducerClass(R.class);
35
+ job.setNumReduceTasks(1);
36
+ job.setOutputKeyClass(Text.class);
37
+ job.setOutputValueClass(Text.class);
38
+ FileInputFormat.addInputPath(job, new Path(args[0]));
39
+ FileOutputFormat.setOutputPath(job, new Path(args[1]));
40
+ System.exit(job.waitForCompletion(true) ? 0 : 1);
41
+ }
42
+ }
@@ -0,0 +1,50 @@
1
+ import java.io.IOException;
2
+ import org.apache.hadoop.fs.Path;
3
+ import org.apache.hadoop.io.*;
4
+ import org.apache.hadoop.mapreduce.*;
5
+ import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
6
+ import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
7
+ import org.apache.hadoop.util.GenericOptionsParser;
8
+ public class Titanic {
9
+ public static class M extends Mapper<LongWritable, Text, Text, Text> {
10
+ public void map(LongWritable offset, Text row, Context c)
11
+ throws IOException, InterruptedException {
12
+ String[] f = row.toString().split(",", -1);
13
+ if (f.length < 6) return;
14
+ if (f[1].equals("1"))
15
+ c.write(new Text("Class " + f[2]), new Text("1"));
16
+ if (f[1].equals("0") && !f[5].isEmpty()) {
17
+ c.write(new Text("Age " + f[4]), new Text(f[5]));
18
+ c.write(new Text("Age all"), new Text(f[5]));
19
+ }
20
+ }
21
+ }
22
+ public static class R extends Reducer<Text, Text, Text, Text> {
23
+ public void reduce(Text key, Iterable<Text> values, Context c)
24
+ throws IOException, InterruptedException {
25
+ double sum = 0;
26
+ int count = 0;
27
+ for (Text v : values) {
28
+ sum += Double.parseDouble(v.toString());
29
+ count++;
30
+ }
31
+ String result = key.toString().startsWith("Age")
32
+ ? "" + sum / count : "" + count;
33
+ c.write(key, new Text(result));
34
+ }
35
+ }
36
+ public static void main(String[] args) throws Exception {
37
+ Job job = Job.getInstance();
38
+ args = new GenericOptionsParser(job.getConfiguration(), args)
39
+ .getRemainingArgs();
40
+ job.setJarByClass(Titanic.class);
41
+ job.setMapperClass(M.class);
42
+ job.setReducerClass(R.class);
43
+ job.setNumReduceTasks(1);
44
+ job.setOutputKeyClass(Text.class);
45
+ job.setOutputValueClass(Text.class);
46
+ FileInputFormat.addInputPath(job, new Path(args[0]));
47
+ FileOutputFormat.setOutputPath(job, new Path(args[1]));
48
+ System.exit(job.waitForCompletion(true) ? 0 : 1);
49
+ }
50
+ }
@@ -0,0 +1,29 @@
1
+ C = LOAD 'crops.csv' USING PigStorage(',')
2
+ AS (state:chararray, district:chararray, yr:int, season:chararray,
3
+ crop:chararray, area:double, production:double);
4
+
5
+ -- 1. Total production by crop
6
+ P = FILTER C BY yr IS NOT NULL AND production IS NOT NULL;
7
+ G = GROUP P BY crop;
8
+ Total = FOREACH G GENERATE group, SUM(P.production);
9
+ DUMP Total;
10
+
11
+ -- 2. Sum by crop and year, then average the yearly totals
12
+ Y = GROUP P BY (crop, yr);
13
+ Yearly = FOREACH Y GENERATE group.crop AS crop,
14
+ SUM(P.production) AS annual;
15
+ G = GROUP Yearly BY crop;
16
+ Average = FOREACH G GENERATE group, AVG(Yearly.annual);
17
+ DUMP Average;
18
+
19
+ -- 3. Crop names grown in Karnataka
20
+ K = FILTER C BY state == 'Karnataka';
21
+ Names = FOREACH K GENERATE crop;
22
+ UniqueCrops = DISTINCT Names;
23
+ DUMP UniqueCrops;
24
+
25
+ -- 4. Total area by crop in 2010
26
+ A = FILTER C BY yr == 2010;
27
+ G = GROUP A BY crop;
28
+ Area2010 = FOREACH G GENERATE group, SUM(A.area);
29
+ DUMP Area2010;
@@ -0,0 +1,14 @@
1
+ A = LOAD 'athletes.tsv' USING PigStorage('\t')
2
+ AS (url:chararray, athlete:chararray, first_game:chararray);
3
+ H = LOAD 'hosts.tsv' USING PigStorage('\t')
4
+ AS (game:chararray, country:chararray, season:chararray, yr:int);
5
+
6
+ Tokyo = FILTER A BY first_game == 'Tokyo 2020';
7
+ DUMP Tokyo;
8
+ China = FILTER H BY country == 'China';
9
+ DUMP China;
10
+ G = GROUP H BY season;
11
+ SeasonCounts = FOREACH G GENERATE group, COUNT(H);
12
+ DUMP SeasonCounts;
13
+ Recent = FILTER H BY yr > 2000;
14
+ DUMP Recent;
@@ -0,0 +1,15 @@
1
+ import csv
2
+ import sys
3
+ from pathlib import Path
4
+
5
+ folder = Path(sys.argv[1])
6
+ for file, out, fields in [
7
+ ("B5. olympic_athletes.csv", "athletes.tsv", (0, 1, 3)),
8
+ ("B5. olympic_host.csv", "hosts.tsv", (4, 3, 5, 6)),
9
+ ]:
10
+ with (folder / file).open(encoding="utf-8-sig", newline="") as src:
11
+ with open(out, "w", encoding="utf-8") as dst:
12
+ rows = csv.reader(src)
13
+ next(rows)
14
+ for row in rows:
15
+ print(*(row[i] for i in fields), sep="\t", file=dst)
@@ -0,0 +1,35 @@
1
+ R = LOAD 'retail.csv' USING PigStorage(',')
2
+ AS (invoice:chararray, stock:chararray, description:chararray,
3
+ qty:int, yr:int, price:double, customer:chararray,
4
+ country:chararray);
5
+
6
+ -- 1. Count distinct UK customer IDs
7
+ UK = FILTER R BY country == 'United Kingdom'
8
+ AND customer IS NOT NULL AND customer != '';
9
+ IDs = FOREACH UK GENERATE customer;
10
+ UniqueIDs = DISTINCT IDs;
11
+ G = GROUP UniqueIDs ALL;
12
+ UKCount = FOREACH G GENERATE COUNT(UniqueIDs);
13
+ DUMP UKCount;
14
+
15
+ -- 2. Count distinct invoice numbers in 2009
16
+ Y = FILTER R BY yr == 2009;
17
+ Invoices = FOREACH Y GENERATE invoice;
18
+ UniqueBills = DISTINCT Invoices;
19
+ G = GROUP UniqueBills ALL;
20
+ BillCount = FOREACH G GENERATE COUNT(UniqueBills);
21
+ DUMP BillCount;
22
+
23
+ -- 3. Sum quantity times unit price for France
24
+ F = FILTER R BY country == 'France';
25
+ Amounts = FOREACH F GENERATE qty * price AS revenue;
26
+ G = GROUP Amounts ALL;
27
+ FranceRevenue = FOREACH G GENERATE SUM(Amounts.revenue);
28
+ DUMP FranceRevenue;
29
+
30
+ -- 4. Sum quantities by country and take the top country
31
+ G = GROUP R BY country;
32
+ Totals = FOREACH G GENERATE group AS country, SUM(R.qty) AS items;
33
+ Ranked = ORDER Totals BY items DESC, country ASC;
34
+ Top = LIMIT Ranked 1;
35
+ DUMP Top;
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ cd -- "$(dirname -- "$0")"
4
+ data=$(cd -- "${LAB_DATA_DIR:-Dataset}" && pwd)
5
+ work=$(mktemp -d "$PWD/run-${1:-lab}-XXXXXX")
6
+ echo "Run directory: $work"
7
+
8
+ case "${1:-}" in
9
+ A1) main=WordCount; input=words.txt ;;
10
+ A2) main=OddEven; input=numbers.txt ;;
11
+ A3) main=Employee; input='A3. EmpDataset.csv' ;;
12
+ A4) main=Titanic; input='A4. Titanic.csv' ;;
13
+ B1)
14
+ spark-submit --master 'local[*]' spark/B1/lengths.py "file://$data/paragraph.txt"
15
+ exit ;;
16
+ B2)
17
+ spark-submit --master 'local[*]' spark/B2/weather.py "file://$data/B2. Weather.txt" "${2:-1942}"
18
+ exit ;;
19
+ B4)
20
+ tail -n +2 "$data/B4. Crop_Production.csv" > "$work/crops.csv"
21
+ cd "$work"
22
+ pig -x local ../pig/B4/crops.pig
23
+ exit ;;
24
+ B5)
25
+ cd "$work"
26
+ python3 ../pig/B5/prepare.py "$data"
27
+ pig -x local ../pig/B5/olympics.pig
28
+ exit ;;
29
+ B6)
30
+ tail -n +2 "$data/B6. online-retail.csv" > "$work/retail.csv"
31
+ cd "$work"
32
+ pig -x local ../pig/B6/retail.pig
33
+ exit ;;
34
+ *) echo 'Usage: bash run.sh A1|A2|A3|A4|B1|B2|B4|B5|B6 [weather-year]' >&2; exit 2 ;;
35
+ esac
36
+
37
+ mkdir "$work/classes"
38
+ javac -cp "$(hadoop classpath --glob)" -d "$work/classes" "hadoop/$1/$main.java"
39
+ jar cf "$work/$main.jar" -C "$work/classes" .
40
+ hadoop jar "$work/$main.jar" "$main" -D mapreduce.framework.name=local \
41
+ "file://$data/$input" "file://$work/output"
42
+ cat "$work"/output/part-r-*
@@ -0,0 +1,14 @@
1
+ import re
2
+ import sys
3
+ from pyspark import SparkContext
4
+
5
+ sc = SparkContext(appName="WordLengths")
6
+ sc.setLogLevel("ERROR")
7
+ lines = sc.textFile(sys.argv[1])
8
+
9
+ print("Lines:", lines.count())
10
+ words = lines.flatMap(lambda line: re.findall(r"\b\w+\b", line))
11
+ for word, length in words.map(lambda w: (w, len(w))).collect():
12
+ print(word, length)
13
+
14
+ sc.stop()
@@ -0,0 +1,22 @@
1
+ import sys
2
+ from pyspark import SparkContext
3
+
4
+ sc = SparkContext(appName="Weather")
5
+ sc.setLogLevel("ERROR")
6
+ path, year = sys.argv[1:3]
7
+ rows = sc.textFile(path).filter(
8
+ lambda s: len(s) >= 93 and s[15:19] == year
9
+ and s[92] in "01459" and s[87:92] != "+9999")
10
+ data = rows.map(lambda s: (int(s[87:92]), s[4:10] + "-" + s[10:15]))
11
+ data.cache()
12
+
13
+ if data.isEmpty():
14
+ print("No valid observations for", year)
15
+ else:
16
+ low, high = data.keys().min(), data.keys().max()
17
+ for label, temp in [("Minimum", low), ("Maximum", high)]:
18
+ stations = data.filter(lambda x: x[0] == temp)
19
+ ids = sorted(stations.values().distinct().collect())
20
+ print(year, label, temp / 10, "C", ", ".join(ids))
21
+
22
+ sc.stop()