Google App Engine - 本地开发服务器和已部署服务器之间的区别

Google App Engine - Difference between local Dev server and deployed server

我正在 Google App Engine 上编写应用程序,我已经能够在本地编写 运行 的代码并将数据放入本地数据存储区。但是,当我将代码部署到 google 服务器时,没有数据被放入数据存储区。我没有将任何内容放入数据存储区的主要指标是,第一,当我进入开发人员控制台时,我被告知没有条目,第二,当我 运行 获取数据的页面时,没有任何内容返回。

我是否需要先在 app.yaml 或开发人员控制台中定义新种类,然后它们才能在生产数据存储上工作?

这是我在数据存储中的放置和获取方法。同样,在测试环境中工作,在生产环境中不工作,一切都编译得很好。

package tweetdata

import (
    "net/url"
    "time"

    "golang.org/x/net/context"
    "google.golang.org/appengine/datastore"
    "google.golang.org/appengine/log"

    "github.com/ChimeraCoder/anaconda"
)

const linkTweetKind string = "LinkTweet"
const tweetKey string = "Tweets"
const tweetKeyID string = "default_tweetstore"

//LinkTweet contains the address extracted from a tweet and the original tweet
type LinkTweet struct {
    Address *url.URL
    Tweet   anaconda.Tweet
}

//StoreTweet is a struct used for storing a tweet in the datastore
type StoreTweet struct {
    Address     string
    Text        string
    TweetID     int64
    CreatedTime time.Time
    Retweets    int
    Favorites   int
}

//TweetScore is a struct that shows the relative score of an address based on
// it's populatrity
type TweetScore struct {
    Address    *url.URL
    score      int
    lastActive time.Time
}

//WriteLinkTweet writes a given Tweet to the datastore
func WriteLinkTweet(tweet LinkTweet, c context.Context) error {
    log.Infof(c, "Putting Tweet into datastore: %v", tweet.Tweet.Id)
    key := datastore.NewIncompleteKey(c, linkTweetKind, getTweetKey(c))
    created, _ := tweet.Tweet.CreatedAtTime()
    store := &StoreTweet{Address: tweet.Address.String(),
        Text:        tweet.Tweet.Text,
        TweetID:     tweet.Tweet.Id,
        CreatedTime: created,
        Retweets:    tweet.Tweet.RetweetCount,
        Favorites:   tweet.Tweet.FavoriteCount,
    }

    err := datastore.RunInTransaction(c, func(c context.Context) error {
        _, err := datastore.Put(c, key, store)
        log.Errorf(c, "Failed to write LinkTweet to datastore. %v", err.Error())
        return err
    }, nil)
    return err
}

//GetAllNewTweets queries the datastore and gets all tweets created since the last
// time given
func GetAllNewTweets(since time.Time, c context.Context) []StoreTweet {
    q := datastore.NewQuery(linkTweetKind).Ancestor(getTweetKey(c)).Filter("CreatedTime >=", since)
    out := make([]StoreTweet, 0, 15)
    q.GetAll(c, &out)
    return out
}

// guestbookKey returns the key used for all guestbook entries.
func getTweetKey(c context.Context) *datastore.Key {
    // The string "default_guestbook" here could be varied to have multiple guestbooks.
    return datastore.NewKey(c, tweetKey, tweetKeyID, 0, nil)
}

您看到了 "eventual consistency" 的(通常最初令人困惑的)效果。在事务之外编写的实体需要一段时间才能对查询可见。

https://cloud.google.com/datastore/docs/articles/balancing-strong-and-eventual-consistency-with-google-cloud-datastore/ explains the problem. https://cloud.google.com/appengine/docs/go/datastore/transactions具体去。

您是否在 StoreTweet 结构中的任何字段上使用 datastore:,noindex?为了 运行 针对任何字段的任何查询,数据存储必须首先索引您要查询的字段。因此,不索引这些字段将导致查询 return nil 而不会出现任何错误。在您第一次 运行 查询后,您的开发服务器会自动在您的项目中创建一个 index.yaml 文件吗?生产数据存储使用这个文件来决定哪些字段应该被索引,所有没有被索引的字段将无法被查询。根据您提供的信息,我能想到的就是这些会导致您的查询 return 为空。

为了进一步调试,我会浏览您的生产数据存储并确保您的数据结构正确并且值设置为您期望的值。