public async Task UpsertMultipleEdges()
        {
            var movies = _movies.Take(MOVIES_TO_TEST).ToDictionary(k => k.TmdbId);
            var cast   = movies.SelectMany(m => _cast[m.Key]).ToList();

            var edges = cast.Select(c => new EdgeDefinition(new MovieCastEdge()
            {
                Order = c.Order
            }, _cosmosClient.CosmosSerializer.ToGraphItemBase(movies[c.MovieId]), _cosmosClient.CosmosSerializer.ToGraphItemBase(c), true)).ToList();
            var upsertEdges = await _cosmosClient.UpsertEdges(edges, (partial) => { Console.WriteLine($"upserted {partial.Count()} edges"); });
        }
Example #2
0
        public async Task LoadEdges(string parameter)
        {
            var moviesCsv = DataLoader.LoadMovies();

            //Parse parameters
            var args = Parse2intParameters(parameter, moviesCsv.Count());

            moviesCsv = moviesCsv.Take(args.records).ToList();

            var castCsv  = DataLoader.LoadCast().GroupBy(c => c.MovieId).ToDictionary(k => k.Key, v => v);
            var movies   = moviesCsv.Select(m => m.ToMovie()).ToDictionary(m => m.TmdbId);
            var cast     = moviesCsv.SelectMany(m => castCsv[m.TmdbId].Select(c => c.ToCast())).ToList();
            var actors   = cast.AllActors().ToDictionary(a => a.Name);
            var genres   = moviesCsv.AllGenres().ToDictionary(g => g.Genre);
            var keywords = moviesCsv.AllKeywords().ToDictionary(k => k.Keyword);

            //Generate Keyword edges. Each movie has multiple keywords so for each movie we need to generate one for each keyword it has.
            var keywordEdgeDefinitions = movies.Values.SelectMany(m =>
            {
                var mgib = _graphClient.CosmosSerializer.ToGraphItemBase(m);
                return(m.Keywords.Select(k => new EdgeDefinition(new MovieKeywordEdge(), mgib, _graphClient.CosmosSerializer.ToGraphItemBase(keywords[k]), true)));
            }).ToArray();

            var startTime = DateTime.Now;

            ConsoleHelpers.ConsoleLine($"Inserting {keywordEdgeDefinitions.Count()} keyword Edges (using {args.threads} threads)...");
            var upsertKeywordEdges = await _graphClient.UpsertEdges(keywordEdgeDefinitions, (res) => { ConsoleHelpers.ConsoleLine($"processed {res.Count()}/{keywordEdgeDefinitions.Count()} keyword Edges"); }, threads : args.threads);

            ConsoleHelpers.PrintStats(upsertKeywordEdges, DateTime.Now.Subtract(startTime).TotalSeconds);
            //==================


            //Generate genre edges. Each movie has multiple genres so for each movie we need to generate one for each genre it has.
            var genreEdgeDefinitions = movies.Values.SelectMany(m =>
            {
                var mgib = _graphClient.CosmosSerializer.ToGraphItemBase(m);
                return(m.Genres.Where(g => !string.IsNullOrEmpty(g)).Select(g => new EdgeDefinition(new MovieGenreEdge(), mgib, _graphClient.CosmosSerializer.ToGraphItemBase(genres[g]), true)));
            }).ToArray();

            var startTime2 = DateTime.Now;

            ConsoleHelpers.ConsoleLine($"Inserting {genreEdgeDefinitions.Count()} genre Edges (using {args.threads} threads)...");
            var upsertGenreEdges = await _graphClient.UpsertEdges(genreEdgeDefinitions, (res) => { ConsoleHelpers.ConsoleLine($"processed {res.Count()}/{genreEdgeDefinitions.Count()} genre Edges"); }, threads : args.threads);

            ConsoleHelpers.PrintStats(upsertGenreEdges, DateTime.Now.Subtract(startTime2).TotalSeconds);
            //==================


            //Generate reverse genre edges. Each movie has multiple genres so for each movie we need to generate one for each genre it has.
            var genreEdge2Definitions = movies.Values.SelectMany(m =>
            {
                var mgib = _graphClient.CosmosSerializer.ToGraphItemBase(m);
                return(m.Genres.Where(g => !string.IsNullOrEmpty(g)).Select(g => new EdgeDefinition(new MovieCastEdge(), _graphClient.CosmosSerializer.ToGraphItemBase(genres[g]), mgib, true)));
            }).ToArray();

            var startTime5 = DateTime.Now;

            ConsoleHelpers.ConsoleLine($"Inserting {genreEdge2Definitions.Count()} genre Edges (using {args.threads} threads)...");
            var upsertGenre2Edges = await _graphClient.UpsertEdges(genreEdge2Definitions, (res) => { ConsoleHelpers.ConsoleLine($"processed {res.Count()}/{genreEdge2Definitions.Count()} genre Edges"); }, threads : args.threads);

            ConsoleHelpers.PrintStats(upsertGenre2Edges, DateTime.Now.Subtract(startTime5).TotalSeconds);
            //==================



            //Generate movieCast edges. Each movie has multiple keywords so for each movie we need to generate one for each genre it has.
            var castEdgeDefinitions = cast.Select(c =>
            {
                return(new EdgeDefinition(new MovieCastEdge(), _graphClient.CosmosSerializer.ToGraphItemBase(movies[c.MovieId]), _graphClient.CosmosSerializer.ToGraphItemBase(c), true));
            }).ToArray();

            var startTime3 = DateTime.Now;

            ConsoleHelpers.ConsoleLine($"Inserting {castEdgeDefinitions.Count()} cast Edges (using {args.threads} threads)...");
            var upsertCastEdges = await _graphClient.UpsertEdges(castEdgeDefinitions, (res) => { ConsoleHelpers.ConsoleLine($"processed {res.Count()}/{castEdgeDefinitions.Count()} cast Edges"); }, threads : args.threads);

            ConsoleHelpers.PrintStats(upsertCastEdges, DateTime.Now.Subtract(startTime3).TotalSeconds);
            //==================


            //Generate actor edges. Each movie has multiple keywords so for each movie we need to generate one for each genre it has.
            var actorEdgeDefinitions = cast.Select(c =>
            {
                return(new EdgeDefinition(new ActorCastEdge(), _graphClient.CosmosSerializer.ToGraphItemBase(actors[c.ActorName]), _graphClient.CosmosSerializer.ToGraphItemBase(c), true));
            }).ToArray();

            var startTime4 = DateTime.Now;

            ConsoleHelpers.ConsoleLine($"Inserting {actorEdgeDefinitions.Count()} actor Edges (using {args.threads} threads)...");
            var upsertActorEdges = await _graphClient.UpsertEdges(actorEdgeDefinitions, (res) => { ConsoleHelpers.ConsoleLine($"processed {res.Count()}/{actorEdgeDefinitions.Count()} actor Edges"); }, threads : args.threads);

            ConsoleHelpers.PrintStats(upsertActorEdges, DateTime.Now.Subtract(startTime4).TotalSeconds);
            //==================
        }