You are on page 1of 68

Data Ingestion with Python Cookbook:

A practical guide to ingesting,


monitoring, and identifying errors in the
data ingestion process 1st Edition
Esppenchutz
Visit to download the full and correct content document:
https://ebookmass.com/product/data-ingestion-with-python-cookbook-a-practical-guid
e-to-ingesting-monitoring-and-identifying-errors-in-the-data-ingestion-process-1st-editi
on-esppenchutz/
Data Ingestion with Python
Cookbook

A practical guide to ingesting, monitoring, and identifying


errors in the data ingestion process

Gláucia Esppenchutz

BIRMINGHAM—MUMBAI
Data Ingestion with Python Cookbook
Copyright © 2023 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted
in any form or by any means, without the prior written permission of the publisher, except in the case
of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information
presented. However, the information contained in this book is sold without warranty, either express
or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable
for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and
products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot
guarantee the accuracy of this information.

Group Product Manager: Reshma Raman


Publishing Product Manager: Arindam Majumdar
Senior Editor: Tiksha Lad
Technical Editor: Devanshi Ayare
Copy Editor: Safis Editing
Project Coordinator: Farheen Fathima
Proofreader: Safis Editing
Indexer: Sejal Dsilva
Production Designer: Jyoti Chauhan
Marketing Coordinator: Nivedita Singh

First published: May 2023

Production reference: 1300523

Published by Packt Publishing Ltd.


Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.

ISBN 978-1-83763-260-2

www.packtpub.com
This book represents a lot and wouldn’t be possible without my loving husband, Lincoln, and his
support and understanding during this challenging endeavor. I want to thank all my friends that
didn’t let me give up and always boosted my spirits, along with my grandmother, who always believed,
helped, and said I would do big things one day. Finally, I want to thank my beloved and four-pawed
best friend, who is at peace, Minduim, for “helping” me to write this book.

– Gláucia Esppenchutz
Contributors

About the author


Gláucia Esppenchutz is a data engineer with expertise in managing data pipelines and vast amounts
of data using cloud and on-premises technologies. She worked in companies such as Globo.com,
BMW Group, and Cloudera. Currently, she works at AiFi, specializing in the field of data operations
for autonomous systems.
She comes from the biomedical field and shifted her career ten years ago to chase the dream of
working closely with technology and data. She is in constant contact with the open source community,
mentoring people and helping to manage projects, and has collaborated with the Apache, PyLadies
group, FreeCodeCamp, Udacity, and MentorColor communities.

I want to thank my patient and beloved husband and my friends. Thanks also to my mentors in the
Python open source community and the DataBootCamp founders, who guided me at the beginning
of my journey.
Thanks to the Packt team, who helped me through some hard times; you were terrific!
About the reviewers
Bitthal Khaitan is currently working as a big data and cloud engineer with CVS Health, a Fortune
4 organization. He has a demonstrated history of working in the cloud, data and analytics industry
for 12+ years. His primary certified skills are Google Cloud Platform (GCP), the big data ecosystem
(Hadoop, Spark, etc.), and data warehousing on Teradata. He has worked in all phases of the SDLC
of DW/BI and big data projects with strong expertise in the USA healthcare, insurance and retail
domains. He actively helps new graduates with mentoring, resume reviews, and job hunting tips in
the data engineering domain. Over 20,000 people follow Bitthal on LinkedIn. He is currently based
out of Dallas, Texas, USA.
Jagjeet Makhija is a highly accomplished technology leader with over 20 years of experience. They are
skilled not only in various domains including AI, data warehouse architecture, and business analytics,
but also have a strong passion for staying ahead of technology trends such as AI and ChatGPT.
Jagjeet is recognized for their significant contributions to the industry, particularly in complex proof
of concepts and integrating Microsoft products with ChatGPT. They are also an avid book reviewer
and have actively shared their extensive knowledge and expertise through presentations, blog articles,
and online forums.
Krishnan Raghavan is an IT professional with over 20 years of experience in the area of software
development and delivery excellence across multiple domains and technology, ranging from C++ to
Java, Python, data warehousing, and big data tools and technologies. Krishnan tries to give back to the
community by being part of GDG – Pune Volunteer Group, helping the team in organizing events.
When not working, Krishnan likes to spend time with his wife and daughter, as well as reading fiction,
non-fiction, and technical books. Currently, he is unsuccessfully trying to learn how to play the guitar.
You can connect with Krishnan at mail to: k r i s h n a n @ g m a i l . c o m or via
LinkedIn: www.linkedin.com/in/krishnan-raghavan

I would like to thank my wife, Anita, and daughter, Ananya, for giving me the time and space to
review this book.
Table of Contents

Prefacexv

Part 1: Fundamentals of Data Ingestion


1
Introduction to Data Ingestion 3
Technical requirements 4 How to do it… 17
Setting up Python and its environment 4 How it works… 21
See also 22
Getting ready 4
How to do it… 5 Creating schemas 22
How it works… 7 Getting ready 22
There’s more… 7 How to do it… 22
See also 7 How it works… 24
Installing PySpark 8 See also 25

Getting ready 8 Applying data governance in ingestion 25


How to do it… 10 Getting ready 25
How it works… 12 How to do it… 26
There’s more… 12 How it works… 28
See also 12 See also 28
Configuring Docker for MongoDB 13 Implementing data replication 29
Getting ready 13 Getting ready 29
How to do it… 13 How to do it… 29
How it works… 14 How it works… 29
There’s more… 16 There’s more… 30
See also 16
Further reading 30
Configuring Docker for Airflow 16
Getting ready 17
viii Table of Contents

2
Principals of Data Access – Accessing Your Data 31
Technical requirements 31 How to do it… 47
Implementing governance in a data How it works… 48
access workflow 32 There’s more… 49
Getting ready 32 See also 52
How to do it… 33 Managing encrypted files 52
How it works… 34 Getting ready 52
See also 34 How to do it… 53
Accessing databases and data How it works… 54
warehouses34 There’s more… 55
Getting ready 35 See also 56
How to do it… 35 Accessing data from AWS using S3 56
How it works… 37 Getting ready 56
There’s more… 38 How to do it… 59
See also 39 How it works… 62
Accessing SSH File Transfer Protocol There’s more… 63
(SFTP) files 39 See also 63
Getting ready 39 Accessing data from GCP using
How to do it… 41 Cloud Storage 64
How it works… 43 Getting ready 64
There’s more… 43 How to do it… 66
See also 44 How it works… 68
Retrieving data using API There’s more… 70
authentication44 Further reading 70
Getting ready 45

3
Data Discovery – Understanding Our Data before Ingesting It 71
Technical requirements 71 How to do it… 73
Documenting the data discovery How it works… 77
process71 Configuring OpenMetadata 77
Getting ready 72 Getting ready 77
Table of Contents ix

How to do it… 79 Getting ready 86


How it works… 84 How to do it… 88
There’s more… 86 How it works… 92
See also 86
Further reading 93
Connecting OpenMetadata Other tools 94
to our database 86

4
Reading CSV and JSON Files and Solving Problems 95
Technical requirements 95 How it works… 105
Reading a CSV file 96 There’s more… 106
See also 107
Getting ready 96
How to do it… 96 Using PySpark to read CSV files 108
How it works… 98 Getting ready 108
There’s more… 98 How to do it… 108
See also 99 How it works… 109
Reading a JSON file 99 There’s more… 110
See also 114
Getting ready 100
How to do it… 100 Using PySpark to read JSON files 114
How it works… 100 Getting ready 114
There’s more… 101 How to do it… 115
See also 103 How it works… 116
Creating a SparkSession for PySpark 103 There’s more… 117
See also 117
Getting ready 103
How to do it… 104 Further reading 117

5
Ingesting Data from Structured and Unstructured Databases 119
Technical requirements 119 There’s more… 127
Configuring a JDBC connection 120 See also 129

Getting ready 120 Ingesting data from a JDBC database


How to do it… 124 using SQL 129
How it works… 126 Getting ready 129
x Table of Contents

How to do it… 130 Getting ready 146


How it works… 132 How to do it… 146
There’s more… 133 How it works… 148
See also 133 There’s more… 149
See also 150
Connecting to a NoSQL
database (MongoDB) 134 Ingesting data from MongoDB
Getting ready 134 using PySpark 150
How to do it… 136 Getting ready 150
How it works… 142 How to do it… 151
There’s more… 144 How it works… 153
See also 145 There’s more… 155
See also 156
Creating our NoSQL table in
MongoDB145 Further reading 157

6
Using PySpark with Defined and Non-Defined Schemas 159
Technical requirements 159 How to do it… 169
Applying schemas to data ingestion 160 How it works… 170

Getting ready 160 Ingesting unstructured data with a


How to do it… 162 well-defined schema and format 172
How it works… 163 Getting ready 172
There’s more… 165 How to do it… 172
See also 165 How it works… 174
Importing structured data using a There’s more… 176
well-defined schema 165 See also 176

Getting ready 165 Inserting formatted SparkSession


How to do it… 165 logs to facilitate your work 176
How it works… 167 Getting ready 176
There’s more… 168 How to do it… 176
See also 169 How it works… 178
Importing unstructured data without There’s more… 179
a schema 169 See also 179

Getting ready… 169 Further reading 179


Table of Contents xi

7
Ingesting Analytical Data 181
Technical requirements 181 How it works… 197
Ingesting Parquet files 182 There’s more… 198
See also 200
Getting ready 182
How to do it… 183 Ingesting partitioned data 200
How it works… 184 Getting ready 200
There’s more… 185 How to do it… 201
See also 185 How it works… 201
Ingesting Avro files 185 There’s more… 203
See also 204
Getting ready 186
How to do it… 186 Applying reverse ETL 204
How it works… 188 Getting ready 204
There’s more… 190 How to do it… 205
See also 190 How it works… 206
Applying schemas to analytical data 191 There’s more… 207
See also 207
Getting ready 191
How to do it… 191 Selecting analytical data for reverse
How it works… 194 ETL207
There’s more… 194 Getting ready 207
See also 195 How to do it… 208
Filtering data and handling common How it works… 209
issues195 See also 210

Getting ready 195 Further reading 210


How to do it… 196

Part 2: Structuring the Ingestion Pipeline


8
Designing Monitored Data Workflows 213
Technical requirements 213 Getting ready 214
Inserting logs 214 How to do it… 214
How it works… 216
xii Table of Contents

See also 217 How it works… 229


There’s more… 229
Using log-level types 217
See also 230
Getting ready 217
How to do it… 217 Logging based on data 231
How it works… 219 Getting ready 231
There’s more… 220 How to do it… 231
See also 221 How it works… 232
There’s more… 233
Creating standardized logs 221
See also 233
Getting ready 222
How to do it… 222 Retrieving SparkSession metrics 234
How it works… 224 Getting ready 234
There’s more… 227 How to do it… 234
See also 227 How it works… 237
There’s more… 241
Monitoring our data ingest file size 227
See also 242
Getting ready 228
How to do it… 228 Further reading 242

9
Putting Everything Together with Airflow 243
Technical requirements 244 How to do it… 257
Installing Airflow 244 How it works… 260
There's more… 262
Configuring Airflow 244 See also 262
Getting ready 244
How to do it… 245 Configuring sensors 262
How it works… 247 Getting ready 262
See also 248 How to do it… 263
How it works… 264
Creating DAGs 248 See also 265
Getting ready 248
How to do it… 249 Creating connectors in Airflow 265
How it works… 253 Getting ready 266
There's more… 254 How to do it… 266
See also 255 How it works… 269
There's more… 270
Creating custom operators 255 See also 270
Getting ready 255
Table of Contents xiii

Creating parallel ingest tasks 270 Getting ready 274


Getting ready 270 How to do it… 275
How to do it… 271 How it works… 277
How it works… 272 There's more… 278
There's more… 273 See also 279
See also 274
Further reading 279
Defining ingest-dependent DAGs 274

10
Logging and Monitoring Your Data Ingest in Airflow 281
Technical requirements 281 Designing advanced monitoring 304
Installing and running Airflow 282 Getting ready 304
How to do it… 306
Creating basic logs in Airflow 283
How it works… 308
Getting ready 284
There’s more… 309
How to do it… 284
See also 309
How it works… 287
See also 289 Using notification operators 309
Getting ready 310
Storing log files in a remote location 289
How to do it… 312
Getting ready 289
How it works… 315
How to do it… 290
There’s more… 318
How it works… 298
See also 299 Using SQL operators for data quality 318
Getting ready 318
Configuring logs in airflow.cfg 299
How to do it… 320
Getting ready 299
How it works… 321
How to do it… 299
There’s more… 323
How it works… 301
See also 323
There’s more… 303
See also 304 Further reading 324

11
Automating Your Data Ingestion Pipelines 325
Technical requirements 325 Scheduling daily ingestions 326
Installing and running Airflow 326 Getting ready 327
xiv Table of Contents

How to do it… 327 There's more… 340


How it works… 330
Setting up the schedule_interval
There's more… 330
parameter340
See also 331
Getting ready 340
Scheduling historical data ingestion 331 How to do it… 341
Getting ready 331 How it works… 342
How to do it… 332 See also 342
How it works… 335
Solving scheduling errors 343
There's more… 336
Getting ready 343
Scheduling data replication 337 How to do it… 343
Getting ready 337 How it works… 346
How to do it… 338 There’s more… 347
How it works… 339
Further reading 347

12
Using Data Observability for Debugging, Error Handling,
and Preventing Downtime 349
Technical requirements 349 Getting ready 358
Docker images 350 How to do it… 358
How it works… 361
Setting up StatsD for monitoring 351 There’s more… 363
Getting ready 351
How to do it… 351 Creating an observability dashboard 363
How it works… 353 Getting ready 363
See also 354 How to do it… 363
How it works… 369
Setting up Prometheus for storing There’s more… 370
metrics354
Getting ready 354 Setting custom alerts or notifications 370
How to do it… 354 Getting ready 371
How it works… 356 How to do it… 371
There’s more… 357 How it works… 377

Setting up Grafana for monitoring 358 Further reading 378

Index379

Other Books You May Enjoy 388


Preface
Welcome to Data Ingestion with Python Cookbook. I hope you are excited as me to enter the world
of data engineering.
Data Ingestion with Python Cookbook is a practical guide that will empower you to design and implement
efficient data ingestion pipelines. With real-world examples and renowned open-source tools, this
book addresses your queries and hurdles head-on.
Beginning with designing pipelines, you’ll explore working with and without data schemas, constructing
monitored workflows using Airflow, and embracing data observability principles while adhering
to best practices. Tackling the challenges of reading diverse data sources and formats, you’ll gain a
comprehensive understanding of all these.
Our journey continues with essential insights into error logging, identification, resolution, data
orchestration, and effective monitoring. You’ll discover optimal approaches for storing logs, ensuring
easy access and references for them in the future.
By the end of this book, you’ll possess a fully automated setup to initiate data ingestion and pipeline
monitoring. This streamlined process will seamlessly integrate into the subsequent stages of the Extract,
Transform, and Load (ETL) process, propelling your data integration capabilities to new heights. Get
ready to embark on an enlightening and transformative data ingestion journey.

Who this book is for


This comprehensive book is specifically designed for Data Engineers, Data Integration Specialists, and
passionate data enthusiasts seeking a deeper understanding of data ingestion processes, data flows,
and the typical challenges encountered along the way. It provides valuable insights, best practices, and
practical knowledge to enhance your skills and proficiency in handling data ingestion tasks effectively.
Whether you are a beginner in the data world or an experienced developer, this book will suit you.
It is recommended to know the Python programming fundamentals and have basic knowledge of
Docker to read and run this book’s code.

What this book covers


Chapter 1, Introduction to Data Ingestion, introduces you to data ingestion best practices and the
challenges of working with diverse data sources. It explains the importance of the tools covered in
the book, presents them, and provides installation instructions.
xvi Preface

Chapter 2, Data Access Principals – Accessing your Data, explores data access concepts related to data
governance, covering workflows and management of familiar sources such as SFTP servers, APIs,
and cloud providers. It also provides examples of creating data access policies in databases, data
warehouses, and the cloud.
Chapter 3, Data Discovery – Understanding Our Data Before Ingesting It, teaches you the significance of
carrying out the data discovery process before data ingestion. It covers manual discovery, documentation,
and using an open-source tool, OpenMetadata, for local configuration.
Chapter 4, Reading CSV and JSON Files and Solving Problems, introduces you to ingesting CSV and JSON
files using Python and PySpark. It demonstrates handling varying data volumes and infrastructures
while addressing common challenges and providing solutions.
Chapter 5, Ingesting Data from Structured and Unstructured Databases, covers fundamental concepts
of relational and non-relational databases, including everyday use cases. You will learn how to read
and handle data from these models, understand vital considerations, and troubleshoot potential errors.
Chapter 6, Using PySpark with Defined and Non-Defined Schemas, delves deeper into common PySpark
use cases, focusing on handling defined and non-defined schemas. It also explores reading and
understanding complex logs from Spark (PySpark core) and formatting techniques for easier debugging.
Chapter 7, Ingesting Analytical Data, introduces you to analytical data and common formats for reading
and writing. It explores reading partitioned data for improved performance and discusses Reverse
ETL theory with real-life application workflows and diagrams.
Chapter 8, Designing Monitored Data Workflows, covers logging best practices for data ingestion,
facilitating error identification, and debugging. Techniques such as monitoring file size, row count,
and object count enable improved monitoring of dashboards, alerts, and insights.
Chapter 9, Putting Everything Together with Airflow, consolidates the previously presented information
and guides you in building a real-life data ingestion application using Airflow. It covers essential
components, configuration, and issue resolution in the process.
Chapter 10, Logging and Monitoring Your Data Ingest in Airflow, explores advanced logging and
monitoring in data ingestion with Airflow. It covers creating custom operators, setting up notifications,
and monitoring for data anomalies. Configuration of notifications for tools such as Slack is also covered
to stay updated on the data ingestion process.
Chapter 11, Automating Your Data Ingestion Pipelines, focuses on automating data ingests using
previously learned best practices, enabling reader autonomy. It addresses common challenges with
schedulers or orchestration tools and provides solutions to avoid problems in production clusters.
Chapter 12, Using Data Observability for Debugging, Error Handling, and Preventing Downtime,
explores data observability concepts, popular monitoring tools such as Grafana, and best practices
for log storage and data lineage. It also covers creating visualization graphs to monitor data source
issues using Airflow configuration and data ingestion scripts.
Preface xvii

To get the most out of this book


To execute the code in this book, you must have at least a basic knowledge of Python. We will use
Python as the core language to execute the code. The code examples have been tested using Python
3.8. However, it is expected to still work with future language versions.
Along with Python, this book uses Docker to emulate data systems and applications in our local
machine, such as PostgreSQL, MongoDB, and Airflow. Therefore, a basic knowledge of Docker is
recommended to edit container image files and run and stop containers.
Please, remember that some command-line commands may need adjustments depending on your local
settings or operating system. The commands in the code examples are based on the Linux command-
line syntax and might need some adaptations to run on Windows PowerShell.

Software/Hardware covered in the book OS Requirements

Python 3.8 or higher Windows, Mac OS X, and Linux (any)


Docker Engine 24.0 / Docker Desktop 4.19 Windows, Mac OS X, and Linux (any)

For almost all recipes in this book, you can use a Jupyter Notebook to execute the code. Even though it
is not mandatory to install it, this tool can help you to test the code and try new things on the code due
to the friendly interface.
If you are using the digital version of this book, we advise you to type the code yourself or access
the code via the GitHub repository (link available in the next section). Doing so will help you
avoid any potential errors related to the copying and pasting of code.

Download the example code files


You can download the example code files for this book from GitHub at https://github.com/
PacktPublishing/Data-Ingestion-with-Python-Cookbook. In case there’s an update
to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://
github.com/PacktPublishing/. Check them out!

Download the color images


We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You
can download it here: https://packt.link/xwl0U
xviii Preface

Conventions used
There are a number of text conventions used throughout this book.
Code in text: Indicates code words in text, database table names, folder names, filenames, file
extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: “Then
we proceeded with the with open statement.”
A block of code is set as follows:

def gets_csv_first_line (csv_file):


    logging.info(f"Starting function to read first line")
    try:
        with open(csv_file, 'r') as file:
            logging.info(f"Reading file")

Any command-line input or output is written as follows:

$ python3 –-version
Python 3.8.10

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words
in menus or dialog boxes appear in the text like this. Here is an example: “Then, when we selected
showString at NativeMethodAccessorImpl.java:0, which redirected us to the
Stages page.”

Tips or important notes


Appear like this.

Sections
In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How
it works..., There’s more..., and See also).
To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready
This section tells you what to expect in the recipe and describes how to set up any software or any
preliminary settings required for the recipe.

How to do it…
This section contains the steps required to follow the recipe.
Preface xix

How it works…
This section usually consists of a detailed explanation of what happened in the previous section.

There’s more…
This section consists of additional information about the recipe in order to make you more knowledgeable
about the recipe.

See also
This section provides helpful links to other useful information for the recipe.

Get in touch
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, mention the book title in the
subject of your message and email us at customercare@packtpub.com.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen.
If you have found a mistake in this book, we would be grateful if you would report this to us. Please
visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata
Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would
be grateful if you would provide us with the location address or website name. Please contact us at
copyright@packt.com with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you
are interested in either writing or contributing to a book, please visit authors.packtpub.com.
xx Preface

Share Your Thoughts


Once you’ve read Data Ingestion with Python Cookbook, we’d love to hear your thoughts! Please
click here to go straight to the Amazon review page for this book and share
your feedback.
Your review is important to us and the tech community and will help us make sure we’re delivering
excellent quality content.
Preface xxi

Download a free PDF copy of this book


Thanks for purchasing this book!
Do you like to read on the go but are unable to carry your print books everywhere?
Is your eBook purchase not compatible with the device of your choice?
Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.
Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical
books directly into your application.
The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content
in your inbox daily
Follow these simple steps to get the benefits:

1. Scan the QR code or visit the link below

https://packt.link/free-ebook/9781837632602

2. Submit your proof of purchase


3. That’s it! We’ll send your free PDF and other benefits to your email directly
Part 1:
Fundamentals
of Data Ingestion

In this part, you will be introduced to the fundamentals of data ingestion and data engineering,
passing through the basic definition of an ingestion pipeline, the common types of data sources, and
the technologies involved.
This part has the following chapters:

• Chapter 1, Introduction to Data Ingestion


• Chapter 2, Principals of Data Access – Accessing Your Data
• Chapter 3, Data Discovery – Understanding Our Data Before Ingesting It
• Chapter 4, Reading CSV and JSON Files and Solving Problems
• Chapter 5, Ingesting Data from Structured and Unstructured Databases
• Chapter 6, Using PySpark with Defined and Non-Defined Schemas
• Chapter 7, Ingesting Analytical Data
1
Introduction to Data Ingestion
Welcome to the fantastic world of data! Are you ready to embark on a thrilling journey into data
ingestion? If so, this is the perfect book to start! Ingesting data is the first step into the big data world.
Data ingestion is a process that involves gathering and importing data and also storing it properly
so that the subsequent extract, transform, and load (ETL) pipeline can utilize the data. To make it
happen, we must be cautious about the tools we will use and how to configure them properly.
In our book journey, we will use Python and PySpark to retrieve data from different data sources
and learn how to store them properly. To orchestrate all this, the basic concepts of Airflow will be
implemented, along with efficient monitoring to guarantee that our pipelines are covered.
This chapter will introduce some basic concepts about data ingestion and how to set up your
environment to start the tasks.
In this chapter, you will build and learn the following recipes:

• Setting up Python and the environment


• Installing PySpark
• Configuring Docker for MongoDB
• Configuring Docker for Airflow
• Logging libraries
• Creating schemas
• Applying data governance in ingestion
• Implementing data replication
4 Introduction to Data Ingestion

Technical requirements
The commands inside the recipes of this chapter use Linux syntax. If you don’t use a Linux-based
system, you may need to adapt the commands:

• Docker or Docker Desktop


• The SQL client of your choice (recommended); we recommend DBeaver, since it has a
community-free version

You can find the code from this chapter in this GitHub repository: https://github.com/
PacktPublishing/Data-Ingestion-with-Python-Cookbook.

Note
Windows users might get an error message such as Docker Desktop requires a newer WSL
kernel version. This can be fixed by following the steps here: https://docs.docker.
com/desktop/windows/wsl/.

Setting up Python and its environment


In the data world, languages such as Java, Scala, or Python are commonly used. The first two languages
are used due to their compatibility with the big data tools environment, such as Hadoop and Spark,
the central core of which runs on a Java Virtual Machine (JVM). However, in the past few years, the
use of Python for data engineering and data science has increased significantly due to the language’s
versatility, ease of understanding, and many open source libraries built by the community.

Getting ready
Let’s create a folder for our project:

1. First, open your system command line. Since I use the Windows Subsystem for Linux (WSL),
I will open the WSL application.
2. Go to your home directory and create a folder as follows:
$ mkdir my-project

3. Go inside this folder:


$ cd my-project

4. Check your Python version on your operating system as follows:


$ python -–version
Setting up Python and its environment 5

Depending on your operational system, you might or might not have output here – for example,
WSL 20.04 users might have the following output:
Command 'python' not found, did you mean:
command 'python3' from deb python3
command 'python' from deb python-is-python3

If your Python path is configured to use the python command, you will see output similar
to this:
Python 3.9.0

Sometimes, your Python path might be configured to be invoked using python3. You can
try it using the following command:
$ python3 --version

The output will be similar to the python command, as follows:


Python 3.9.0

5. Now, let’s check our pip version. This check is essential, since some operating systems have
more than one Python version installed:
$ pip --version

You should see similar output:


pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.9)

If your operating system (OS) uses a Python version below 3.8x or doesn’t have the language
installed, proceed to the How to do it steps; otherwise, you are ready to start the following Installing
PySpark recipe.

How to do it…
We are going to use the official installer from Python.org. You can find the link for it here: https://
www.python.org/downloads/:

Note
For Windows users, it is important to check your OS version, since Python 3.10 may not be
yet compatible with Windows 7, or your processor type (32-bits or 64-bits).

1. Download one of the stable versions.


At the time of writing, the stable recommended versions compatible with the tools and resources
presented here are 3.8, 3.9, and 3.10. I will use the 3.9 version and download it using the
following link: https://www.python.org/downloads/release/python-390/.
Scrolling down the page, you will find a list of links to Python installers according to OS, as shown
in the following screenshot.
6 Introduction to Data Ingestion

Figure 1.1 – Python.org download files for version 3.9

2. After downloading the installation file, double-click it and follow the instructions in the wizard
window. To avoid complexity, choose the recommended settings displayed.
The following screenshot shows how it looks on Windows:

Figure 1.2 – The Python Installer for Windows


Setting up Python and its environment 7

3. If you are a Linux user, you can install it from the source using the following commands:
$ wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tgz

$ tar -xf Python-3.9.1.tgz

$ ./configure –enable-optimizations

$ make -j 9

After installing Python, you should be able to execute the pip command. If not, refer to the pip
official documentation page here: https://pip.pypa.io/en/stable/installation/.

How it works…
Python is an interpreted language, and its interpreter extends several functions made with C or
C++. The language package also comes with several built-in libraries and, of course, the interpreter.
The interpreter works like a Unix shell and can be found in the usr/local/bin directory: https://
docs.python.org/3/tutorial/interpreter.html.
Lastly, note that many Python third-party packages in this book require the pip command to be
installed. This is because pip (an acronym for Pip Installs Packages) is the default package manager
for Python; therefore, it is used to install, upgrade, and manage the Python packages and dependencies
from the Python Package Index (PyPI).

There’s more…
Even if you don’t have any Python versions on your machine, you can still install them using the
command line or HomeBrew (for macOS users). Windows users can also download them from the
MS Windows Store.

Note
If you choose to download Python from the Windows Store, ensure you use an application
made by the Python Software Foundation.

See also
You can use pip to install convenient third-party applications, such as Jupyter. This is an open source,
web-based, interactive (and user-friendly) computing platform, often used by data scientists and data
engineers. You can install it from the official website here: https://jupyter.org/install.
8 Introduction to Data Ingestion

Installing PySpark
To process, clean, and transform vast amounts of data, we need a tool that provides resilience and
distributed processing, and that’s why PySpark is a good fit. It gets an API over the Spark library that
lets you use its applications.

Getting ready
Before starting the PySpark installation, we need to check our Java version in our operational system:

1. Here, we check the Java version:


$ java -version

You should see output similar to this:


openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-8u292-b10-
0ubuntu1~20.04-b10)
OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)

If everything is correct, you should see the preceding message as the output of the command
and the OpenJDK 18 version or higher. However, some systems don’t have any Java version
installed by default, and to cover this, we need to proceed to step 2.
2. Now, we download the Java Development Kit (JDK).
Go to https://www.oracle.com/java/technologies/downloads/, select
your OS, and download the most recent version of JDK. At the time of writing, it is JDK 19.
The download page of the JDK will look as follows:
Installing PySpark 9

Figure 1.3 – The JDK 19 downloads official web page

Execute the downloaded application. Click on the application to start the installation process.
The following window will appear:

Note
Depending on your OS, the installation window may appear slightly different.
10 Introduction to Data Ingestion

Figure 1.4 – The Java installation wizard window

Click Next for the following two questions, and the application will start the installation.
You don’t need to worry about where the JDK will be installed. By default, the application is
configured, as standard, to be compatible with other tools’ installations.
3. Next, we again check our Java version. When executing the command again, you should see
the following version:
$ java -version
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-8u292-b10-
0ubuntu1~20.04-b10)
OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)

How to do it…
Here are the steps to perform this recipe:

1. Install PySpark from PyPi:


$ pip install pyspark
Installing PySpark 11

If the command runs successfully, the installation output’s last line will look like this:
Successfully built pyspark
Installing collected packages: py4j, pyspark
Successfully installed py4j-0.10.9.5 pyspark-3.3.2

2. Execute the pyspark command to open the interactive shell. When executing the pyspark
command in your command line, you should see this message:
$ pyspark
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more
information.
22/10/08 15:06:11 WARN Utils: Your hostname, DESKTOP-DVUDB98
resolves to a loopback address: 127.0.1.1; using 172.29.214.162
instead (on interface eth0)
22/10/08 15:06:11 WARN Utils: Set SPARK_LOCAL_IP if you need to
bind to another address
22/10/08 15:06:13 WARN NativeCodeLoader: Unable to load native-
hadoop library for your platform... using builtin-java classes
where applicable
Using Spark's default log4j profile: org/apache/spark/log4j-
defaults.properties
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For
SparkR, use setLogLevel(newLevel).
Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /__ / .__/\_,_/_/ /_/\_\   version 3.1.2
      /_/

Using Python version 3.8.10 (default, Jun 22 2022 20:18:18)


Spark context Web UI available at http://172.29.214.162:4040
Spark context available as 'sc' (master = local[*], app id =
local-1665237974112).
SparkSession available as 'spark'.
>>>

You can observe some interesting messages here, such as the Spark version and the Python
used from PySpark.
3. Finally, we exit the interactive shell as follows:
>>> exit()
$
12 Introduction to Data Ingestion

How it works…
As seen at the beginning of this recipe, Spark is a robust framework that runs on top of the JVM. It is
also an open source tool for creating resilient and distributed processing output from vast data. With
the growth in popularity of the Python language in the past few years, it became necessary to have a
solution that adapts Spark to run alongside Python.
PySpark is an interface that interacts with Spark APIs via Py4J, dynamically allowing Python code to
interact with the JVM. We first need to have Java installed on our OS to use Spark. When we install
PySpark, it already comes with Spark and Py4J components installed, making it easy to start the
application and build the code.

There’s more…
Anaconda is a convenient way to install PySpark and other data science tools. This tool encapsulates all
manual processes and has a friendly interface for interacting with and installing Python components,
such as NumPy, pandas, or Jupyter:

1. To install Anaconda, go to the official website and select Products | Anaconda


Distribution: https://www.anaconda.com/products/distribution.
2. Download the distribution according to your OS.

For more detailed information about how to install Anaconda and other powerful commands, refer
to https://docs.anaconda.com/.

Using virtualenv with PySpark

It is possible to configure and use virtualenv with PySpark, and Anaconda does it automatically
if you choose this type of installation. However, for the other installation methods, we need to make
some additional steps to make our Spark cluster (locally or on the server) run it, which includes
indicating the virtualenv /bin/ folder and where your PySpark path is.

See also
There is a nice article about this topic, Using VirtualEnv with PySpark, by jzhang, here: https://
community.cloudera.com/t5/Community-Articles/Using-VirtualEnv-with-
PySpark/ta-p/245932.
Configuring Docker for MongoDB 13

Configuring Docker for MongoDB


MongoDB is a Not Only SQL (NoSQL) document-oriented database, widely used to store Internet
of Things (IoT) data, application logs, and so on. A NoSQL database is a non-relational database that
stores unstructured data differently from relational databases such as MySQL or PostgreSQL. Don’t
worry too much about this now; we will cover it in more detail in Chapter 5.
Your cluster production environment can handle huge amounts of data and create resilient data storage.

Getting ready
Following the good practice of code organization, let’s start creating a folder inside our project to
store the Docker image:
Create a folder inside our project directory to store the MongoDB Docker image and data as follows:
my-project$ mkdir mongo-local
my-project$ cd mongo-local

How to do it…
Here are the steps to try out this recipe:

1. First, we pull the Docker image from Docker Hub as follows:


my-project/mongo-local$ docker pull mongo

You should see the following message in your command line:


Using default tag: latest
latest: Pulling from library/mongo
(...)
bc8341d9c8d5: Pull complete
(...)
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest

Note
If you are a WSL user, an error might occur if you use the WSL 1 version instead of version 2.
You can easily fix this by following the steps here: https://learn.microsoft.com/
en-us/windows/wsl/install.
14 Introduction to Data Ingestion

2. Then, we run the MongoDB server as follows:


my-project/mongo-local$ docker run \
--name mongodb-local \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME="your_username" \
-e MONGO_INITDB_ROOT_PASSWORD="your_password"\
-d mongo:latest

We then check our server. To do this, we can use the command line to see which Docker
images are running:
my-project/mongo-local$ docker ps

We then see this on the screen:

Figure 1.5 – MongoDB and Docker running container

We can even check on the Docker Desktop application to see whether our container is running:

Figure 1.6 – The Docker Desktop vision of the MongoDB container running

3. Finally, we need to stop our container. We need to use Container ID to stop the container,
which we previously saw when checking the Docker running images. We will rerun it in Chapter 5:
my-project/mongo-local$ docker stop 427cc2e5d40e

How it works…
MongoDB’s architecture uses the concept of distributed processing, where the main node interacts with
clients’ requests, such as queries and document manipulation. It distributes the requests automatically
among its shards, which are a subset of a larger data collection here.
Configuring Docker for MongoDB 15

Figure 1.7 – MongoDB architecture

Since we may also have other running projects or software applications inside our machine, isolating
any database or application server used in development is a good practice. In this way, we ensure
nothing interferes with our local servers, and the debug process can be more manageable.
This Docker image setting creates a MongoDB server locally and even allows us to make additional
changes if we want to simulate any other scenario for testing or development.
The commands we used are as follows:

• The --name command defines the name we give to our container.


• The -p command specifies the port our container will open so that we can access it
via localhost:27017.
• -e command defines the environment variables. In this case, we set the root username and
password for our MongoDB container.
• -d is detached mode – that is, the Docker process will run in the background, and we will
not see input or output. However, we can still use docker ps to check the container status.
• mongo:latest indicates Docker pulling this image’s latest version.
16 Introduction to Data Ingestion

There’s more…
For frequent users, manually configuring other parameters for the MongoDB container, such as the
version, image port, database name, and database credentials, is also possible.
A version of this image with example values is also available as a docker-compose file in the official
documentation here: https://hub.docker.com/_/mongo.
The docker-compose file for MongoDB looks similar to this:
# Use your own values for username and password
version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/

See also
You can check out MongoDB at the complete Docker Hub documentation here: https://hub.
docker.com/_/mongo.

Configuring Docker for Airflow


In this book, we will use Airflow to orchestrate data ingests and provide logs to monitor our pipelines.
Airflow can be installed directly on your local machine and any server using PyPi (https://pypi.
org/project/apache-airflow/) or a Docker container (https://hub.docker.com/r/
apache/airflow). An official and supported version of Airflow can be found on Docker Hub,
and the Apache Foundation community maintains it.
Configuring Docker for Airflow 17

However, there are some additional steps to configure our Airflow. Thankfully, the Apache Foundation
also has a docker-compose file that contains all other requirements to make Airflow work. We
just need to complete a few more steps.

Getting ready
Let’s start by initializing our Docker application on our machine. You can use the desktop version or
the CLI command.
Make sure you are inside your project folder for this. Create a folder to store Airflow internal components
and the docker-compose.yaml file:
my-project$ mkdir airflow-local
my-project$ cd airflow-local

How to do it…
1. First, we fetch the docker-compose.yaml file directly from the Airflow official docs:
my-project/airflow-local$ curl -LfO 'https://airflow.apache.org/
docs/apache-airflow/2.3.0/docker-compose.yaml'

You should see output like this:

Figure 1.8 – Airflow container image download progress

Note
Check the most stable version of this docker-compose file when you download it, since
new, more appropriate versions may be available after this book is published.

2. Next, we create the dags, logs, and plugins folders as follows:


my-project/airflow-local$ mkdir ./dags ./logs ./plugins

3. Then, we create and set the Airflow user as follows:


my-project/airflow-local$ echo -e "AIRFLOW_UID=$(id -u)\
nAIRFLOW_GID=0" > .env
18 Introduction to Data Ingestion

Note
If you have any error messages related to the AIRFLOW_UID variable, you can create a .env
file in the same folder where your docker-compose.yaml file is and define the variable
as AIRFLOW_UID=50000.

4. Then, we initialize the database:


my-project/airflow-local$ docker-compose up airflow-init

After executing the command, you should see output similar to this:
Creating network "airflow-local_default" with the default driver
Creating volume "airflow-local_postgres-db-volume" with default
driver
Pulling postgres (postgres:13)...
13: Pulling from library/postgres
(...)
Status: Downloaded newer image for postgres:13
Pulling redis (redis:latest)...
latest: Pulling from library/redis
bd159e379b3b: Already exists
(...)
Status: Downloaded newer image for redis:latest
Pulling airflow-init (apache/airflow:2.3.0)...
2.3.0: Pulling from apache/airflow
42c077c10790: Pull complete
(...)
Status: Downloaded newer image for apache/airflow:2.3.0
Creating airflow-local_postgres_1 ... done
Creating airflow-local_redis_1    ... done
Creating airflow-local_airflow-init_1 ... done
Attaching to airflow-local_airflow-init_1
(...)
airflow-init_1       | [2022-10-09 09:49:26,250] {manager.
py:213} INFO - Added user airflow
airflow-init_1       | User "airflow" created with role "Admin"
(...)
airflow-local_airflow-init_1 exited with code 0

5. Then, we start the Airflow service:


my-project/airflow-local$ docker-compose up

6. Then, we need to check the Docker processes. Using the following CLI command, you will see
the Docker images running:
my-project/airflow-local$ docker ps
Configuring Docker for Airflow 19

These are the images we see:

Figure 1.9 – The docker ps command output

In the Docker Desktop application, you can also see the same containers running but with a
more friendly interface:

Figure 1.10 – A Docker desktop view of the Airflow containers running


20 Introduction to Data Ingestion

7. Then, we access Airflow in a web browser:


In your preferred browser, type http://localhost:8080/home. The following screen
will appear:

Figure 1.11 – The Airflow UI login page

8. Then, we log in to the Airflow platform. Since it’s a local application used for testing and
learning, the default credentials (username and password) for administrative access in Airflow
are airflow.
When logged in, the following screen will appear:
Configuring Docker for Airflow 21

Figure 1.12 – The Airflow UI main page

9. Then, we stop our containers. We can stop our containers until we reach Chapter 9, when we
will explore data ingest in Airflow:
my-project/airflow-local$ docker-compose stop

How it works…
Airflow is an open source platform that allows batch data pipeline development, monitoring, and
scheduling. However, it requires other components, such as an internal database, to store metadata to
work correctly. In this example, we use PostgreSQL to store the metadata and Redis to cache information.
All this can be installed directly in our machine environment one by one. Even though it seems quite
simple, it may not be due to compatibility issues with OS, other software versions, and so on.
Docker can create an isolated environment and provide all the requirements to make it work. With
docker-compose, it becomes even simpler, since we can create dependencies between the
components that can only be created if the others are healthy.
You can also open the docker-compose.yaml file we downloaded for this recipe and take a look
to explore it better. We will also cover it in detail in Chapter 9.
22 Introduction to Data Ingestion

See also
If you want to learn more about how this docker-compose file works, you can look at the Apache
Airflow official Docker documentation on the Apache Airflow documentation page: https://
airflow.apache.org/docs/apache-airflow/stable/howto/docker-compose/
index.html.

Creating schemas
Schemas are considered blueprints of a database or table. While some databases strictly require
schema definition, others can work without it. However, in some cases, it is advantageous to work
with data schemas to ensure that the application data architecture is maintained and can receive the
desired data input.

Getting ready
Let’s imagine we need to create a database for a school to store information about the students, the
courses, and the instructors. With this information, we know we have at least three tables so far.

Figure 1.13 – A table diagram for three entities

In this recipe, we will cover how schemas work using the Entity Relationship Diagram (ERD), a visual
representation of relationships between entities in a database, to exemplify how schemas are connected.

How to do it…
Here are the steps to try this:

1. We define the type of schema. The following figure helps us understand how to go about this:
Another random document with
no related content on Scribd:
roots of the conscience and the will. Teufelsdröckh’s mental progress
out of the mood of the “Everlasting No” is a succession of practical
determinations as to the conduct of his own spirit, each
determination coming as an inspired effort of the will, altering his
demeanour from that moment, and the last bringing him into a final
condition of freedom and self-mastery. The effort of the will does
indeed diffuse a corresponding change through the intellect; but it is
as if on the principle, “Henceforth such and such a view of things
shall be my view,”—which is but a variation of the Scriptural
principle that it is by doing the law that one comes to know the
gospel.
The Leith Walk incident, accordingly, is to be taken as the
equivalent in Carlyle’s case to that first step out of the “Everlasting
No” of which he makes so much in the biography of Teufelsdröckh. It
was not by any means his complete conversion or emancipation, but
it was a beginning. It was, to use his own words, a change at least “in
the temper of his misery,” and a change for the better, inasmuch as it
substituted indignation and defiance for what had been mere fear
and whimpering. His mood thenceforth, though still miserable
enough, was to be less abject and more stern. On the whole, if this
construction of the Leith Walk incident of June 1821 does not make
so much of it as Mr. Froude’s does, it leaves enough of reason for any
Edinburgh youth, when he next chances to be in that straggling
thoroughfare between Edinburgh and Leith, to pause near the
middle of it, and look about him. The spot must have been just below
Pilrig Street, which was Carlyle’s starting-point from his lodgings in
Moray Street (now Spey Street) on his way to Leith.
There was, at all events, no very obvious change in Carlyle’s
mood and demeanour in Edinburgh in the latter part of 1821. His
own report in the Reminiscences is still of the dreariness of his life,
his gruff humours, and gloomy prognostications. But, corroborated
though this report is in the main by contemporary letters, it would be
a mistake, I believe, to accept it absolutely, or without such
abatements as mere reflection on the circumstances will easily
suggest. It is impossible to suppose that Carlyle, at this period of his
life or at any other, can have been all unhappy, even when he thought
himself most unhappy. There must have been ardours and glows of
soul, great joys and exhilarations, corresponding to the complexity of
nervous endowment that could descend to such depths of sadness.
From himself we learn, in particular, how the society of Irving,
whether in their Annandale meetings, or in Irving’s visits to
Edinburgh, had always an effect upon his spirits like that of sunrising
upon night or fog. Irving’s letters must have had a similar effect:
such a letter, for example, as that from Glasgow in which Irving had
written, “I am beginning to see the dawn of the day when you shall be
plucked by the literary world from my solitary, and therefore more
clear, admiration,” and had added this interesting note respecting
Dr. Chalmers: “Our honest Demosthenes, or shall I call him
Chrysostom?—Boanerges would fit him better!—seems to have
caught some glimpse of your inner man, though he had few
opportunities; for he never ceases to be inquiring after you.”[37]
Whether such letters brought Carlyle exhilaration or not, there
must have been exhilaration for him, or at least roused interest, on
Irving’s own account, in the news, which came late in 1821, that
Irving was not to be tied much longer to the great Glasgow
Demosthenes and his very difficult congregation. After two years and
a half of the Glasgow assistantship to Dr. Chalmers, there had come
that invitation to the pastorship of the Scotch Church, Hatton
Garden, London, which Irving received as exultingly, as he
afterwards said, as if it had been a call to the Archbishopric of
Canterbury. He passed through Edinburgh on his way to London to
offer himself on probation to the little colony of London Scots that
thought he might suit them for their minister; and Carlyle was the
last person he saw before leaving Scotland. The scene of their parting
was the coffee-room of the old Black Bull Hotel in Leith Street, then
the great starting-place for the Edinburgh coaches. It was “a dim
night, November or December, between nine and ten,” Carlyle tells
us; but Mrs. Oliphant’s Life of Irving helps us to the more precise
dating of December 1821, a day or two before Christmas. They had
their talk in the coffee-room; and Carlyle, on going, gave Irving a
bundle of cigars, that he might try one or two of them in the tedium
of his journey next day on the top of the coach. Who smoked the
cigars no one ever knew; for Irving, in the hurry of starting next
morning, forgot to take them with him, and left them lying in a stall
in the coffee-room.
That meeting at the Black Bull in Leith Street, however, was to
be remembered by both. Irving had gone to London to set the
Thames on fire; Carlyle remained in Edinburgh for his mathematical
teaching, his private German readings, his hackwork for Brewster’s
Edinburgh Encyclopædia, and the chances of continued
contributorship to the New Edinburgh Review. Thus the year 1821
ended, and the year 1822 began.
PART III.—1822–1828
By Carlyle’s own account, and still more distinctly by the
evidence of other records, the beginning of the year 1822 was marked
by a break in his hitherto cloudy sky. How much of this is to be
attributed to the continuance of the change of mental mood which
has to be dated from June 1821, and associated with the Leith Walk
revelation of that month, one can hardly say. One finds causes of an
external kind that must have contributed to the result.
One was the Charles Buller engagement. Carlyle’s dating of this
very important event in his life is rather hazy. In his Reminiscences
he gives us to understand that, after his parting with Irving at the
Black Bull in Edinburgh, just before the Christmas of 1821, he lost
sight of Irving altogether for a while, and was chagrined by Irving’s
silence. He thought their correspondence had come to an end;
accounted for the fact as well as he could by remembering in what a
turmoil of new occupation Irving was then involved in London; and
only came to know how faithful his friend had been to him all the
while when the Buller tutorship at £200 a year emerged, “in the
spring and summer of 1822,” as the product of Irving’s London
exertions in his behalf. In reading this account, one fancies Irving
already established in London, In fact, however, as Mrs. Oliphant’s
Life of Irving makes clear, Irving’s journey from the Black Bull to
London in December 1821 had been on a trial visit only. He was back
in Glasgow early in February 1822,—whence, on the 9th of that
month, he wrote a long letter to his “dear and lovely pupil,” Miss
Jane Welsh, sending it under cover to his friend “T. C.” in Edinburgh,
because he was not sure but she might be then in Edinburgh too; and
it was not till July 1822, and after some difficult negotiation, that
Irving, ordained by his native Presbytery of Annan, took his farewell
of Glasgow and the West of Scotland, and settled in London
definitely. The good turn he had done Carlyle in the matter of the
Buller tutorship must have been done, therefore, in his preliminary
London visit of January 1822, within a month after his parting from
Carlyle at the Black Bull, and before Carlyle’s cigars, if Irving had
taken them with him, could have been smoked out. It must have
been in those January weeks of his probationary preachings before
the Hatton Garden people that Irving, moving about as a new
Scottish lion in the drawing-room of the English Stracheys of the
India House, was introduced to Mrs. Strachey’s sister, Mrs. Buller,
and, after some meetings with that lady, helped her in a “domestic
intricacy.” This was that her eldest son, Charles Buller, a very clever
and high-spirited boy, of about fifteen years of age, “fresh from
Harrow,” but too young to go to Cambridge, was somewhat
troublesome, and she and her husband were at a loss what to do with
him. Irving’s advice had been to send the boy for a session or two to
the University of Edinburgh, and to secure for him there the private
tutorship of a certain young literary man, Mr. Thomas Carlyle, whom
Irving knew thoroughly and could highly recommend. Mrs. Buller
must have been a rapid lady, for the thing was arranged almost at
once. Carlyle had been communicated with; and he had accepted the
tutorship on the terms stipulated by Irving. It must have been on an
early day in the spring of 1822 that he made that call at the house of
the Rev. Dr. Fleming in George Square, to receive his new pupil,
Charles Buller, with Charles’s younger brother Arthur, on their
arrival in Edinburgh, and had that first walk with them by the foot of
Salisbury Crags, and up the High Street from Holyrood, of which
there is such pleasant mention in the Reminiscences. Dr. Fleming, a
fellow-contributor with Carlyle to Brewster’s Encyclopædia, and a
much respected clergyman of Edinburgh, had interested himself
greatly in Irving’s London prospects, and had tried to smooth the
way for him by letters to London friends; and it was in his house in
George Square that the two English boys were to board,—Carlyle
coming to them daily from his lodgings in Moray Street. He had
already, before the arrival of the boys, he tells us, entered Charles
Buller in Dunbar’s “third Greek class” in the University. The
information agrees with the University records; for in the
matriculation-book of the session 1821–22 I find one of the very
latest matriculations to have been that of “Charles Buller, Cornwall,”
and I find him to have been all but the last student enrolled for that
session in Dunbar’s senior class. This of itself would imply that
Carlyle’s tutorship of the boys must have begun in February 1822;
for, as the University session ends in the beginning of April, it would
have hardly been worth while to enroll the young Buller in a class
after February. The tutorship was a settled thing, therefore, while
Irving was still in Glasgow, and it had been going on for some
months before Irving’s permanent removal to London. Carlyle
himself seems to have become aware of the haziness of his dating of
the transaction; for he inserts, by way of afterthought, a dim
recollection of one or two sights of Irving somewhere shortly after
the Black Bull parting, and of talks with him about the Buller family
while the tutorship was in its infancy. Anyhow, the Buller tutorship,
with its £200 a year, was “a most important thing” to Carlyle in “the
economies and practical departments” of his life at the time; and he
owed it “wholly to Irving.” The two boys, Charles Buller especially,
took to their new tutor cordially at once, and he cordially to them;
and there were no difficulties. In the classics, indeed, and especially
in Greek, Charles Buller, fresh from his Harrow training, was
Carlyle’s superior; but Carlyle could do his duty for both the boys by
getting up their Latin and Greek lessons along with them, teaching
them as much mathematics as they would learn, and guiding them
generally into solid reading, inquiry, and reflection.
Another gleam of sunshine in Carlyle’s life early in 1822, or what
ought to have been such, was the correspondence with Haddington.
Since the visit of the previous June that had gradually established
itself, till it had become constant, in the form of “weekly or oftener
sending books, etc., etc.,” with occasional runs down to Haddington
in person, or sights of Miss Welsh, with her mother, in Edinburgh.
How far matters had gone by this time does not distinctly appear;
but there is some significance in the fact that Irving, writing from
Glasgow to Miss Welsh immediately after his return from the trial-
preachings before the Hatton Garden congregation in London, had
sent the letter through “T. C.” The impression made by that letter, as
it may be read in Mrs. Oliphant’s Life of Irving, certainly is that
Irving’s own feelings in the Haddington quarter were still of so
tender a kind that the advancing relations of “T. C.” to the “dear and
lovely pupil” were not indifferent to him. Doubtless there were
obstacles yet in the way of any definite engagement between Carlyle
and the young lady who was heiress of Craigenputtock,—criticisms of
relatives and others who “saw only the outside of the thing”; but the
young lady “had faith in her own insight,” as she afterwards told Miss
Jewsbury, and was likely to act for herself. Meanwhile, to be “aiding
and directing her studies,” and have a kind of home at Haddington
when he chose to go there on a Saturday, was surely a tinge of gold
upon the silver of the Buller tutorship.
Moreover, Carlyle’s occupations of a literary kind were
becoming more numerous and congenial. “I was already getting my
head a little up,” he says, “translating Legendre’s Geometry for
Brewster; my outlook somewhat cheerfuller.” All through the
preceding year, it appears from private letters, he had been exerting
himself indefatigably to find literary work. Thus, in a letter of date
March 1821 to an old college friend: “I have had about twenty plans
this winter in the way of authorship: they have all failed. I have about
twenty more to try; and, if it does but please the Director of all things
to continue the moderate share of health now restored to me, I will
make the doors of human society fly open before me yet,
notwithstanding. My petards will not burst, or make only noise when
they do. I must mix them better, plant them more judiciously; they
shall burst, and do execution too.”[38] Again, in a letter of the very
next month: “I am moving on, weary and heavy-laden, with very
fickle health, and many discomforts,—still looking forward to the
future (brave future!) for all the accommodation and enjoyment that
render life an object of desire. Then shall I no longer play a candle-
snuffer’s part in the great drama; or, if I do, my salary will be
raised.”[39] From Mr. Froude we learn that one of the burst petards of
1821 had been the proposal to a London publishing firm of a
complete translation of Schiller’s Works. That offer having been
declined, with the twenty others of which Carlyle speaks, the only
obvious increase of his literary engagements at the time of the
beginning of the Buller tutorship in 1822 consisted, it would appear,
in that connection with the New Edinburgh Review of which
mention has been already made, and in the translation of Legendre
which he had undertaken for Brewster. But there was more in the
background. There is significance in the fact that his second
contribution to the New Edinburgh, published in April 1822, when
the Buller tutorship had just begun, was an article on Goethe’s Faust.
The German readings which had been going on since 1819 had
influenced him greatly; and he was now absorbed in a passion for
German Literature. Schiller, Goethe, and Jean Paul were the
demigods of his intellectual worship, the authors in whose works,
rather than in those of any of the same century in France or Britain,
he found suitable nutriment for his own spirit. He had proposed, we
see, to translate the whole of Schiller. Of his studies in Goethe and
their effects we have a striking commemoration in the passage of his
Reminiscences where he tells of that “windless, Scotch-misty,
Saturday night,” apparently just about our present date, when,
having finished the reading of Wilhelm Meister, he walked through
the deserted streets of Edinburgh in a state of agitation over the
wonders he had found in that book. Henceforth, accordingly, he had
a portion of his literary career definitely marked out for him.
Whatever else he was to be, there was work enough before him for a
while in translation from the German and in commentary on the
great German writers for the behoof of the British public. There were
but three or four men in Britain competent for that business, and he
was one of them.
The translation of Legendre’s Geometry for Brewster deserves a
passing notice. Though not published till 1824,—when it appeared,
from the press of Oliver and Boyd of Edinburgh, as an octavo of
nearly 400 pages, with the title Elements of Geometry and
Trigonometry; with Notes. Translated from the French of A. M.
Legendre, Member of the Institute, etc. Edited by David Brewster,
LL.D., etc. With Notes and Additions, and an Introductory Essay on
Proportion,—it was begun by Carlyle in 1822, and continued to
occupy him through the whole of that year. His authorship of this
Translation remained such a secret, or had been so forgotten, that
the late Professor De Morgan, specially learned though he was in the
bibliography of mathematics, did not know the fact, and would
hardly believe it, till I procured him the evidence. It was one day in
or about 1860, if I remember rightly, and in the common room of
University College, London, that De Morgan, in the course of the
chats on all things and sundry which I used to have with him there,
adverted to the Legendre book. He knew, he said, that Brewster
himself could not have done the translation; but he had always been
under the impression that the person employed by Brewster had
been a certain Galbraith, a noted teacher of mathematics in
Edinburgh. Recently, however, he had heard Carlyle named as the
man; and, being very doubtful on the point, he wanted very much to
be certain. To back my own statement, I undertook to obtain an
affidavit from head-quarters. “Tell De Morgan,” said Carlyle, when I
next saw him, “that every word of the book is mine, and that I got
£50 for the job from Brewster; which was then of some consideration
to me.” He went on to speak, very much as he does in the
Reminiscences, of the prefixed little Essay on Proportion, retaining a
fond recollection of that section of the book,—begun and finished, he
says, on “a happy forenoon (Sunday, I fear)” in his Edinburgh
lodgings, and never seen again since he had revised the proof. De
Morgan, who had some correspondence on the subject with Carlyle
after I had conveyed Carlyle’s message, paid it a compliment
afterwards in his Budget of Paradoxes, by calling it “as good a
substitute for the Fifth Book of Euclid as could be given in speech”;
and a glance at the Essay in the volume itself will confirm the
opinion. It fills but eight printed pages, and consists of but four
definitions and three theorems, wound up with these concluding
sentences:—“By means of these theorems, and their corollaries, it is
easy to demonstrate, or even to discover, all the most important facts
connected with the Doctrine of Proportion. The facts given here will
enable the student to go through these Elements [Legendre’s]
without any obstruction on that head.”
The Translation of Legendre, with this Essay on Proportion, was
Carlyle’s farewell to Mathematics. To the end of his life, however, he
would talk with great relish of mathematical matters. Once, in the
vicinity of Sloane Street, when I mentioned to him a geometrical
theorem which Dr. Chalmers had confided to me, with the
information that he had been working at it all his life and had never
accomplished the solution, Carlyle became so eager that he made me
stop and draw a diagram of the theorem for him on the pavement.
Having thus picked up the notion of it, he branched out, in the most
interesting manner, as we walked on, into talk and anecdote about
mathematics and mathematicians, with references especially to
Leslie, West, Robert Simson, and Pappus. A marked similarity of
character between Carlyle and Chalmers was discernible in the fact
that they both avowed a strong personal preference for the old pure
geometry over the more potent modern analytics. “In geometry, sir,
you are dealing with the ipsissima corpora,” Chalmers used to say;
and Carlyle’s feeling seems to have been something of the same kind.
There was a variation of Carlyle’s Edinburgh existence, not
altogether disagreeable, when the seniors of the Buller family
followed the two boys, and made Edinburgh for some time their
residence. They took up house in India Street, giving dinners and
seeing a good deal of company; and Carlyle, while continuing his
lessons to young Charles and Arthur, was thus a good deal in India
Street, observing new society, and becoming acquainted with Mr.
Buller senior, the sprightly Mrs. Buller, and their third and youngest
child, Reginald. As he makes this advent of the Bullers to Edinburgh
to have been “towards the autumn” in 1822, we are able to connect it
with another advent.
It was on the 15th of August 1822, after several weeks of
enormous expectation, that George IV. arrived in Edinburgh,
welcomed so memorably on board his yacht before landing by Sir
Walter Scott; and thence to the 29th, when his Majesty took his
departure, all Edinburgh was in that paroxysm of loyal excitement
and Celtic heraldry and hubbub of which Sir Walter was the soul and
manager, and a full account of which is to be found in his Life by
Lockhart. It is hardly a surprise to know that what the veteran Scott,
with his great jovial heart, his Toryism, and his love of symbols, thus
plunged into and enjoyed with such passionate avidity, tasking all his
energies for a fortnight to make the business a triumphant success,
the moody young Carlyle, then a Radical to the core, fled from in
unmitigated disgust. He tells us in his Reminiscences how, on seeing
the placard by the magistrates of Edinburgh, a day or two before the
King’s arrival, requesting all the citizens to appear in the streets well-
dressed on the day of his Majesty’s entry, the men in “black coats and
white duck trousers,” he could stand it no longer, and resolved to be
absent from the approaching “efflorescence of the flunkeyisms.” The
tutorial duties with the Bullers being naturally in abeyance at such a
time, and rooms in Edinburgh being so scarce that the use of
Carlyle’s in Moray Street was a welcome gift to his merchant friends,
Graham and Hope, who were to arrive from Glasgow for the
spectacle, he himself was off for a run in Annandale and Galloway
before his Majesty made his appearance; and he did not return till all
the hubbub of the fortnight was “comfortably rolled away.” I have
heard him describe this flight of his from George IV., and from the
horrors of that fortnight of feastings, processionings, huzzaings, and
bagpipings, round his Majesty in Edinburgh, at more length and in
greater detail than in the passage incidentally given to the subject in
the Reminiscences; and one of the details may be worth relating:—
On the first stage out of Edinburgh he put up for the night at some
village inn. Even at that distance the “efflorescence of the
flunkeyisms” from which he had fled seemed to pursue him; for the
talk of the people at the inn, and the very papers that were lying
about, were of nothing but George IV. and the Royal Visit. Taking
refuge at last in his bedroom, he was fighting there with his habitual
enemy, sleeplessness, when, as if to make sleep absolutely impossible
for that night, there came upon his ear from the next room, from
which he was separated only by a thin partition, the moanings and
groanings of a woman, in distress with toothache or some other pain.
The “oh! oh!” from the next room had become louder and louder,
and threatened to be incessant through the whole night, so that each
repetition of it became more and more insufferable. At last, having
knocked to solicit attention, he addressed the invisible sufferer
through the partition thus: “For God’s sake, woman, be articulate. If
anything can be done for you, be it even to ride ten miles in the dark
for a doctor, tell me, and I’ll do it; if not, endeavour to compose
yourself.” There ensued a dead silence, and he was troubled no more.
The Edinburgh University records show that “Charles Buller,
Cornwall,” matriculated again for the session 1822–3 (one of the very
earliest students to matriculate that year, for he stands as No. 8 in a
total of 2071 matriculations), and that he attended the 2nd Latin
class, under Professor James Pillans, who had succeeded Christison
as Humanity Professor in 1820. A later name in the matriculation list
(No. 836) is that of “Arthur Buller,” who had not attended the
University with his brother in the previous year, but now joined him
in the 2nd Latin class, and also took out Dunbar’s 2nd Greek class. In
the same matriculation list of 1823–3 (No. 21), as entering the
University for the first time, and attending Pillans’s 2nd Latin class
with the two Bullers, appears “John Carlyle, Dumfriesshire.” This
was Carlyle’s younger brother, the future Dr. John Carlyle, translator
of Dante, and the only other of the family who received a University
education. He had been for some time a teacher in Annan School, in
succession to his brother; and, as he was to choose the medical
profession, his present attendance in the Arts classes was but
preliminary to attendance in the medical classes in the sessions
immediately to follow. He lodged, as the Reminiscences tell us, with
his brother, in the rooms in Moray Street, Pilrig Street.
The winter of 1822–3 was passed by Carlyle in the Edinburgh
routine of his daily walks from those rooms to the house of the
Bullers in India Street, his tutorship of the two young Bullers and
other intercourse with the Buller family and their guests, and his
own German and other readings and literary efforts and schemings.
It was in that winter, and not at the earlier date hazily assigned in the
Reminiscences, that the cessation of correspondence with Irving
became a matter of secret vexation to him. The good Irving, now in
the full whirl of his activity with the Hatton Garden congregation and
of the London notoriety to which that led, was too busy to write; and
it was only by rumour, or by letters from others, that Carlyle heard of
Irving’s extraordinary doings and extraordinary successes in the
metropolis, of the crowds that were flocking to hear him in the little
Scotch chapel, and of the stir he and his preachings were making in
the London fashionable world. “People have their envies, their pitiful
self-comparisons,” says Carlyle, admitting that the real joy he felt at
the vast and sudden effulgence of his friend into a fame
commensurate with his powers was tempered by a sense of the
contrast between himself, still toiling obscurely in Edinburgh, a
“poor, suffering, handcuffed wretch,” and the other Annandale
fellow, now so free and glorious among the grandees on the Thames.
There was, he adds, just a speck of another feeling. Would Irving be
able to keep his head in the blaze of such enormous London
popularity? Had he strength enough to guide and manage himself in
that huge element with anything like the steadiness that had
characterised the behaviour of the more massive and more simple-
hearted Chalmers in Glasgow? This feeling, he seems to hint, was
increased rather than lessened when Irving’s first publication came
into his hands,—the famous Orations and Arguments for Judgment
to Come, by which, early in 1823, the cooler and more critical world
was enabled to judge of the real substance of those pulpit-discourses
which were so amazing the Londoners. Meanwhile, as Irving himself
was still silent, Carlyle could only plod on at his own work. It seems
to have been late in 1822, or early in 1823, that, having closed his
contributions to Brewster’s Encyclopædia, and got the Legendre
translation off his hands, he set himself to his Life of Schiller.
If, however, the Life of Schiller was begun in Edinburgh, it was
not finished there. The University session of 1822–3 over, and the
spring and summer of 1823 having come, the Bullers, with that
aptitude for change of residence which characterises retired Indians
and people with plenty of money, had removed to the mansion of
Kinnaird in Perthshire, situated on the river Tay, some miles to the
north of Dunkeld. Carlyle and his tutorship of young Charles and
Arthur Buller had, accordingly, been transferred thither. He must
have been there early in June 1823; for a letter of his is extant, dated
from Kinnaird House on the 17th of that month, in which he
describes his first sight of Dunkeld and its old cathedral, with
Dunsinane Hill, and the position of old Birnam Wood in the
neighbourhood, and his thoughts in those spots of “the immortal
link-boy” that had made them famous. The same letter gives an
interesting glimpse of his own mood in the first month of his Tayside
residence with the Bullers. “Some time hence,” he says to his
correspondent, Thomas Mitchell, “when you are seated in your
peaceful manse,—you at one side of the parlour fire, Mrs. M. at the
other, and two or three little M.’s, fine chubby urchins, hopping
about the carpet,—you will suddenly observe the door fly open, and a
tall, meagre, careworn figure stalk forward, his grave countenance
lightened by unusual smiles in the certainty of meeting a cordial
welcome. This knight of the rueful visage will, in fact, mingle with the
group for a season, and be merry as the merriest, though his looks
are sinister. I warn you to make provision for such emergencies. In
process of time I too must have my own peculiar hearth; wayward as
my destiny has hitherto been, perplexed and solitary as my path of
life still is, I never cease to reckon on yet paying scot and lot on my
own footing.”[40] From the Reminiscences, where we learn that he
was at this time persevering with his Life of Schiller, we have his
later recollection of those summer and autumn months, and on into
late autumn, in Kinnaird House:—
“I was nightly working at the thing in a serious, sad, and totally solitary, way.
My two rooms were in the old mansion of Kinnaird, some three or four hundred
yards from the new, and on a lower level, overshadowed with wood. Thither I
always retired directly after tea, and for most part had the edifice all to myself,—
good candles, good wood fire, place dry enough, tolerably clean, and such silence
and total absence of company, good or bad, as I never experienced before or since.
I remember still the grand sough of those woods, or, perhaps, in the stillest times,
the distant ripple of the Tay. Nothing else to converse with but this and my own
thoughts, which never for a moment pretended to be joyful, and were sometimes
pathetically sad. I was in the miserablest dyspeptic health, uncertain whether I
ought not to quit on that account, and at times almost resolving to do it; dumb, far
away from all my loved ones. My poor Schiller, nothing considerable of a work
even to my own judgment, had to be steadily persisted in, as the only protection
and resource in this inarticulate huge wilderness, actual and symbolical.”[41]
It was in October 1823 that the first part of Schiller’s Life and
Writings appeared, without the author’s name, in the then
celebrated London Magazine of Messrs. Taylor and Hessey. It was
the most important of the metropolitan magazines of that time,
counting among its contributors, since its foundation in 1820, such
writers as Charles Lamb, Hazlitt, Allan Cunningham, the Rev. Henry
Francis Cary, Hamilton Reynolds, Bryan Waller Procter, Thomas
Noon Talfourd, young Thomas Hood, and De Quincey. The
admission of Carlyle into such company, the opening of such a
London connection at last, ought to have been some gratification to
him in his recluse life at Kinnaird; and, doubtless, it was, to a far
greater extent than he could remember when he wrote the
Reminiscences. He does vaguely mention there that, though his own
judgment of the merits of his performance was not very high, he had
compliments from the editor of the magazine,—i.e., we must
suppose, from Messrs. Taylor and Hessey, who were their own
editors, unless indeed young Thomas Hood, who was a kind of
assistant editor, was the medium of the communication. What is
more important is that the Life of Schiller, if not all in the editor’s
hands complete when the first part appeared, must have been
reported as complete, or as approaching completeness, in Carlyle’s
own hands at Kinnaird. This, accordingly, fixes October 1823, or
thereabouts, as the date of his passing on from Schiller to the new
work which he had prescribed for himself as a sequel, viz. the
Translation of the Wilhelm Meister. It must have been in one of
those nocturnal sittings in the late autumn of 1823 in the old
mansion of Kinnaird, amid “the grand sough of those woods”
outside, when his Schiller manuscript lay finished beside him, and he
had Goethe before him, that there happened that “Tragedy of the
Night-Moth” which he has commemorated in one of his metrical
fragments—
“’Tis placid midnight; stars are keeping
Their meek and silent course in heaven;
Save pale recluse, for knowledge seeking,
All mortal things to sleep are given.

But see! a wandering night-moth enters,


Allured by taper gleaming bright;
A while keeps hovering round, then ventures
On Goethe’s mystic page to light.

With awe she views the candle blazing


A universe of fire it seems
To moth-savante with rapture gazing,
Or fount whence life and motion streams.

What passions in her small heart whirling,


Hopes boundless, adoration, dread?
At length, her tiny pinions twirling,
She darts, and,—puff!—the moth is dead.”

Carlyle’s own distinct statement in the Reminiscences is that


Irving had encouraged him in the Life of Schiller, and had “prepared
the way” for it in the London Magazine. How is this to be reconciled
with his repeated references to the total cessation of correspondence
between himself and Irving from the date of Irving’s definite
settlement in London to that week, “late in autumn 1823,” when
Irving, having married Miss Martin of Kirkcaldy, was on his
marriage-jaunt with her in Scotland, and generously determined to
pass near Kinnaird, so as to pick up his old friend and have a day or
two of his society? One might have thought that it was in this
renewed meeting of the two friends in Irving’s honeymoon jaunt that
there came from Irving the suggestion of the London Magazine as a
place for the Schiller, or the intimation that he had already arranged
for it and knew it would be welcome there. This supposition,
however, will not cohere with the date of Irving’s marriage. It took
place at Kirkcaldy on the 13th of October 1823, after the number of
the London Magazine containing the first part of the Schiller had
been out for a fortnight; and Irving’s marriage-tour in Scotland
lasted through the rest of that month and the whole of November.
There must, therefore, have been renewed correspondence between
Irving and Carlyle, with arrangements about the Schiller, some while
before October 1823, though Carlyle’s memory had become hazy
about that matter too. It is pleasant to be sure of the main fact,—
which is that it was to the ever-friendly Irving that Carlyle owed this
second great service of his introduction to the London Magazine,
just as he had already owed him the Buller tutorship.
The winter of 1823–4 seems to have been passed wholly at
Kinnaird. At least, there was no re-appearance of the Bullers in
Edinburgh that winter, and no re-attendance that winter of Charles
Buller or his brother Arthur in any of the classes of Edinburgh
University. What we gather from the Reminiscences is that, towards
the end of the winter, the Bullers had begun to weary of Kinnaird life,
and indeed of life in Scotland, and were meditating a return to
England, possibly for ultimate settlement in Cornwall, but certainly
with a view to London as their intermediate head-quarters. He hints
also that they had by this time been a good deal exercised by the
moodiness and miserable bad health of the strange tutor they had
with them, and whom they respected and admired so much. Might it
not be the best arrangement that he should go for a month or two to
his native Annandale to recruit his health, and then rejoin them in
London, there again to take charge of his pupils?
Taking leave of Kinnaird with that understanding, Carlyle, it
appears, rode, either directly thence or very soon afterwards from his
father’s house at Mainhill, all the way to Edinburgh, to consult a
doctor about his dyspepsia. Was it chronic, and incurable except by
regimen? or could it be removed by medical treatment? “It is all
tobacco, sir; give up tobacco,” was the physician’s answer; on which
Carlyle’s comment is that, having instantly and absolutely followed
the advice, and persevered for “long months” in total abstinence
from tobacco, without the slightest sign of improvement, he came to
the conclusion that he might as well have ridden sixty miles in the
opposite direction, and poured his sorrows into the “long hairy ear of
the first jackass” he met, as have made that ride to Edinburgh to
consult the great authority. This story of the tobacco consultation
was a favourite one with Carlyle in later days. I have heard it from
him several times, with two additions to what appears in the
Reminiscences. One was that, the doctor having asked him whether
he could give up tobacco, “Give it up, sir?” he replied; “I can cut off
my left hand with an axe, if that should be necessary!” The other was
an account of his months of probation of the new no-tobacco
regimen. The account took the form of a recollection of himself as
staggering for months from tree-trunk to tree-trunk in a
metaphorical wood, tobaccoless and dreary, without one symptom of
benefit from his self-denial, till at last, sinking at the foot of one of
the tree-trunks, and seeing a long clay and a tobacco-pouch
providentially lying on the turf, he exclaimed “I will endure this
diabolical farce and delusion no longer,” and had a good smoke then
and there once more, in signal of reverting for ever to his old
comfort. Tobacco and a very little good brandy, he used to say to the
end of his life, were the only two drugs in the whole pharmacopœia
that he had found of any real utility to the distressed human
organism.[42]
It was during the two or three spring months of 1824, spent at
Mainhill in Dumfriesshire, under the care of that “best of nurses and
of hostesses,” his mother, that the Translation of Wilhelm Meister
was finished. It was in the June of the same year that, having revised
the proofs of the three volumes of that book for Messrs. Oliver and
Boyd of Edinburgh, who had agreed to be the publishers, as they
were also of his translation of Legendre’s Geometry, and having run
up to Edinburgh himself with the last proofs and the preface, and
received from Messrs. Oliver and Boyd £180 for the labour, and
having taken a farewell at Haddington the purport of which may be
guessed, he embarked in the Leith smack that was to carry him to
London. He was then in his twenty-ninth year, and it was his first
visit to the Great Babylon. The second part of his Life of Schiller had
appeared in the number of the London Magazine for January 1824;
but the rest had still to be published, and would probably appear in
the magazine when he was himself in London and had formed
personal acquaintance with the editorial powers. Copies of the
Wilhelm Meister from the press of Messrs. Oliver and Boyd would
follow him from Edinburgh; and it would thus be as the anonymous
author of the Life of Schiller and of the Translation of Wilhelm
Meister that he would first step into London literary society. For the
rest, his prospects were utterly undefined. Whether he should
remain in London permanently, or return to Scotland, depended on
events not yet calculable. All that was certain was that the Buller
tutorship would still be his anchorage for a time in London, as it had
been for the last two years in Scotland, and that he had Irving’s
house for his London home so long as he might choose. It was, in
fact, to Irving’s house in Myddelton Terrace, Islington, where Irving
and his wife were living as a newly-married couple, that Carlyle was
to steer himself after the Leith smack had landed him in London
river.

From this point there is a break of two years and four months in
Carlyle’s life, during which he had nothing to do with Edinburgh. The
incidents of that interval may be filled in briefly thus:—
Nine Months in London and Birmingham (June 1824–March
1825).—Residing with the Irvings at Islington, or in lodgings near
them, Carlyle in those months made his first acquaintance with
London, and with various persons in it of greater or less note.
Introduced at once to the Stracheys, and to the then celebrated Mr.
and Mrs. Basil Montagu of Bedford Square, it was through them, or
otherwise directly or indirectly through Irving, that he saw
something of Coleridge, Charles Lamb, Allan Cunningham, Bryan
Waller Procter, Crabb Robinson, and others of literary name, besides
such commercial London Scots of Irving’s congregation as Sir Peter
Laurie, Mr. William Hamilton, and Mr. Dinwiddie, and the young
English manufacturing chemist, Mr. Badams of Birmingham. After
Mrs. Strachey and the queenly Mrs. Basil Montagu, his most valued
new friends in this list, he tells us, were Procter, Allan Cunningham,
and Badams. This last, indeed, under pretext of putting him on a
regimen that would cure his dyspepsia, lured him away to
Birmingham for three months; which three months of residence with
Badams in Birmingham, and of rambles with Badams hither and
thither in Warwickshire and sights of Joe Parkes and other
Birmingham notabilities, have to be interpolated therefore in the
general bulk of the London visit. There was also a trip to Dover, in
the company of the Stracheys and the Irvings, with a run of some of
the party, Carlyle one of them, to Paris, for ten days of Parisian sight-
seeing. Altogether, the London visit had been so successful that,
when the tutorial engagement with the Bullers came to an end in the
course of it,—which it did from the impossibility of an adjustment of
Carlyle’s views with Mrs. Buller’s ever-changing plans,—the notion
among his friends was that he could not do better than remain in
London and take his chances as a London man of letters. The
concluding portions of his Schiller had appeared in the London
Magazine during the first months of his visit; and before the end of
1825 the five portions into which the work had been cut up for
magazine purposes had been gathered together, and published by
Messrs. Taylor and Hessey in the form of an octavo volume, with the
title The Life of Friedrich Schiller, bringing the author £100. It was
during his stay in London also that he received his first
communication from Goethe, in the form of a brief letter of thanks
for a copy of the Translation of Wilhelm Meister which had been
sent to Weimar some months before. But, though things seemed thus
to conspire in favour of the detention of Carlyle in London, he had
made up his mind to the contrary; and in March 1825 he turned his
back upon the great city, and was on his way once more to his native
Dumfriesshire.
Nineteen Months of Dumfriesshire Farm-life (March 1825–
October 1826):—For about two months Carlyle was at his father’s
farmhouse of Mainhill, near Ecclefechan, resting from his return-
tour through England, and preparing for the adventure which he had
planned. This was an attempt at tenant-farming on his own account
in that neighbourhood. A letter of his to Mrs. Basil Montagu, of date
May 20, 1825, is still from Mainhill; but on the 26th of that month he
entered on the possession of the adjacent little farm of Hoddam Hill,
which he had taken on lease from his father’s landlord, General
Sharpe,—“a neat, compact little farm, rent £100,” with “a
prettyishlooking cottage.” Here for a whole year he lived, nominally a
tenant-farmer, as his father was, and close to his father, but in reality
entrusting the practical farm-work to his brother Alick, while he
himself, with his mother or one of his sisters for his housekeeper,
delved a little for amusement, rode about for health, and pursued his
studies and literary tasks,—chiefly his projected translation of
Specimens of German Romance for the bookseller Tait of
Edinburgh. There were letters to and from his London friends; there
was once a sight in Annan of poor Irving, whose London troubles and
aberrations were by this time matters of public notoriety; there were
visits to and from neighbours; but, on the whole, the year was one of
industrious loneliness. Though he tells us but little of it, what he does
tell us enables us to see that it was a most important and memorable
year in his recollection. Perhaps in all Carlyle’s life no other year is so
important intrinsically. “I call that year idyllic,” he tells us, “in spite
of its russet coat.” This is general; but he gives us vital particulars. It
was the time, he distinctly says, of his complete spiritual triumph, his
attainment once and for ever to that state of clear and high serenity,
as to all the essentials of religion and moral belief, which enabled
him to understand in his own case “what the old Christian people
meant by conversion,” and which he described afterwards, in the
Teufelsdröckh manner, as the reaching of the harbour of the
“Everlasting Yes” at last. The word happiness was no favourite one in
Carlyle’s vocabulary, with reference to himself at least; but he does
not refuse even this word in describing his new mental condition
through the year at Hoddam Hill. What he felt, he says, was the
attainment of “a constant inward happiness that was quite royal and
supreme, in which all temporal evil was transient and insignificant.”
Even his bodily health seemed to be improving; and the effect
extended itself most manifestly to his temper and disposition
towards others. “My thoughts were very peaceable,” he says, “full of
pity and humanity as they had never been before.” In short, he was
no longer the moody, defiant, mainly despondent and sarcastic
Carlyle he had been, or had seemed to be to superficial observers,
through the past Edinburgh days, but a calmer, wiser, and more self-
possessed Carlyle, with depths of tenderness under all his strength
and fearlessness,—the Carlyle that he was to be recognised as being
by all who knew him through the next twenty years of his life, and
that indeed he continued to be essentially to the very end. To what
agency does he attribute this “immense victory,” as he calls it, which
he had thus permanently gained over his own spirit in this thirtieth
year of his age, passed at Hoddam Hill? “Pious musings,
communings silent and spontaneous with Fact and Nature in those
poor Annandale localities,”—these, including the sound on Sundays
of the Hoddam kirk-bell coming to him touchingly from the plain
below, “like the departing voice of eighteen centuries,” are
mentioned as accounting for much, but not for all. “I then felt, and
still feel, endlessly indebted to Goethe in the business. He, in his
fashion, I perceived, had travelled the steep rocky road before me,
the first of the moderns.” Not to be forgotten either, as that which
tinged the year to perfection in its “idyllic” character, was the flitting
across the scene of the presence that was dearest to him. His pledged
bride, no longer at Haddington, but residing with her relatives in
Nithsdale, made her first visit to his family in this year; they rode

You might also like