WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Python · Expert · question 77 of 100

How do you use Python to work with big data and distributed computing frameworks, such as Hadoop, Spark, and Dask?

📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

Python is a popular language for working with big data and distributed computing frameworks, thanks to its ease of use, rich library ecosystem, and support for parallel processing. In this answer, we’ll explore some common techniques for working with these frameworks in Python.

Working with Hadoop

Hadoop is a popular open-source framework for distributed storage and processing of large data sets. Python can interact with Hadoop in a few different ways:

Using the hadoop command-line tool: You can use the subprocess module in Python to execute hadoop commands and retrieve the output. For example, to list the files in a Hadoop directory:

    import subprocess
    
    cmd = ['hadoop', 'fs', '-ls', '/path/to/directory']
    output = subprocess.check_output(cmd).decode()
    
    print(output)

Using the hdfs module: The hdfs module provides a Pythonic interface to the Hadoop Distributed File System (HDFS). You can use it to read and write files, as well as to navigate the file system. For example, to read a file from HDFS:

    from hdfs import InsecureClient
    
    client = InsecureClient('http://localhost:50070')
    with client.read('/path/to/file.txt', encoding='utf-8') as reader:
        contents = reader.read()
    
    print(contents)

Using the pydoop module: The pydoop module provides a Pythonic interface to Hadoop’s MapReduce framework. You can use it to write MapReduce jobs in Python. For example, here’s a simple MapReduce job that counts the occurrences of each word in a text file:

    import pydoop.mapreduce.api as api
    from collections import Counter
    
    class Mapper(api.Mapper):
    
        def map(self, context):
            for word in context.value.split():
            context.emit(word, 1)
        
    class Reducer(api.Reducer):
        
        def reduce(self, context):
            counts = Counter(context.values)
            context.emit(context.key, sum(counts.values()))
    
    if __name__ == '__main__':
    job = api.Job(
        mapper_class=Mapper,
        reducer_class=Reducer,
        input_path='/path/to/input',
        output_path='/path/to/output'
    )
    job.wait()

Working with Spark

Apache Spark is a popular distributed computing framework that provides a fast and flexible way to process large data sets. Python can be used with Spark in several ways:

Using PySpark: PySpark is the Python API for Spark. It allows you to write Spark programs in Python, and provides a Pythonic interface to Spark’s distributed data structures (such as RDDs and DataFrames). Here’s an example of using PySpark to read a CSV file and count the number of rows:

    from pyspark.sql import SparkSession
    
    spark = SparkSession.builder.appName('example').getOrCreate()
    df = spark.read.csv('/path/to/file.csv', header=True, inferSchema=True)
    count = df.count()
    
    print(f'Number of rows: {count}')

Using Spark SQL: Spark SQL allows you to run SQL queries on your Spark data. You can use it to create temporary views of your data, and then query those views using SQL. Here’s an example of using Spark SQL to query a CSV file:

    from pyspark.sql import SparkSession
    
    spark = SparkSession.builder.appName('example').getOrCreate()
    df = spark.read.csv('/path/to/file.csv', header=True, inferSchema=True
Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Python interview — then scores it.
📞 Practice Python — free 15 min
📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

All 100 Python questions · All topics