static async Task GetData(string datasetId) { var client = new Knoema.Client(host, clientId, clientSecret); Console.WriteLine("Getting dataset metadata..."); var dataset = await client.GetDataset(datasetId); Console.WriteLine("Dataset has {0} dimensions: {1}.", dataset.Dimensions.Count(), string.Join(", ", dataset.Dimensions.Select(x => x.Name).ToArray())); var stub = new List <PivotRequestItem>(); foreach (var dim in dataset.Dimensions) { Console.WriteLine(string.Format("Getting \"{0}\" dataset dimension details...", dim.Name)); var dimension = await client.GetDatasetDimension(dataset.Id, dim.Id); Console.WriteLine(string.Format("Dimension \"{0}\" has {1} members: {2}.", dimension.Name, dimension.Items.Count(), string.Join(", ", dimension.Items.Select(x => x.Name.ToString()).ToArray()))); stub.Add(new PivotRequestItem() { DimensionId = dimension.Id, Members = dimension.Items.Where(x => x.HasData).Select(x => x.Key).Take(10).Cast <object>().ToList() }); } var header = new PivotRequestItem() { DimensionId = "Time" }; for (var i = 1990; i < 2010; i++) { header.Members.Add(i); } Console.WriteLine("Getting dataset data..."); var result = await client.GetData(new PivotRequest() { Dataset = datasetId, Frequencies = new List <string>() { "A" }, Stub = stub, Header = new List <PivotRequestItem>() { header } }); result.Tuples.Take(10).ToList().ForEach(x => Console.WriteLine(string.Join(", ", x.Select(y => y.Key + ":" + y.Value).ToList())) ); Console.WriteLine(string.Format("{0} rows of data have been received.", result.Tuples.Count)); }
public PivotRequest GetRequest() { var request = new PivotRequest(); request.Dataset = _dataset.Id; if (_frequencies != null) { request.Frequencies = _frequencies.ToList(); } var timeItem = new PivotRequestItem { DimensionId = "Time" }; if (string.IsNullOrEmpty(TimeRange)) { timeItem.UiMode = "allData"; timeItem.Members = null; } else { timeItem.UiMode = "range"; timeItem.Members.Add(TimeRange); } request.Header.Add(timeItem); foreach (var dimPair in _dimensions) { var dimId = dimPair.Key; var dimKeys = dimPair.Value; if (dimKeys == null || dimKeys.Count == 0) { continue; } var dimMeta = _client.GetDatasetDimension(_dataset.Id, dimId).GetAwaiter().GetResult(); object idVal; string keyVal; foreach (var member in dimMeta.Items) { if (member.Fields != null && member.Fields.TryGetValue("id", out idVal) && idVal != null && dimKeys.ContainsKey(idVal.ToString())) { keyVal = idVal.ToString(); } else if (dimKeys.ContainsKey(member.Name)) { keyVal = member.Name; } else if (dimKeys.ContainsKey(member.Key.ToString())) { keyVal = member.Key.ToString(); } else { keyVal = null; } if (!string.IsNullOrEmpty(keyVal)) { dimKeys[keyVal] = member.Key; } } PivotRequestItem dimItem = null; foreach (var memberKey in dimKeys.Values) { if (memberKey != 0) { if (dimItem == null) { dimItem = new PivotRequestItem { DimensionId = dimId }; request.Stub.Add(dimItem); } dimItem.Members.Add(memberKey); } } } return(request); }
static void Main(string[] args) { var client = new Knoema.Client( ConfigurationManager.AppSettings["host"], ConfigurationManager.AppSettings["clientId"], ConfigurationManager.AppSettings["appSecret"]); Console.WriteLine("Getting dataset metadata..."); client.GetDataset("gquvbhe").ContinueWith(d => { Console.WriteLine("Dataset has {0} dimensions: {1}.", d.Result.Dimensions.Count(), string.Join(", ", d.Result.Dimensions.Select(x => x.Name).ToArray())); var stub = new List <PivotRequestItem>(); foreach (var dim in d.Result.Dimensions) { Console.WriteLine(string.Format("Getting \"{0}\" dataset dimension details...", dim.Name)); client.GetDatasetDimension(d.Result.Id, dim.Id).ContinueWith(t => { Console.WriteLine(string.Format("Dimension \"{0}\" has {1} members: {2}.", t.Result.Name, t.Result.Items.Count(), string.Join(", ", t.Result.Items.Select(x => x.Name.ToString()).ToArray()))); stub.Add(new PivotRequestItem() { DimensionId = t.Result.Id, Members = t.Result.Items.Where(x => x.HasData).Select(x => x.Key).Take(10).Cast <object>().ToList() }); }).Wait(); } var header = new PivotRequestItem() { DimensionId = "Time" }; for (var i = 1990; i < 2010; i++) { header.Members.Add(i); } Console.WriteLine("Getting dataset data..."); var pivotRequest = new PivotRequest() { Dataset = "gquvbhe", Frequencies = new List <string>() { "A" }, Stub = stub, Header = new List <PivotRequestItem>() { header } }; client.GetData(pivotRequest).ContinueWith(t => { t.Result.Tuples.Take(10).ToList().ForEach(x => Console.WriteLine(string.Join(", ", x.Select(y => y.Key + ":" + y.Value).ToList())) ); Console.WriteLine(string.Format("{0} rows of data have been received.", t.Result.Tuples.Count)); Console.WriteLine("Press any key to exit."); Console.ReadKey(); }).Wait(); }).Wait(); }