MongoDB and Hadoop: A Step-by Step Tutorial Using
The following is a guest post from Jeremy Karn. This article is excerpted from MongoDB + Hadoop: A Step-by-Step Tutorial. Jeremy is a cofounder at Mortar Data, a Hadoop-as-a-service provider, and creator of mortar, an open source framework
The following is a guest post from Jeremy Karn. This article is excerpted from ‘MongoDB + Hadoop: A Step-by-Step Tutorial’. Jeremy is a cofounder at Mortar Data, a Hadoop-as-a-service provider, and creator of mortar, an open source framework for data processing.
People who are worried about scalability often find themselves looking at two tools: MongoDB for storing large amounts of data easily and Hadoop for processing that data. But a common question is: “How do I combine these two to really get the most out of my data?”
Here’s a step-by-step tutorial that will get you up and running with MongoDB and Hadoop in a matter of minutes. And the best part about this tutorial is that at the end you’ll be ready to jump right into using your own MongoDB data with Hadoop.
For this tutorial you’ll be using Apache Pig, a high-level data flow language that compiles down into Hadoop MapReduce jobs. It was designed to be easy to learn and simple to write. If you’ve written SQL, Pig will feel familiar, it is like procedural SQL.
To run your Hadoop jobs, you’re going to use a free Mortar account. Mortar provides Hadoop as a service, which means you can run your jobs without worrying about how to set up and manage a multi-node Hadoop cluster.
To get started, we’ve already set up a small MongoDB instance on MongoLab, populated it with a random sampling of Twitter data from a single day (around 120,000 tweets), and created a read-only user for you.
We’ve also set up a public Github repo with a Mortar project that has three Pig scripts ready to run. Here’s what you need to do:
If you don’t already have a free Github account - create one.? You’ll need a github username in step 4.
- Sign into (or create) your free Mortar account.
- After you receive the confirmation email, log into Mortar at https://app.mortardata.com.
- Install?the Mortar Development Framework:?
gem install mortar
Salin selepas log masuk -
Clone the example git project and register it as a mortar project:?
git clone git@github.com:mortardata/mongo-pig-examples.git
Salin selepas log masukcd mongo-pig-examples
Salin selepas log masukmortar register mongo-pig-examples
Salin selepas log masuk
Script 1 - Characterize Collection
If you’re like most MongoDB users, you may not have a great sense of the different fields, data types, or values in your collection. We built characterize_collection.pig to deeply inspect your collection to extract that information.
From the base directory of the mongo-pig-examples project you just cloned take a look at pigscripts/characterize_collection.pig. It loads all the data in the collection as a map, sends the map to Python (udfs/python/mongo_util.py) to gather a bunch of metadata, calculates some basic information about the collection, and then it writes the results out to an S3 bucket.
To see this script in action let’s run it on a 4 node Hadoop cluster. In your terminal (from the base directory of your mongo-pig-examples project) run:
mortar run characterize_collection --clustersize 4
This job will take about 10 minutes to finish. You can monitor the job’s status on the command line or by going to https://app.mortardata.com/jobs?
Once the job has finished, you’ll receive an email with a link to your job results. Clicking on this link will bring you into the Mortar web app, where you can download the results from s3. The output is described at the top of the characterize_collection script but as an example you can scroll down the output and find:
… user.is_translator 2 false unicode 118806 user.is_translator 2 true unicode 31 user.lang 26 en unicode 114108 user.lang 26 es unicode 3462 user.lang 26 fr unicode 532 user.lang 26 pt unicode 281 user.lang 26 ja unicode 79 user.listed_count 398 0 int 73757 user.listed_count 398 1 int 18518
Looking at the values for user.lang - we see that there are 26 unique values for the field in our dataset. The most common was “en” with 114108 occurrences, the next most common was “es” with 3462 occurrences, and so on. To see the full results without running the job you can view the output file here.
Script 2 - MongoDB Schema Generator
It can be tricky to properly declare MongoDB’s highly nested schemas in Pig. Now, Pig is graceful—it can roll without a schema, or with inconsistent, or incorrect schemas. But it’s easier to read and write your Pig code if you have a schema because it allows you (and the Pig optimizer) to focus on just the relevant data.
So this next script automatically generates a Pig schema by examining your MongoDB collection. If you don’t need the whole schema, you can easily edit it to keep just the fields you want.
Running this script is similar to running the previous one. If you ran the Characterize Collection script in the past hour, the same cluster you used for that job should still be running. In that case, you can just run:
mortar run mongo_schema_generator
If you don’t have a cluster that’s still running, just run the job on a new 4 node cluster like this:
mortar run mongo_schema_generator --clustersize 4
Script 3 – Twitter Hourly Coffee Tweets
Using a Twitter coffee tweets script (pigscripts/hourly_coffee_tweets.pig), we’re going to demonstrate how we can use a small subset of the fields in our MongoDB collection. For our example, we’ll look at how often the word “coffee” is tweeted throughout the day. As with the Mongo Schema Generator script, you can run this job on an existing cluster or start up a new one.
Next Steps
If you already have a mongo instance/cluster based in US-East EC2, the first two example scripts should run on one of your collections with only minor modifications. You’ll just need to:
- Update the MongoLoader connection strings in the pig scripts to connect to your MongoDB collections with one of your own users. If your mongo instance is on a non-standard port (any port other than 27017), just email us at support@mortardata.com to allow your Mortar account to access that port.
- If you’d like your jobs to write to one of your own S3 buckets, you can update the AWS keys associated with your Mortar account by following these instructions to enable s3 access.
- If you run out of free cluster hours with Mortar, you can upgrade your account to get additional free hours each month.
- You can find more resources for learning Pig here
- If you have any questions or feedback, please contact us at support@mortardata.com or ping us on in-app chat at app.mortardata.com
原文地址:MongoDB and Hadoop: A Step-by Step Tutorial Using , 感谢原作者分享。

Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas



Penyelesaian untuk menyelesaikan isu tamat tempoh Navicat termasuk: memperbaharui lesen dan menyahpasang semula kemas kini automatik, hubungi Navicat Premium Essentials;

Untuk pembangun bahagian hadapan, kesukaran mempelajari Node.js bergantung pada asas JavaScript mereka, pengalaman pengaturcaraan sisi pelayan, kebiasaan baris arahan dan gaya pembelajaran. Keluk pembelajaran termasuk modul peringkat permulaan dan peringkat lanjutan yang memfokuskan pada konsep asas, seni bina bahagian pelayan, penyepaduan pangkalan data dan pengaturcaraan tak segerak. Secara keseluruhan, mempelajari Node.js tidak sukar untuk pembangun yang mempunyai asas yang kukuh dalam JavaScript dan bersedia untuk melaburkan masa dan usaha, tetapi bagi mereka yang kurang pengalaman yang berkaitan, mungkin terdapat cabaran tertentu untuk diatasi.

Untuk menyambung ke MongoDB menggunakan Navicat, anda perlu: Pasang Navicat Buat sambungan MongoDB: a Masukkan nama sambungan, alamat hos dan port b Masukkan maklumat pengesahan (jika perlu) Tambah sijil SSL (jika perlu) Sahkan sambungan Simpan sambungan

Modul yang paling biasa digunakan dalam Node.js termasuk: Modul sistem fail untuk operasi fail Modul rangkaian untuk komunikasi rangkaian Modul aliran untuk memproses aliran data Modul pangkalan data untuk berinteraksi dengan pangkalan data Modul utiliti lain seperti penyulitan, rentetan pertanyaan Penghuraian rentetan dan rangka kerja HTTP

.NET 4.0 digunakan untuk mencipta pelbagai aplikasi dan ia menyediakan pemaju aplikasi dengan ciri yang kaya termasuk: pengaturcaraan berorientasikan objek, fleksibiliti, seni bina berkuasa, penyepaduan pengkomputeran awan, pengoptimuman prestasi, perpustakaan yang luas, keselamatan, Kebolehskalaan, akses data dan mudah alih sokongan pembangunan.

Untuk aplikasi Node.js, memilih pangkalan data bergantung pada keperluan aplikasi. Pangkalan data NoSQL MongoDB menyediakan fleksibiliti, Redis menyediakan konkurensi tinggi, Cassandra mengendalikan data siri masa, dan Elasticsearch dikhususkan untuk mencari. Pangkalan data SQL MySQL mempunyai prestasi cemerlang, PostgreSQL kaya dengan ciri, SQLite ringan, dan Pangkalan Data Oracle adalah komprehensif. Apabila memilih, pertimbangkan jenis data, pertanyaan, prestasi, transaksi, ketersediaan, pelesenan dan kos.

Langkah-langkah untuk menyambung ke pangkalan data dalam Node.js: Pasang pakej MySQL, MongoDB atau PostgreSQL. Buat objek sambungan pangkalan data. Buka sambungan pangkalan data dan kendalikan ralat sambungan.

Menyambung ke pangkalan data dalam Node.js memerlukan memilih sistem pangkalan data (hubungan atau bukan hubungan) dan kemudian mewujudkan sambungan menggunakan modul khusus untuk jenis itu. Modul biasa termasuk mysql (MySQL), pg (PostgreSQL), mongodb (MongoDB), dan redis (Redis). Selepas sambungan diwujudkan, anda boleh menggunakan pernyataan pertanyaan untuk mendapatkan semula data dan mengemas kini pernyataan untuk mengubah suai data. Akhir sekali, sambungan mesti ditutup apabila semua operasi selesai untuk melepaskan sumber. Tingkatkan prestasi dan keselamatan dengan mengikuti amalan terbaik ini, seperti menggunakan pengumpulan sambungan, pertanyaan berparameter dan mengendalikan ralat dengan anggun.
