15. Importing Data

The dusty archives

It was now time to pull the data from themoviedb.org. Registering there and getting an API key was simple, as was using the API on the command-line with curl. Looking at the JSON returned for movies and people, we decided to enhance our domain model and add some more fields to enrich the UI.

Example 15.1. JSON movie response

"translated":true, "adult":false, "language":"en",
"original_name":"[Rec]", "name":"[Rec]", "alternative_name":"[REC]",
"id":8329, "imdb_id":"tt1038988", "url":"http://www.themoviedb.org/movie/8329",
"votes":11, "rating":7.2,
"tagline":"One Witness. One Camera",
"overview":"\"REC\" turns on a young TV reporter and her cameraman who cover the night shift
 at the local fire station...
"keywords":["terror", "lebende leichen", "obsession", "camcorder", "firemen", "reality tv ",
 "bite", "cinematographer",
"attempt to escape", "virus", "lodger", "live-reportage", "schwerverletzt"],
"studios":[{"url":"http://www.themoviedb.org/company/2270", "name":"Filmax Group", "id":2270}],
"languages_spoken":[{"code":"es", "name":"Spanish", "native_name":"Espa\u00f1ol"}],
"countries":[{"code":"ES", "name":"Spain", "url":"http://www.themoviedb.org/country/es"}],
"size":"original", "height":1000, "width":706,
"cast":[{"name":"Manuela Velasco",
"job":"Actor", "department":"Actors",
"character":"Angela Vidal",
"id":34793, "order":0, "cast_id":1,
{"name":"Gl\u00f2ria Viguer",
"job":"Costume Design", "department":"Costume \u0026 Make-Up",
"id":54531, "order":0, "cast_id":21,
"version":150, "last_modified_at":"2011-02-20 23:16:57"}]

Example 15.2. JSON actor response

"name":"Glenn Strange", "known_as":[{"name":"George Glenn Strange"}, {"name":"Glen Strange"},
{"name":"Glen 'Peewee' Strange"}, {"name":"Peewee Strange"}, {"name":"'Peewee' Strange"}],
"birthday":"1899-08-16", "birthplace":"Weed, New Mexico, USA",
"filmography":[{"name":"Bud Abbott Lou Costello Meet Frankenstein",
"job":"Actor", "department":"Actors",
"character":"The Frankenstein Monster",
"adult":false, "release":"1948-06-15"},
"version":19, "last_modified_at":"2011-03-07 13:02:35"}]

For the import process we created a separate importer using Jackson (a JSON library) to fetch and parse the data, and then some transactional methods in the MovieDbImportService to actually import it as movies, roles, and actors. The importer used a simple caching mechanism to keep downloaded actor and movie data on the filesystem, so that we didn't have to overload the remote API. In the code below you can see that we've changed the actor to a person so that we can also accommodate the other folks that participate in movie production.

Example 15.3. Importing the data

public Movie importMovie(String movieId) {
  Movie movie = movieRepository.findById(movieId);
  if (movie == null) { // Not found: Create fresh
      movie = new Movie(movieId,null);

  Map data = loadMovieData(movieId);
  if (data.containsKey("not_found")) throw
     new RuntimeException("Data for Movie "+movieId+" not found.");
  movieDbJsonMapper.mapToMovie(data, movie);
  relatePersonsToMovie(movie, data);
  return movie;

private void relatePersonsToMovie(Movie movie, Map data) {
  Collection<Map> cast = (Collection<Map>) data.get("cast");
  for (Map entry : cast) {
    String id = "" + entry.get("id");
    String jobName = (String) entry.get("job");
    Roles job = movieDbJsonMapper.mapToRole(jobName);
    if (job==null) {
    switch (job) {
      case DIRECTED:
        final Director director = doImportPerson(id, new Director(id));
      case ACTS_IN:
        final Actor actor = doImportPerson(id, new Actor(id));
        actor.playedIn(movie, (String) entry.get("character"));

public void mapToMovie(Map data, Movie movie) {
  movie.setTitle((String) data.get("name"));
  movie.setLanguage((String) data.get("language"));
  movie.setTagline((String) data.get("tagline"));
  movie.setReleaseDate(toDate(data, "released", "yyyy-MM-dd"));
  movie.setImageUrl(selectImageUrl((List<Map>) data.get("posters"), "poster", "mid"));


The last part involved adding a protected URI to the MovieController to allow importing ranges of movies. During testing, it became obvious that the calls to themoviedb.org were a limiting factor. As soon as the data was stored locally, the Neo4j import was a sub-second deal.