使用Dynamo Db在AWS Appsync中执行批量获取时遇到问题

时间:2018-05-28 11:23:57

标签: amazon-web-services amazon-dynamodb aws-appsync

我无法在应用同步中执行Batch Get。这是我的以下解析器。

请求映射模板。

  require(shiny)
  require(stringr)
  require(shinythemes)
  require(leaflet)
  require(RColorBrewer)
  require(rgdal)
  require(rgeos)

  #### UI ####
  ui <- fluidPage(
    theme = shinytheme("spacelab"),
    titlePanel("Indice"),
    navlistPanel( 
      tabPanel(title = "Mappe",
         fluidRow(column(6, sliderInput(inputId = "Anno.map",
                                        label = "Anno di manifestazione",
                                        min = 2000,
                                        max = 2016, 
                                        value = 2016,
                                        step = 1,
                                        ticks = FALSE,
                                        sep = "")),
                  column(6, selectInput(inputId = "Patologia.map",
                                        label = "Patologia",
                                        choices = list("SIST_NERV", "MESOT","TUM_RESP"),
                                        selected = "SIST_NERV",
                                        multiple = FALSE))),
         fluidRow(column(6, leafletOutput(outputId = "Mappa.ASL", height = "600px", width = "100%")))
    )
   )
  )

 #### SERVER ####
 server <- function(input, output) {

    # NOT REACTIVE 
    confini.comuni <- readOGR(dsn = "shapes/originali", layer = "rt.confini.comunali", stringsAsFactors = FALSE)
    confini.comuni.WGS84 <- spTransform(confini.comuni, CRS("+proj=longlat +datum=WGS84 +no_defs")) 

    confini.asl <- readOGR(dsn = "shapes/originali", layer = "rt.confini.asl", stringsAsFactors = FALSE)
    confini.asl.WGS84 <- spTransform(confini.asl, CRS("+proj=longlat +datum=WGS84 +no_defs"))

    # REACTIVE 
    anno.map <- reactive({input$Anno.map})

    pat.map <- reactive({input$Patologia.map})

    mappa <- reactive({                                                         
        zone.WGS84 <- spTransform(readOGR(dsn = "shapes/zone", 
                                  layer = paste0("zone_", anno.map()), stringsAsFactors = FALSE), 
                                  CRS("+proj=longlat +datum=WGS84 +no_defs"))           

        domain <- paste0("zone_", anno.map(), "@data$", pat.map())
        labels.1 <- paste0("zone_", anno.map(), "@data$EXASLNOME")
        labels.2 <- paste0("zone_", anno.map(), "@data$", pat.map())
        labels.3 <- paste0("zone_", anno.map(), "@data$", pat.map(), "p")

        pal <- colorQuantile(palette = "YlOrRd",  
                             domain = domain(), n = 6,
                             na.color = "808080", alpha = FALSE, reverse = FALSE, right = FALSE)
        labels <- sprintf("<strong>%s</strong><br/>%g Segnalazioni<br/> %g con nesso positivo",
                   labels.1(), labels.2(), labels.3()) %>% 
                   lapply(htmltools::HTML)    

    leaflet(options = leafletOptions(zoomControl = FALSE, dragging = FALSE, minZoom = 7.5, maxZoom = 7.5)) %>%   
            addPolygons(data = confini.comuni.WGS84,
            weight = 1,
            opacity = 1,
            color = "black") %>%
    addPolygons(data = confini.asl.WGS84,
                weight = 2,
                opacity = 1,
                color = "red")  %>%      
    addPolygons(data = zone.WGS84(), 
                fillColor = ~pal(domain()),
                weight = 2,
                opacity = 1,
                color = "white",
                dashArray = "3",
                fillOpacity = 0.7,
                highlight = highlightOptions(weight = 5,
                                             color = "666",
                                             dashArray = "",
                                             fillOpacity = 0.7,
                                             bringToFront = TRUE),
                label = labels())
    })


   output$Mappa.ASL <- renderLeaflet({mappa()})

  }

  # Run the application 
  shinyApp(ui = ui, server = server)

响应映射模板

#set($ids = [])
#foreach($id in ${ctx.args.topicIds})
   #set($map = {})
   $util.qr($map.put("topicId", $util.dynamodb.toString($id)))
   $util.qr($ids.add($map))
#end

{
 "version" : "2018-05-29",
 "operation" : "BatchGetItem",
 "tables" : {
    "Topic": {
        "keys": $util.toJson($ids),
        "consistentRead": true
    }
 }
}

这就是我提供查询的方式

$util.toJson($ctx.result.data.Topic)

但我得到的结果为空

 query listStudentBookmarkedTopics {
   listStudentBookmarkedTopics(
     topicIds: [ "503", "501" ]
   ) {
     topicId
   }
 }

这是我的表格的样子

enter image description here

这是云监视日志

enter image description here

2 个答案:

答案 0 :(得分:0)

您的DynamoDB表是什么样的? Topic是表的名称吗?它上面定义的主分区键是什么?我需要其他详细信息来帮助您解决此问题。

或者,请为您的API启用CloudWatch日志(您应该可以通过控制台 - > gt;在您的API的“设置”页面下执行此操作)。尝试再次运行查询,并查看此路径的已解析请求和响应模板。这应该为您提供更多详细信息。

答案 1 :(得分:0)

我有类似的问题。通过应用适当的权限对其进行了修复。如果您创建了新的IAM角色,则默认情况下它不会授予batch*权限。来自AWS文档:

  

与其他解析器一样,您需要在AWS AppSync中创建一个数据源,然后创建一个角色或使用一个现有角色。由于批处理操作在DynamoDB表上需要不同的权限,因此您需要为配置的角色授予读取或写入操作的权限:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "dynamodb:BatchGetItem",
        "dynamodb:BatchWriteItem"
      ],
      "Effect": "Allow",
      "Resource": [
        "arn:aws:dynamodb:region:account:table/TABLENAME",
        "arn:aws:dynamodb:region:account:table/TABLENAME/*"
      ]
    }
  ]
}

您可以附加新策略,也可以仅在IAM仪表板中的现有策略上添加"dynamodb:BatchGetItem"。 `