【问题标题】:Query an existing Google Cloud Datastore from Google app engine从 Google 应用引擎查询现有的 Google Cloud Datastore
【发布时间】:2016-12-07 19:22:47
【问题描述】:

我在 Google Cloud Datastore 中有一个大约 50k 行的实体,它是独立的而不是 GAE。我正在开始使用 GAE 进行开发,并且想查询这个现有的数据存储而无需将其导入 GAE。我一直无法找到连接到现有数据存储种类的方法。

从 Hello World 和其他指南更改的基本代码,我试图作为 POC 工作。

import webapp2
import json
import time
from google.appengine.ext import ndb

class Product(ndb.Model):
type = ndb.StringProperty()

@classmethod
def query_product(cls):
    return ndb.gql("SELECT * FROM Product where name >= :a LIMIT 5 ")

class MainPage(webapp2.RequestHandler):
def get(self):
    self.response.headers['Content-Type'] = 'text/plain'

    query = Product.query_product()

    self.response.write(query)


app = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)

返回的错误是

TypeError: Model Product has no property named 'name'

似乎很明显,它试图将 GAE 数据存储与产品类型一起使用,而不是我现有的已定义产品的数据存储,但我找不到如何建立这种连接。

【问题讨论】:

    标签: google-app-engine google-cloud-datastore


    【解决方案1】:

    只有一个 Google Cloud Datastore。 App Engine 没有自己的数据存储区 - 它与同一个 Google Cloud 数据存储区一起使用。

    数据存储区中的所有实体都存储在特定的项目中。如果您尝试访问来自不同项目的数据,则必须通过特殊身份验证才能看到它。

    【讨论】:

    • 安德烈,感谢您为我澄清这一点。我正在使用的应用程序与我的数据存储在同一个项目中。我将再次查看文档以尝试找到我的问题。
    • 转到您的 Google Cloud Platform 控制台 (console.cloud.google.com) 并检查您是否看到您的数据(在“数据存储区 > 实体”下)。
    【解决方案2】:

    当你说你 would like to query this existing datastore without having to import it to GAE 时,我不太确定你想要完成什么。我猜您的项目 A 的数据存储区有 50k 行,并且您正在启动项目 B。并且您想从项目 B 访问项目 A 数据存储区。如果是这种情况,并且您正在尝试从不同的项目访问数据存储区,然后可能提到 remote apithis previous answer 可以帮助您。

    【讨论】:

      【解决方案3】:

      下面是工作代码。在我发表这篇原始帖子时,我已经很接近了,但我没有得到任何数据的原因是因为我在本地运行我的应用程序。一旦我真正将我的代码部署到 App Engine,它就可以从 Datastore 中提取出来。

      import webapp2
      import json
      import time
      from google.appengine.datastore.datastore_query import Cursor
      from google.appengine.ext import ndb
      
      
      
      class Product(ndb.Model):
          name = ndb.StringProperty()
      
      
      class MainPage(webapp2.RequestHandler):
          def get(self):
          self.response.headers['Content-Type'] = 'text/plain'
      
      
          query = ndb.gql("SELECT * FROM Product where name >= 'a' LIMIT 5 ")
          output = query.fetch()
      
          #query = Product.query(Product.name == 'zubo - pre-owned - nintendo ds')
          #query = Product.query()
          #output = query.fetch(10)
          self.response.write(output)
      
      
      app = webapp2.WSGIApplication([
      ('/', MainPage),
      ], debug=True)
      

      【讨论】:

        猜你喜欢
        • 2017-12-26
        • 1970-01-01
        • 1970-01-01
        • 2019-03-22
        • 2019-09-26
        • 2017-01-16
        • 1970-01-01
        • 2015-09-04
        • 2018-01-22
        相关资源
        最近更新 更多