使用R将JSON数据加载到SQL数据库中

时间:2016-10-26 18:23:45

标签: mysql json r azure-sql-database

R代码 - 尝试将JSON文件加载到SQLDatabase中并陷入以下错误。

json_file <- 'TwitterAPI/FundingData.json'
credentials <- fromJSON(json_file)
print(credentials)
#Azure SQL DataBase Connection
sqldbcnn <- odbcConnect(dsn = "AzureSQLDb", uid = "sn", pwd = "$")
twitter <- list(credentials)
twitter.df <- data.frame(twitter)
twitter.df <- select(twitter.df, data.billed_charge_local_micro, 
                 data.billed_follows, data.estimated_charge_local_micro, 
                 data.granularity, data.id, 
                 data.promoted_account_follow_rate, 
                 data.promoted_account_follows, 
                 data.promoted_tweet_timeline_engagement_rate, 
                 data.promoted_tweet_timeline_engagements)
sqlSave (sqldbcnn,
     twitter.df,
     tablename = 'Twitter_Macy_s',
     append = TRUE,
     rownames = FALSE,
     colnames = FALSE,
     verbose = TRUE,
     safer = TRUE,
     addPK = FALSE,
     fast = FALSE,
     test = FALSE,
     nastring = NULL)
close(Macy-s_DataBase_Connection)

运行此代码时,我遇到错误:

"Error in `colnames<-`(`*tmp*`, value = c("billed_charge_local_micro",  : 
  length of 'dimnames' [2] not equal to array extent"

    {
    "data": {
        "billed_charge_local_micro": [
            22000000,
            22000000,
            22000000
        ],
        "billed_engagements": [
            59,
            50,
            69
        ],
        "billed_follows": [
            0,
            0,
            0
        ],
        "end_time": "2013-04-16T07:00:00Z",
        "estimated_charge_local_micro": [
            22000000,
            22000000,
            22000000
        ],
        "granularity": "DAY",
        "id": "e25e",
        "promoted_account_follow_rate": [
            0,
            0,
            0
        ],
        "promoted_account_follows": [
            0,
            0,
            0
        ],
        "promoted_account_impressions": [
            0,
            0,
            0
        ],
        "promoted_account_profile_visits": [
            0,
            0,
            0
        ],
        "promoted_tweet_search_clicks": [
            0,
            0,
            0
        ],
        "promoted_tweet_search_engagement_rate": [
            0,
            0,
            0
        ],
        "promoted_tweet_search_engagements": [
            0,
            0,
            0
        ],
        "promoted_tweet_search_follows": [
            0,
            0,
            0
        ],
        "promoted_tweet_search_impressions": [
            0,
            0,
            0
        ],
        "promoted_tweet_search_replies": [
            0,
            0,
            0
        ],
        "promoted_tweet_search_retweets": [
            0,
            0,
            0
        ],
        "promoted_tweet_timeline_clicks": [
            65,
            75,
            81
        ],
        "promoted_tweet_timeline_engagement_rate": [
            0.0763807285546416,
            0.0857142857142857,
            0.0682392586352148
        ],
        "promoted_tweet_timeline_engagements": [
            65,
            75,
            81
        ],
        "promoted_tweet_timeline_follows": [
            0,
            0,
            0
        ],
        "promoted_tweet_timeline_impressions": [
            851,
            875,
            1187
        ],
        "promoted_tweet_timeline_replies": [
            0,
            0,
            0
        ],
        "promoted_tweet_timeline_retweets": [
            0,
            0,
            0
        ],
        "start_time": "2013-04-13T07:00:00Z"
    },
    "data_type": "stats",
    "request": {
        "params": {
            "account_id": "5gvk9h",
            "end_time": "2013-04-16T07:00:00Z",
            "granularity": "DAY",
            "id": "e25e",
            "start_time": "2013-04-13T07:00:00Z"
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我不是R连接到Azure SQL的专家,但是您似乎没有像加密这样的东西:连接字符串中的true:

sqldbcnn <- odbcConnect(dsn = "AzureSQLDb", uid = "sn", pwd = "$")

看一下这个例子:

http://blog.revolutionanalytics.com/2016/02/ms_sql_azure.html

他们正在使用RODBC,他们在连接字符串中有Encrypt = yes:

"Driver={SQL Server Native Client 11.0};Server=tcp:hzgi1l8nwn.database.windows.net,1433;Database=Test_R2;Uid=your_logon_ID@hzgi1l8nwn;Pwd={your_password_here};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;"

也许您需要在连接中添加类似的参数。