2016-12-07 47 views
0

我在Google雲數據存儲中有一個約50k行的實體,單獨不是GAE。我正在開始使用GAE進行開發,並且希望查詢此現有數據存儲,而無需將其導入GAE。我一直無法找到連接到現有數據存儲區種類的方式。從谷歌應用引擎查詢現有的Google雲數據存儲

從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數據存儲與實物產品,而不是我的產品現有的數據存儲已經定義的,但我不能找到如何使連接。

回答

1

只有一個Google Cloud Datastore。 App Engine沒有自己的數據存儲 - 它與同一個Google Cloud Datastore一起使用。

數據存儲區中的所有實體都存儲在特定的項目中。如果您嘗試訪問來自其他項目的數據,則無法通過特殊認證就無法看到它。

+0

Andrei,感謝您爲我澄清這一點。我正在使用的應用程序與我的數據存儲在同一個項目中。我將再次查看文檔以嘗試查找我的問題。 –

+0

轉到您的Google雲端平臺控制檯(https://console.cloud.google.com),並檢查您是否看到您的數據(位於「數據存儲>實體」下)。 –

0

我不太確定當你說你would like to query this existing datastore without having to import it to GAE時你試圖完成什麼。我猜測你的項目A的數據存儲區有50k行,並且你正在啓動項目B.並且你想從項目B訪問項目數據存儲區。如果是這種情況,並且如果你想要從不同的項目訪問數據存儲,然後this previous answer,提到remote api可以幫助你。

0

以下是工作代碼。我在做這篇原創文章的時候非常接近,但是我沒有收到數據的原因是我在本地運行我的應用程序。只要我真的將我的代碼部署到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) 
相關問題