I have used this code to give the wordcount for only a single file , it outputs the…
- User
I have used this code to give the wordcount for only a single file , it outputs the first most occuring 25 words in a single file output4-------------------------------------------import string import sys from pyspark import SparkContext
def remove_punctuation(text): return text.translate(str.maketrans(‘’, ‘’, string.punctuation))
def word_count(sc, input_file):
Load the text file
text_file = sc.textFile(input_file)
Split the lines into words and clean them
words = text_file.flatMap(lambda line: remove_punctuation(line).lower().split())
Remove stop words
stopwords = set([ ‘we’, ‘he’, ‘she’, ‘it’, ‘me’, ‘him’, ‘her’, ‘us’, ‘them’, ‘my’, ‘your’, ‘our’, ‘his’, ‘her’, ‘its’, ‘their’, ‘yours’, ‘ours’, ‘theirs’, ‘just’, ‘now’, ‘get’, ‘got’, ‘ill’, ‘ive’, ‘go’, ‘im’, ‘us’, ‘dont’, ‘like’, ‘got’, ‘really’, ‘see’, ‘thats’, ‘know’, ‘yeah’, ‘oh’, ‘ive’, ‘had’, ‘theyre’, ‘would’, ‘could’, ‘should’, ‘might’, ‘must’, ‘shall’, ‘will’, ‘yet’, ‘mr’, ‘mrs’, ‘ms’, ‘miss’, ‘dr’, ‘prof’, ‘a’, ‘an’, ‘the’, ‘and’, ‘in’, ‘of’, ‘to’, ‘if’, ‘are’, ‘that’, ‘is’, ‘was’,‘for’, ‘with’, ‘as’, ‘on’, ‘at’, ‘by’, ‘be’, ‘it’, ‘this’, ‘which’, ‘or’, ‘from’, ‘not’, ‘but’, ‘also’, ‘his’, ‘her’, ‘their’, ‘they’, ‘i’, ‘you’, ])
words = words.filter(lambda word: word not in stopwords)
Map each word to a tuple of (word, 1), then reduce by key to count the occurrences of each word
word_counts = words.map(lambda word: (word, 1)).reduceByKey(lambda x, y: x + y)
Swap key and value to sort by count, then sort by descending count
count_word_pairs = word_counts.map(lambda x: (x[1], x[0])) sorted_word_counts = count_word_pairs.sortByKey(False)
Swap back key and value
word_count_pairs = sorted_word_counts.map(lambda x: (x[1], x[0]))
Save the results to a text file
return word_count_pairs
if name == ‘main’: from pyspark.context import SparkContext sc = SparkContext(‘local’, ‘test’) word_count(sc, ‘file1.txt’).saveAsTextFile(“output4”) sc.stop()-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Now I need the code that does the same for 10 input text files and from that it should give the first 25 most occuring words as a single output file
- Assistant
You can modify the existing code to process multiple files at once by using
wholeTextFiles()method instead oftextFile(). This method reads all files from a specified directory and returns a pair RDD with pairs (file_path, content).Here is the updated code to process 10 text files and compute the 25 most occurring words as a single output file:
python - User
import string import sys from pyspark import SparkContext
def remove_punctuation(text): return text.translate(str.maketrans('', '', string.punctuation))
def word_count(sc, input_file): # Load the text file text_file = sc.textFile(input_file)
code - Assistant
A Directed Acyclic Graph (DAG) is a graphical representation of the operations and data dependencies in a Spark program. It provides a high-level view of how your Spark application executes various operations to process and transform data. Each node in the DAG represents an RDD transformation, and edges represent the data dependencies between these operations.
DAG visualizations can help you understand the execution plan and stages of your Spark application, which can enable you to optimize performance and troubleshoot issues.
- User
- Your WordCount application should compute the same results as your WordCount application from Homework #1. Answer the following based on your knowledge of both MapReduce and Spark: a. If you were running your WordCount programs in a large cluster or cloud environment, and one of the nodes you were running on died mid computation, how would your MapReduce and Spark programs handle this? b. Explain one concrete benefit you experienced when writing the Spark version of WordCount compared to the MapReduce version.
- Assistant
a. If one of the nodes died mid-computation in a large cluster or cloud environment, MapReduce and Spark would handle the situation differently:
-
MapReduce: In a MapReduce framework like Hadoop, the job tracker detects the node failure and reassigns the failed task to another available node in the cluster. Since the intermediate results are written to the distributed file system (e.g., HDFS) and the framework takes multiple replicas of data and intermediate results, the newly assigned node would pick up the failed task, read the input data chunks from HDFS, and proceed with the computation. This process is called fault tolerance in MapReduce.
-
- User
Part 2 - Code Analysis [16/30 points] Read through the following PySpark code snippet and answer the questions following: lines = sc.textFile(file) links = lines.map(lambda urls: parseNeighbors(urls))
.groupByKey() .cache() N = links.count() ranks = links.map(lambda u: (u[0], 1.0/N)) for i in range(iters): contribs = links.join(ranks)
.flatMap(lambda u: computeContribs(u[1][0], u[1][1])) ranks = contribs.reduceByKey(lambda a,b: a+b)
.mapValues(lambda rank: rank * 0.85 + 0.15*(1.0/N)) return ranks ---------Answer the following ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------4. Given the above spark application, draw the lineage graph DAG for the RDD ranks on line 12 when the iteration variable i has a value of 2. Include nodes for all intermediate RDDs, even if they are unnamed. 5. How many stages will the above DAG be broken into? Give the number of stages AND draw stage boundaries on your diagram. 6. Identify in the above code (by function name AND line number) one instance of: a. A transformation that results in a wide dependency b. A transformation that results in a narrow dependency c. A transformation that may result in a narrow dependency OR a wide dependency d. An action 7. How many "jobs" will the above code run if iters has value 10? 8. What algorithm is the above code an implementation of? - Assistant
- Here's the lineage graph DAG representation for the
ranksRDD on line 12 when the iteration variableihas a value of 2:
codetextFile -> map (parseNeighbors) -> groupByKey -> join ^ \ 1.0/N -> map (initialize ranks) -> reduceByKey -> mapValues (update ranks) / v flatMap (computeContribs) -> cache - Here's the lineage graph DAG representation for the
- User
def normalize_database(non_normalized_db_filename):
Normalize 'non_normalized.db'
Call the normalized database 'normalized.db'
Function Output: No outputs
Requirements:
Create four tables
Degrees table has one column:
[Degree] column is the primary key
Exams table has two columns:
[Exam] column is the primary key column
[Year] column stores the exam year
Students table has four columns:
[StudentID] primary key column
[First_Name] stores first name
[Last_Name] stores last name
[Degree] foreign key to Degrees table
StudentExamScores table has four columns:
[PK] primary key column,
[StudentID] foreign key to Students table,
[Exam] foreign key to Exams table ,
[Score] exam score
- Assistant
To create a normalized SQLite database from a non-normalized one, you can follow these steps. Note that some assumptions are made about the structure of the non-normalized database.