What is Hadoop Sqoop?

Introduction

If you’re planning to store data in Hadoop, you may need to first move that data out of a relational database. While that migration process might sound like a daunting task, it’s actually quite simple to accomplish with the help of Sqoop, a tool built to transfer data from a relational database to Hadoop. In this article, we’ll explain how Hadoop and Sqoop work together and talk about some of the most common uses of the tool.

How Do Hadoop and Sqoop Work Together?

There are multiple ways that you can use Hadoop and Sqoop together. Here are some of the key features of the tool:

  • Bulk Import: You can import tables or even a whole database into the Hadoop Distributed File System (HDFS) using Sqoop. The data is then stored in directories and files as part of the HDFS filesystem.

  • Direct Import: Sqoop can be used to import SQL-based databases directly into HBase or Hive.

  • Interaction with Data: If you’d like to interact with your data programmatically, Sqoop can generate the appropriate Java classes.

  • Data Export: Sqoop isn’t just for importing data into Hadoop– it can also be used to transfer data from HDFS to a relational database.

Using Sqoop to Import Data into Hadoop

Sqoop is easy to use, with a simple command-line interface. One of the basic Sqoop commands is the import command, which takes data from a relational database and brings it into Hadoop. You can use this command to import a whole table, or you can add a --where clause, just as you would in a typical SQL query, to import just a subset of a table. When you execute a Sqoop command such as import, the tool parses the command behind the scenes and creates the appropriate MapReduce job to handle the task. The mapper connects to the database using JDBC and writes the data to HDFS, HBase or Hive depending on the options specified in the command.

In addition to importing individual tables or subsets of tables, you can also use the import-all-tables to transfer all tables in a given database to Hadoop. If you use this option, each table will be stored in its own directory within HDFS. Although the basic import command allows you to select specific columns to import, this option isn’t available when you opt to import all tables.

Using Sqoop to Export Data from Hadoop

If you need to transfer data from HDFS to a relational database, the export command can be used. This command offers the --export-dir option to specify the directory from which you’d like to export data and the --table option to indicate the database table that will be populated. Keep in mind that the destination table must already exist in the database before running the export command.

When you export HDFS data into a table, it defaults to “insert mode”, where new rows are appended to the end of a table. However, it’s possible to have Sqoop update existing rows in the database using the --update-key argument. This option signals Sqoop to create UPDATE statements that will run on the target relational database. Sqoop even provides an option to run in “upsert” mode, which is database jargon for an update operation that will instead insert a row if no matching record to update is found. You can think of this process as “UPDATE, else INSERT”. You can perform a Sqoop export in “upsert” model using the --update-mode argument.

Conclusion

Transferring data from a relational database to Hadoop is a tedious process that often requires the development of a custom script. With Sqoop, most of the work is automated, making the transfer process smooth and efficient. Not only can you transfer data from a relational database into Hadoop with Sqoop, but you can also take data from Hadoop and transfer it to a relational database. After reading this overview of Sqoop, you’ll have all the information you need to get started and automate your own data-transfer tasks.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.