如何迭代URL列表以提取JSON数据

时间:2017-03-28 20:49:39

标签: sql-server json

我有MS SQL Server(2014),我需要的是从Web API中提取一堆JSON元数据。不幸的是,API仅限于返回30条记录(我有7400条记录可供选择)。所以我只是迭代一堆URL并导入数据,但我卡住了。

我过去曾使用此方法(Passing filename as variable in OPENROWSET(BULK filename))并使用parseJSON

将JSON数据导入SQL Server

如果我的机器本地数据(例如在我的C驱动器上)但是在我使用URL时不起作用,这种方法可以正常工作 - 我收到以下错误:

(1 row(s) affected)
Msg 4861, Level 16, State 1, Line 1
Cannot bulk load because the file "https://api.stackexchange.com/2.2/search?order=desc&sort=activity&intitle=perl&site=stackoverflow" could not be opened. Operating system error code 123(The filename, directory name, or volume label syntax is incorrect.).
Msg 8115, Level 16, State 2, Line 45
Arithmetic overflow error converting expression to data type nvarchar.

如何修复我的代码以使用互联网上的文件?

--    SET @JSON_FILE= 'C:\Temp\SO.json'  --  If I save the data on my harddrive the following code works.
    SET @JSON_FILE= 'https://api.stackexchange.com/2.2/search?order=desc&sort=activity&intitle=perl&site=stackoverflow'

    /* http://www.sqlservercentral.com/Forums/Topic982066-338-1.aspx
    Read the JSON file into the varchar variable.  This is done via a bulk insert using the OPENROWSET() function.   Because this stored proc is to be re-used with different JSON files, ideally you want to pass the JSON file path as a variable.  However, because the OPENROWSET() function won't accept variables as a parameter, the command needs to be built as a string and then passed to the sp_executesql system stored procedure.  The results are then passed back by an output variable.
    */
    -- Setup varchar variable to be used to hold contents of JSON file.
    DECLARE @txt varchar(max) 

    -- The command line
    DECLARE @COMMAND NVARCHAR(MAX) 
    -- The definition of the parameters used within the command line
    DECLARE @PARAM_DEF NVARCHAR(500)
    -- The parameter used to pass the file name into the command
    DECLARE @FILEVAR NVARCHAR(MAX)
    -- The output variable that holds the results of the OPENROWSET()
    DECLARE @JSON_OUT VARCHAR(MAX) 

    SET @FILEVAR = @JSON_FILE
    SET @PARAM_DEF = N'@JSON_FILE NVARCHAR(MAX), @JSON_OUT VARCHAR(MAX) OUTPUT'
    SET @COMMAND = N'SELECT @JSON_OUT = BulkColumn FROM OPENROWSET(BULK ''' +  @JSON_FILE + ''', SINGLE_BLOB) ROW_SET';

    EXEC sp_executesql @COMMAND, @PARAM_DEF, @JSON_FILE = @FILEVAR, @JSON_OUT = @txt OUTPUT;
    --parseJSON function from https://www.simple-talk.com/sql/t-sql-programming/consuming-json-strings-in-sql-server/
    SELECT * FROM parseJSON(@txt)

2 个答案:

答案 0 :(得分:0)

如果你升级到SQL Server 2016,你可以使用openjson,但是在2014年,我认为你仍然坚持使用openrowset希望获得一些魔力(虽然我是'还没看到它远程工作的一个例子),或者其他选项包括SQL-CLR程序集,这些程序由marc_s here很好地总结。

- 是的,该问题明确表现为只读URL访问。

答案 1 :(得分:0)

事实证明,使用Microsoft Powershell下载所有JSON然后只需使用我的代码导入数据就变得非常容易。

我使用

设置了一个简单的电子表格
  1. A栏从1到最后,
  2. B栏为序列添加30
  3. 将C列添加到生成URL字符串=CONCAT("$resource",A2,"='myURL/retrieve.php?query=&coll=cxr&m=",B2,"&n=",B2+30,"'")
  4. 的公式中
  5. D列然后使用URL字符串=CONCAT(" Invoke-RestMethod -Method Get -Uri $resource",A2," -Outfile ",A2,".json")
  6. 查询该网站

    然后我只是复制/粘贴所有C列(种子$ resourceN),然后复制/粘贴D列以下载JSON,使其受到SQL攻击。

相关问题