Created
May 10, 2017 17:25
-
-
Save cjkoester/569455ba4f8c0dd11a71fa23f14280e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.Collections.Generic; | |
using System.Web.Script.Serialization; | |
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] | |
public class ScriptMain : UserComponent | |
{ | |
string json; | |
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); | |
public override void PreExecute() | |
{ | |
base.PreExecute(); | |
string url = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.geojson"; ; | |
json = DownloadJson(url); | |
} | |
public override void CreateNewOutputRows() | |
{ | |
// Convert json string to .net object using the old school JavaScriptSerializer class | |
JavaScriptSerializer serialize = new JavaScriptSerializer(); | |
Earthquakes earthquakes = (Earthquakes)serialize.Deserialize(json, typeof(Earthquakes)); | |
// Loop through array of earthquakes, outputing desired values to SSIS buffer | |
foreach (var feature in earthquakes.features) | |
{ | |
Output0Buffer.AddRow(); | |
Output0Buffer.FeatureID = feature.id; | |
Output0Buffer.DateTimeEpoch = feature.properties.time; | |
Output0Buffer.DateTimeLocal = epoch.AddMilliseconds(feature.properties.time).ToLocalTime(); | |
Output0Buffer.Magnitude = feature.properties.mag; | |
Output0Buffer.Place = feature.properties.place; | |
Output0Buffer.Longitude = feature.geometry.coordinates[0]; | |
Output0Buffer.Latitude = feature.geometry.coordinates[1]; | |
Output0Buffer.Depth = feature.geometry.coordinates[2]; | |
} | |
} | |
public static string DownloadJson(string downloadURL) | |
{ | |
using (WebClient client = new WebClient()) | |
{ | |
return client.DownloadString(downloadURL); | |
} | |
} | |
public class Earthquakes | |
{ | |
public string type { get; set; } | |
public Metadata metadata { get; set; } | |
public List<Feature> features { get; set; } | |
public List<decimal> bbox { get; set; } | |
} | |
public class Metadata | |
{ | |
public long generated { get; set; } | |
public string url { get; set; } | |
public string title { get; set; } | |
public int status { get; set; } | |
public string api { get; set; } | |
public int count { get; set; } | |
} | |
public class Feature | |
{ | |
public string type { get; set; } | |
public Properties properties { get; set; } | |
public Geometry geometry { get; set; } | |
public string id { get; set; } | |
} | |
public class Properties | |
{ | |
public decimal mag { get; set; } | |
public string place { get; set; } | |
public long time { get; set; } | |
public object updated { get; set; } | |
public int tz { get; set; } | |
public string url { get; set; } | |
public string detail { get; set; } | |
public int? felt { get; set; } | |
public decimal? cdi { get; set; } | |
public object mmi { get; set; } | |
public object alert { get; set; } | |
public string status { get; set; } | |
public int tsunami { get; set; } | |
public int sig { get; set; } | |
public string net { get; set; } | |
public string code { get; set; } | |
public string ids { get; set; } | |
public string sources { get; set; } | |
public string types { get; set; } | |
public object nst { get; set; } | |
public decimal? dmin { get; set; } | |
public decimal? rms { get; set; } | |
public int? gap { get; set; } | |
public string magType { get; set; } | |
public string type { get; set; } | |
public string title { get; set; } | |
} | |
public class Geometry | |
{ | |
public string type { get; set; } | |
public List<decimal> coordinates { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment