public static async Task AggregateGdpPopAsync(string filename)
        {
            Task <string> csvdatatask    = IOOperations.ReadfileAsync(filename);
            Task <string> mapperdatatask = IOOperations.ReadfileAsync(@"../../../../AggregateGDPPopulation/data/countriesmap.txt");
            await         mapperdatatask;
            string        mapperdata = mapperdatatask.Result;

            // making mapper dictionary
            string[] countrymap = mapperdata.Split('\n');
            Dictionary <string, string> mapper = new Dictionary <string, string>();

            foreach (string str in countrymap)
            {
                string[] row = str.Split(',');
                mapper[row[0]] = row[1];
            }

            await  csvdatatask;
            string csvdata = csvdatatask.Result;

            // making csv data
            string[] data         = csvdata.Split('\n');
            string[] headers      = data[0].Split(',');
            int      indexcountry = Array.IndexOf(headers, "\"Country Name\"");
            int      indexgdp     = Array.IndexOf(headers, "\"GDP Billions (USD) 2012\"");
            int      indexpop     = Array.IndexOf(headers, "\"Population (Millions) 2012\"");

            Dictionary <string, POPGDPObject> finalobjects = new Dictionary <string, POPGDPObject>();

            // aggregating data
            try
            {
                for (int i = 1; i < data.Length; i++)
                {
                    string[] datarow = data[i].Replace("\"", "").Split(',');

                    if (!finalobjects.ContainsKey(mapper[datarow[indexcountry]]))
                    {
                        finalobjects[mapper[datarow[indexcountry]]] = new POPGDPObject();
                        finalobjects[mapper[datarow[indexcountry]]]
                        .GDP_2012 = float.Parse(datarow[indexgdp]);
                        finalobjects[mapper[datarow[indexcountry]]]
                        .POPULATION_2012 = float.Parse(datarow[indexpop]);
                    }
                    else
                    {
                        finalobjects[mapper[datarow[indexcountry]]]
                        .GDP_2012 += float.Parse(datarow[indexgdp]);
                        finalobjects[mapper[datarow[indexcountry]]]
                        .POPULATION_2012 += float.Parse(datarow[indexpop]);
                    }
                }
            }
            catch (Exception) { }

            // writing to json
            await IOOperations.WritefileAsync(@"../../../../AggregateGDPPopulation/data/output.json", finalobjects);
        }
示例#2
0
        public async Task performAggregateOperation()
        {
            Task <List <string> > CsvData       = IOOperations.ReadFileByLineAsync("../../../../AggregateGDPPopulation/data/datafile.csv");
            Task <string>         MapDataString = IOOperations.ReadFileToEndAsync("../../../../AggregateGDPPopulation/data/countrytocontinentmap.json");

            List <string> CsvDataList = await CsvData;

            string[] CsvDataHeaders  = CsvDataList[0].Replace("\"", string.Empty).Split(',');
            int      CountryIndex    = Array.IndexOf(CsvDataHeaders, "Country Name");
            int      PopulationIndex = Array.IndexOf(CsvDataHeaders, "Population (Millions) 2012");
            int      GDPIndex        = Array.IndexOf(CsvDataHeaders, "GDP Billions (USD) 2012");

            CsvDataList.Remove(CsvDataList[0]);
            List <string[]>   CsvDataListSplitByComma = new List <string[]>();
            AddOrUpdateOutput output = new AddOrUpdateOutput();

            foreach (string item in CsvDataList)
            {
                CsvDataListSplitByComma.Add(item.Replace("\"", string.Empty).Split(','));
            }

            string MapData            = await MapDataString;
            var    CountryContinetMap = JSONOperations.JSONDeserialize(MapData);

            foreach (string[] row in CsvDataListSplitByComma)
            {
                float Population = float.Parse(row[PopulationIndex]);
                float GDP        = float.Parse(row[GDPIndex]);
                try
                {
                    string Continent = CountryContinetMap.GetValue(row[CountryIndex]).ToString();
                    output.AddOrUpdate(Continent, Population, GDP);
                }
                catch (Exception) { }
            }

            string Output = JSONOperations.JSONSerialize(output.AggregateOutput);
            await IOOperations.WriteToFileAsync("../../../../AggregateGDPPopulation/output/output.json", Output);
        }