查询json文档SQL Server

ix0qys7i  于 2022-12-15  发布在  SQL Server
关注(0)|答案(1)|浏览(179)

我正在尝试使用下面的查询从Azure SQL数据库中的Azure数据湖存储上存储的json文档读取数据。我尝试了几种方法,但似乎都无法恢复数据。例如,我尝试在OPENJSON中使用此'$.data[0].AccID',但仅从多个数组中恢复第一条记录,但不确定如何恢复所有数据。

儿子:

[
    {
        "data":  [
                      {
                          "AccID":  1234,
                          "CustID":  456,
                          "Total":  1234.1234,
                          "OrderDate":  "2022-12-01"
                      },
                      {
                          "AccID":  5678,
                          "CustID":  890,
                          "Total":  5678.5678,
                          "OrderDate":  "2022-12-01"
                      }
                  ],
        "count":  2
    },
    {
        "data":  [
                      {
                          "AccID":  1234,
                          "CustID":  456,
                          "Total":  100.0,
                          "OrderDate":  "2021-12-01"
                      },
                      {
                          "AccID":  5678,
                          "CustID":  890,
                          "Total":  200.0,
                          "OrderDate":  "2021-12-01"
                      },
                      {
                          "AccID":  8900,
                          "CustID":  235,
                          "Total":  300.0,
                          "OrderDate":  "2021-12-01"
                      }
                  ],
        "count":  3
    }   
]

查询:

SELECT *
FROM OPENROWSET (
    BULK 'blobpath/file.json', 
    DATA_SOURCE = 'adls',
    SINGLE_CLOB
     ) AS [data]
    CROSS APPLY OPENJSON (X.BulkColumn, '$.value')
    WITH (
    AccID int,
    CustID int,
    Total float,
    OrderDate date)
j13ufse2

j13ufse21#

我创建了存储帐户并将Json文件上传到容器reference image.
输入.json:

[
    {
        "data":  [
                      {
                          "AccID":  1234,
                          "CustID":  456,
                          "Total":  1234.1234,
                          "OrderDate":  "2022-12-01"
                      },
                      {
                          "AccID":  5678,
                          "CustID":  890,
                          "Total":  5678.5678,
                          "OrderDate":  "2022-12-01"
                      }
                  ],
        "count":  2
    },
    {
        "data":  [
                      {
                          "AccID":  1234,
                          "CustID":  456,
                          "Total":  100.0,
                          "OrderDate":  "2021-12-01"
                      },
                      {
                          "AccID":  5678,
                          "CustID":  890,
                          "Total":  200.0,
                          "OrderDate":  "2021-12-01"
                      },
                      {
                          "AccID":  8900,
                          "CustID":  235,
                          "Total":  300.0,
                          "OrderDate":  "2021-12-01"
                      }
                  ],
        "count":  3
    }   
]

我生成了sas令牌并创建了主密钥和数据源。我在sql中创建了一个表,其中包含以下列

Create  table data1( ACCID varchar(100),               
          CustID varchar(100),
          Total float(100),                
          OrderDate date,
          count int  )

使用以下代码将数据插入该表:

INSERT  INTO data1
SELECT ACCID,CustID,Total,OrderDate,count FROM  OPENROWSET(
BULK  'jsonfile path',
DATA_SOURCE = 'your data source'
SINGLE_CLOB
) AS DataFile
cross  apply openjson(BulkColumn)
WITH (
      AccID varchar(100) '$.data[0].AccID',               
          CustID varchar(100) '$.data[0].CustID',
          Total float '$.data[0].Total',                
          OrderDate date '$.data[0].OrderDate',
          count int '$.count'
)
INSERT  INTO data1
SELECT ACCID,CustID,Total,OrderDate,count FROM  OPENROWSET(
BULK  'jsonfile path',
DATA_SOURCE = 'your data source'
SINGLE_CLOB
) AS DataFile
cross  apply openjson(BulkColumn)
WITH (
      AccID varchar(100) '$.data[1].AccID',               
          CustID varchar(100) '$.data[1].CustID',
          Total float '$.data[1].Total',                
          OrderDate date '$.data[1].OrderDate',
          count int '$.count'
)
         
INSERT  INTO data1
SELECT ACCID,CustID,Total,OrderDate,count FROM  OPENROWSET(
BULK  'jsonfile path',
DATA_SOURCE = 'your data source'
SINGLE_CLOB
) AS DataFile
cross  apply openjson(BulkColumn)
WITH (
          AccID varchar(100) '$.data[2].AccID',               
          CustID varchar(100) '$.data[2].CustID',
          Total float '$.data[2].Total',                
          OrderDate date '$.data[2].OrderDate',
          count int '$.count'
)

数据插入成功,我检索到的表的数据如下

我删除了空值行使用下面的代码图像供参考:

delete from data1 where ACCID is Null

我使用下面的代码根据json检索了表中按计数排序的数据

select * from data1 order by count

输出:

通过这种方式,我从Json文档中检索所有数据。

相关问题