Files and Directories Used in this Exercise
In this exercise, you will practice data processing in Hadoop using Hive.
Lab Experiment
The data sets for this exercise are the movie and movierating data imported from MySQL into Hadoop in the "Importing Data with Sqoop" exercise.
Review the Data
1. Make sure you've completed the "Importing Data with Sqoop" exercise. Review the data you already loaded into HDFS in that exercise:
Prepare The Data For Hive
For Hive data sets, you create tables, which attach field names and data types to your Hadoop data for subsequent queries. You can create external tables on themovie and movierating data sets, without having to move the data at all. Prepare the Hive tables for this exercise by performing the following steps:
1. Invoke the Hive shell.
2. Create the movie table:
3. Create the movierating table:
4. Quit the Hive shell.
Practicing HiveQL
If you are familiar with SQL, most of what you already know is applicably to HiveQL. Skip ahead to section called "The Questions" later in this exercise, and see if you can solve the problems based on your knowledge of SQL.
If you are unfamiliar with SQL, follow the steps below to learn how to use HiveSQL to solve problems.
1. Start the Hive shell.
2. Show the list of tables in Hive
3. View the metadata for the two tables you created previously:
Hint: You can use the up and down arrow keys to see and edit your command history in the hive shell, just as you can in the Linux command shell.
4. The SELECT * FROM TABLENAME command allows you to query data from a table. Although it is very easy to select all the rows in a table, Hadoop generally deals with very large tables; so it is best to limit how many you select. Use LIMIT to view only the first N rows:
5. Use the WHERE clause to select only rows that match certain criteria. For example, select movies released before 1930:
6. The results include movies whose year field is 0, meaning that the year is unknown or unavailable. Exclude those movies from the results:
7. The results now correctly include movies before 1930, but the list is unordered. Order them alphabetically by title:
8. Now let's move on to the movierating table. List all the ratings by a particular user, e.g.
9. SELECT * shows all the columns, but as we've already selected by userid, display the other columns but not that one:
10. Use the JOIN function to display data from both tables. For example, include the name of the movie (from the movie table) in the list of a user's ratings:
11. How tough a rater is user 149? Find out by calculating the average rating she gave to all movies using the AVG function (UDAV):
12. List each user who rated movies, the number of movies they've rated, and their average rating.
13. Take the same data, and copy it into a new table called userrating.
The Questions
Now that the data is imported and suitably prepared, write a HiveQL command to implement each of the following queries.
Supplement
* Apache Hive - LanguageManual
沒有留言:
張貼留言