【发布时间】:2021-03-14 12:58:36
【问题描述】:
我使用 SSIS 中的脚本任务创建了 Web API,该任务使用本教程将 JSON 格式的天气数据检索到 SQL 数据库表中:Weather API SSIS。在本教程示例中,只有一组坐标被使用,它在数据库表中为我们提供了 JSON 对象的一行。
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Net;
#endregion
namespace ST_6f60bececd8f4f94afaf758869590918
{
/// <summary>
/// ScriptMain is the entry point class of the script. Do not change the name, attributes,
/// or parent of this class.
/// </summary>
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region Help: Using Integration Services variables and parameters in a script
/* To use a variable in this script, first ensure that the variable has been added to
* either the list contained in the ReadOnlyVariables property or the list contained in
* the ReadWriteVariables property of this script task, according to whether or not your
* code needs to write to the variable. To add the variable, save this script, close this instance of
* Visual Studio, and update the ReadOnlyVariables and
* ReadWriteVariables properties in the Script Transformation Editor window.
* To use a parameter in this script, follow the same steps. Parameters are always read-only.
*
* Example of reading from a variable:
* DateTime startTime = (DateTime) Dts.Variables["System::StartTime"].Value;
*
* Example of writing to a variable:
* Dts.Variables["User::myStringVariable"].Value = "new value";
*
* Example of reading from a package parameter:
* int batchId = (int) Dts.Variables["$Package::batchId"].Value;
*
* Example of reading from a project parameter:
* int batchId = (int) Dts.Variables["$Project::batchId"].Value;
*
* Example of reading from a sensitive project parameter:
* int batchId = (int) Dts.Variables["$Project::batchId"].GetSensitiveValue();
* */
#endregion
#region Help: Firing Integration Services events from a script
/* This script task can fire events for logging purposes.
*
* Example of firing an error event:
* Dts.Events.FireError(18, "Process Values", "Bad value", "", 0);
*
* Example of firing an information event:
* Dts.Events.FireInformation(3, "Process Values", "Processing has started", "", 0, ref fireAgain)
*
* Example of firing a warning event:
* Dts.Events.FireWarning(14, "Process Values", "No values received for input", "", 0);
* */
#endregion
#region Help: Using Integration Services connection managers in a script
/* Some types of connection managers can be used in this script task. See the topic
* "Working with Connection Managers Programatically" for details.
*
* Example of using an ADO.Net connection manager:
* object rawConnection = Dts.Connections["Sales DB"].AcquireConnection(Dts.Transaction);
* SqlConnection myADONETConnection = (SqlConnection)rawConnection;
* //Use the connection in some code here, then release the connection
* Dts.Connections["Sales DB"].ReleaseConnection(rawConnection);
*
* Example of using a File connection manager
* object rawConnection = Dts.Connections["Prices.zip"].AcquireConnection(Dts.Transaction);
* string filePath = (string)rawConnection;
* //Use the connection in some code here, then release the connection
* Dts.Connections["Prices.zip"].ReleaseConnection(rawConnection);
* */
#endregion
/// <summary>
/// This method is called when this script task executes in the control flow.
/// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
/// To open Help, press F1.
/// </summary>
public void Main()
{
string Longitude = (string)Dts.Variables["User::Longitude"].Value.ToString();
string Latitude = (string)Dts.Variables["User::Latitude"].Value.ToString();
string municipality = (string)Dts.Variables["User::Municipality"].Value.ToString();
MessageBox.Show("Longitude:" + Longitude + ", Latitude:" + Latitude);
var url = @"https://api.met.no/weatherapi/locationforecast/2.0/compact.json?lat="+Latitude+"&lon="+Longitude+"";
System.Net.ServicePointManager.DefaultConnectionLimit = int.MaxValue;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.UseDefaultCredentials = true;
req.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
var syncClient = new WebClient();
syncClient.Headers.Add("user-agent", "acmeweathersite.com support@acmeweathersite.com");
var content = syncClient.DownloadString(url);
string connectionString = "Data Source=localhost;Initial Catalog=Weather;Integrated Security=True;";
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand Storproc =
new SqlCommand(@"INSERT INTO [dbo].[Weather] (JSONData)
select @JSONData", conn);
Storproc.Parameters.AddWithValue("@JSONData", content.ToString());
conn.Open();
Storproc.ExecuteNonQuery();
conn.Close();
}
// TODO: Add your code here
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
///
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
之后,我获取从上面发布的脚本任务代码中检索到的 JSON 对象,并将其插入到 sql 数据表格式的新表中:
declare @JSON nvarchar(max)
select @JSON = [JSONData]
FROM [Weather].[dbo].[Weather]
;with cteTemp as
(SELECT *
FROM OPENJSON(@json, '$.properties.timeseries')
WITH (
time datetime '$.time',
air_temperature nvarchar(50) '$.data.instant.details.air_temperature',
wind_speed nvarchar(50) '$.data.instant.details.wind_speed',
precipitation_amount_next_1_hour nvarchar(50) '$.data.next_1_hours.details.precipitation_amount',
symbol_code_next_1_hour nvarchar(50) '$.data.next_1_hours.summary.symbol_code',
precipitation_amount_next_6_hour nvarchar(50) '$.data.next_6_hours.details.precipitation_amount',
symbol_code_next_6_hour nvarchar(50) '$.data.next_6_hours.summary.symbol_code'
)
)
insert into [dbo].[WeatherByHour]([time],[air_temperature],wind_speed,precipitation_amount_next_1_hour,symbol_code_next_1_hour,precipitation_amount_next_6_hour,symbol_code_next_6_hour)
select [time],
[air_temperature],
wind_speed,
precipitation_amount_next_1_hour,
symbol_code_next_1_hour,
precipitation_amount_next_6_hour,
symbol_code_next_6_hour
from cteTemp;
现在下一步是创建具有多个坐标的表,我将在脚本任务中将其用作参数变量并添加一个 foreach 循环:
这将为我的坐标表中存在的每组坐标提供 3 行 JSON 对象,这些坐标在 URL API 中用作可变参数:
我现在遇到的问题是,当我将 JSON 对象插入到 WeatherByHour 表中时,它仅使用 JSON 对象的第一行和第一组坐标,在我的示例中为我提供了 83 行但理想情况下我会喜欢它产生 83 + 83 + 83 = 249 行。这里最好的解决方案是什么,在脚本任务中创建一个大的 JSON 数组对象,或者以某种方式遍历 Weather 表中的所有 JSON 对象行并将它们插入 WeatherByHour 表中?
这些是我的示例中使用的所有表:
USE [Weather]
GO
/****** Object: Table [dbo].[Weather] Script Date: 2021-03-14 13:55:44 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Weather](
[JSONData] [nvarchar](max) NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
USE [Weather]
GO
/****** Object: Table [dbo].[WeatherByHour] Script Date: 2021-03-14 13:56:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[WeatherByHour](
[id] [int] IDENTITY(1,1) NOT NULL,
[time] [datetime] NULL,
[air_temperature] [nvarchar](50) NULL,
[wind_speed] [nvarchar](50) NULL,
[precipitation_amount_next_1_hour] [nvarchar](50) NULL,
[symbol_code_next_1_hour] [nvarchar](50) NULL,
[precipitation_amount_next_6_hour] [nvarchar](50) NULL,
[symbol_code_next_6_hour] [nvarchar](50) NULL,
CONSTRAINT [PK_WeatherByHour] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
create table Coordinates(
Municipality nvarchar(50),
Latitide nvarchar(50),
Longitude nvarchar(50)
)
INSERT INTO Coordinates (Municipality, Latitide, Longitude)
VALUES (114, 59.5166667, 17.9),
(115, 59.5833333, 18.2),
(117, 59.5, 18.45)
【问题讨论】:
-
@Zhorov 你知道 m8 吗?
标签: sql json database api ssis