Project: YugabyteDB (App Definition and Development / Database)
File: imdb-data.md

title: Computing Bacon Numbers for data form the IMDb headerTitle: Computing Bacon Numbers for real IMDb data linkTitle: Bacon numbers for IMDb data description: This section shows how to compute Bacon Numbers for real IMDb data. menu: v2.18: identifier: imdb-data parent: bacon-numbers weight: 20 type: docs {{< tip title="Download a zip of scripts that include all the code examples that implement this case study" >}} All of the `.sql` scripts that this section presents for copy-and-paste at the `ysqlsh` prompt are included for download in a zip-file. . After unzipping it on a convenient new directory, you'll see a `README.txt`. It tells you how to start the Bacon Numbers master-script. This invokes all the child scripts that: (1) ingest the `imdb.small.txt` data set from the Oberlin College Computer Science department's ; and (2) solve the Bacon Numbers problem using this data set. (The `imdb.small.txt` file is included in the `.zip`.) Simply start it in `ysqlsh`. You can run it time and again. It always finishes silently. You can see the report that it produces on a dedicated spool directory and confirm that your reports are identical to the reference copies that are delivered in the zip-file. {{< /tip >}} Before trying the code in this section, make sure that you have created the supporting infrastructure: The "actors", "movies", and "castmembers"_ tables for representing IMDb datasee The "edges" table and the procedure to populate it from the "castmembers"_ tablesee All the code shown in the section . Be sure to chose the option. Use Internet search for "download IMDb data for Bacon Numbers". This brings you to useful subsets of the vast, entire, IMDb data set that have been prepared by, for example, university Computer Science departments specifically to support Bacon Numbers course assignments. The is a good source. Go to and look for this: ": a 1817 line file with just a handful of performers (161), fully connected" Here are a few lines from the file: ``` ... Alec Guinness|Kind Hearts and Coronets (1949) Alec Guinness|Ladykillers, The (1955) Alec Guinness|Last Holiday (1950) Alec Guinness|Lavender Hill Mob, The (1951) Alec Guinness|Lawrence of Arabia (1962) Alec Guinness|Little Dorrit (1988) Alec Guinness|Lovesick (1983) ... ``` The information content is identical to what the "castmembers"_ table was designed to represent. (See the script.) This means that the `ysqlsh` meta-command `\COPY` can be used, straightforwardly, to ingest the downloaded

However, as this script creates it, the "actors" and "movies" tables must be populated first so that the foreign key constraints to these from the "castmembers"_ table will be satisfied when it its populated. Use this script to do the steps in the proper order and display the resulting table counts: ```plpgsql delete from edges; delete from cast_members; delete from actors; delete from movies; drop table if exists cast_members cascade; create table cast_members( actor text not null, movie text not null, constraint imdbfactspk primary key(actor, movie)); \copy cast_members(actor, movie) from 'imdb.small.txt' with delimiter '|'; insert into actors select distinct actor from cast_members; insert into movies select distinct movie from cast_members; alter table cast_members add constraint castmembersfk1 foreign key (actor) references actors(actor) match full on delete cascade on update restrict; alter table cast_members add constraint castmembersfk2 foreign key(movie) references movies(movie) match full on delete cascade on update restrict; \t on select 'count() from cast_members... '||to_char(count(), '9,999') from cast_members; select 'count() from actors......... '||to_char(count(), '9,999') from actors; select 'count() from movies......... '||to_char(count(), '9,999') from movies; \t off ``` This is the result: ``` count(*) from cast_members... 1,817 count(*) from actors......... 161 count(*) from

1,586 ``` Populate the "edges" table and inspect a sample of the results: ```plpgsql call insert_edges(); with v(actor) as ( select node_1 from edges union select node_2 from edges) select actor from v order by 1 limit 10; select distinct unnest(movies) as movie from edges order by 1 limit 10; select node_1, node_2, replace(translate(movies::text, '{"}', ''), ',', ' | ') as movies from edges where node1 < node2 order by 1, 2 limit 10; select node_1, node_2, replace(translate(movies::text, '{"}', ''), ',', ' | ') as movies from edges where node1 > node2 order by 2, 1 limit 10; ``` Here are the results of all the queries: ``` actor Adam Sandler (I) Al Pacino Alan Rickman Alec Guinness Alireza Shayegan Allen Covert Alyson Hannigan Amir Ashayeri Andy Dick Ann Pala movie 180: Christopher Nolan Interviews Al Pacino (2002) Actor's Notebook: Christopher Lee (2002) Adam Sandler Goes to Hell (2001) Airport '77 (1977) America's Sweethearts (2001) Austin Powers: International Man of Mystery (1997) Bandits (2001) Batman Begins (2005) Being John Malkovich (1999) Big Parade of Comedy, The (1964) node1 | node2 | movies +--+ Adam Sandler (I) | Allen Covert | Adam Sandler Goes to Hell (2001) Adam Sandler (I) | Ann Pala | Adam Sandler Goes to Hell (2001) Adam Sandler (I) | Betsy Asher Hall | Adam Sandler Goes to Hell (2001) Adam Sandler (I) | Bill Murray (I) | Saturday Night Live Christmas (1999) | Saturday Night Live: Game Show Parodies (1998) Adam Sandler (I) | Billy Bob Thornton | Going Overboard (1989) Adam Sandler (I) | Billy Crystal | Saturday Night Live: Game Show Parodies (1998) Adam Sandler (I) | Blake Clark | Adam Sandler Goes to Hell (2001) Adam Sandler (I) | Craig A. Mumma | Adam Sandler Goes to Hell (2001) Adam Sandler (I) | Dana Carvey | Adam Sandler Goes to Hell (2001) Adam Sandler (I) | David Sosalla | Adam Sandler Goes to Hell (2001) node1 | node2 | movies --++ Allen Covert | Adam Sandler (I) | Adam Sandler Goes to Hell (2001) Ann Pala | Adam Sandler (I) | Adam Sandler Goes to Hell (2001) Betsy Asher Hall | Adam Sandler (I) | Adam Sandler Goes to Hell (2001) Bill Murray (I) | Adam Sandler (I) | Saturday Night Live Christmas (1999) | Saturday Night Live: Game Show Parodies (1998) Billy Bob Thornton | Adam Sandler (I) | Going Overboard (1989) Billy Crystal | Adam Sandler (I) | Saturday Night Live: Game Show Parodies (1998) Blake Clark | Adam Sandler (I) | Adam Sandler Goes to Hell (2001) Craig A. Mumma | Adam Sandler (I) | Adam Sandler Goes to Hell (2001) Dana Carvey | Adam Sandler (I) | Adam Sandler Goes to Hell (2001) David Sosalla | Adam Sandler (I) | Adam Sandler Goes to Hell (2001) ``` Invoke the implementation of "findpaths()" that will be able to handle the real IMDd data that is shown at . Invoke it with_ early pruning: ```plpgsql call find_paths('Kevin Bacon (I)', true); ``` Count the total number of paths that were found: ```plpgsql \t on select 'total number of pruned paths: '||count(*)::text from raw_paths; \t off ``` This is the result. ``` total number of pruned paths: 160 ``` You might like to use "listpaths)"_ to list them all. See what effect "restricttounqcontainingpaths()" has: ```plpgsql call restricttounqcontainingpaths('rawpaths', 'unqcontaining_paths'); \t on select 'total number of unique containing paths: '||count(*)::text from unqcontainingpaths; \t off ``` This is the result. ``` total number of unique containing paths: 144 ``` Ensure that you have created the "decoratedpathsreport()" table function using the script. Invoke it thus to list