Google Datastore 學習記錄

由於在google app engine 使用google cloud sql  是要收費的,於是學習一下google提供的免費的非關係型數據庫datastore。

它的特點有:

  • No planned downtime(全天24小時無宕機服務)
  • Atomic transactions(原子性的事務機制)
  • High availability of reads and writes(高可用的讀寫操作)
  • Strong consistency for reads and ancestor queries(祖先查詢和讀操作的強一致性)
  • Eventual consistency for all other queries(所有查詢的最終一致性)
關於Datastore,官方文檔時這樣介紹的:

The Datastore holds data objects known as entities. An entity has one or more properties, named values of one of several supported data types: for instance, a property can be a string, an integer, or a reference to another entity. Each entity is identified by its kind, whichcategorizes the entity for the purpose of queries, and a key that uniquely identifies it within its kind. The Datastore can execute multiple operations in a single transaction. By definition, a transaction cannot succeed unless every one of its operations succeeds; if any of the operations fails, the transaction is automatically rolled back. This is especially useful for distributed web applications, where multiple users may be accessing or manipulating the same data at the same time.

Datastore是以entities來存儲數據對象的,一個entity有一個或者多個property,property的取值是某一種支持的數據類型:例如,一個property的取值可以是string,integer, 或者是一個到其他entity的引用。每一個entity可以由它的kind來進行區分的,kind根據查詢的目的來分類entity,key唯一的來區分entity。datastore可以在一個事務中處理多個操作。根據定義一個事務在所有的操作未完成之前是不能夠成功的;如果某一個操作失敗那麼事務將會回滾。這在分佈式的web應用中是非常常見的,應爲在同一時刻會存在同一個用戶操作同一數據的情況。


同傳統數據庫的對比

Unlike traditional relational databases, the Datastore uses a distributed architecture to automatically manage scaling to very large data sets. While the Datastore interface has many of the same features as traditional databases, it differs from them in the way it describes relationships between data objects. Entities of the same kind can have different properties, and different entities can have properties with the same name but different value types.

不像傳統的關係型數據庫datastore使用了分佈式的體系結構來自動管理大規模的數據集,datastore也有許多踢同傳統的關係型數據庫一樣的特性,它最大的不同是它描述數據對象關係的方式。數據同一個kind的entity可以有不同的properties,不同的entity可以有取值不一樣的相同的properties

These unique characteristics imply a different way of designing and managing data to take advantage of the ability to scale automatically. In particular, the Datastore differs from a traditional relational database in the following important ways:

以上這些不同的設計和管理數據的方式能夠充分利用google datastore在分佈式環境下的自適應性。一般datastore具有以下與傳統的關係型數據庫不一樣的地方:

  • The Datastore is designed to scale, allowing applications to maintain high performance as they receive more traffic:(在大規模訪問下能夠保證自適應和高性能)
    • Datastore writes scale by automatically distributing data as necessary.(大規模寫時自動分發寫入的數據)
    • Datastore reads scale because the only queries supported are those whose performance scales with the size of the result set (as opposed to the data set). This means that a query whose result set contains 100 entities performs the same whether it searches over a hundred entities or a million. This property is the key reason some types of queries are not supported.()(讀受限制於結果集的大小)
  • Because all queries are served by pre-built indexes, the types of queries that can be executed are more restrictive than those allowed on a relational database with SQL. In particular, the following are not supported:(由於所有的query被預先編了索引,所以查詢的類型嚴格受限)
    • Join operations(不允許Join查詢)
    • Inequality filtering on multiple properties()
    • Filtering of data based on results of a subquery(不支持子查詢的過濾)
  • Unlike traditional relational databases, the Datastore doesn't require entities of the same kind to have a consistent property set (although you can choose to enforce such a requirement in your own application code).(不要求有相同的kind的entity有相同的property集)


entity

datastore中的對象叫做entity,一個entity有一個或者多個property,每一個property有一個或者多個value,proerpty的value的類型可以是,integer,float number ,string,date,和binary數據 等其他的。在一個具有多種取值的property上的查詢能夠測試這些取值是否滿足特徵查詢。

注意:datastore不要求指定kind的entity具有相同的property,同樣也不要求所有的entity同一個property具有相同的值。

kinds,keys,和identifiers

kind是來表義entity的類型的,例如一個人力資源管理應用中可以用一個kind叫 employee的entity來代表每一個僱員。

然而每一個entity有它唯一的一個key 這是來唯一區別entity的,key中一般包括瞭如下的信息:

entity的kind

一個identifier,identifier既可以是 key 的字符串名稱 也可以是 一個 整形的id

一個祖先路徑(可選) 它是用在在datastore繼承關係中來定位當前entity的


identifier是在entity創建的時候指定的 由於它是entity的key的一部分,它永久的與entity聯繫且不可以更改,可以通過以下兩種方式來指定identifier

在構造entity的時候傳遞 string類型的key

使用缺省的構造函數時datastore會自動的指定一個integer類型的id爲identifier。


Ancestor paths

datastore中entity的繼承結構有點類似於文件系統中的目錄結構。當你創建一個entity的時候,你可以指定另一個entity作爲它的parent.那麼這個新創建的entity就叫做parent entity的child(不想文件系統,這裏的parent不要求一定存在),如果一個entity沒有父entity那麼我們叫他root entity.當entity創建後它與它的parent的關係就是不可更改的。datastore不會爲entity的子entity 分配相同的numeric id,也不會爲兩個root entity分配相同的numeric id。entity的parent 的parent就叫做entity的祖先,同樣的entity的children的children就叫做entity的後代。一個entity同它的後代屬於同一個entity group.一個entity的ancestor path就是從root entity到 當前entity的一個entity的序列。

完整的key 包含了entity的ancestor path(是以kind-identifier的序列的形式):

[Person:GreatGrandpa, Person:Grandpa, Person:Dad, Person:Me]
如果是一個root entity 它的完整的key 則是如下的形式:

[Person:GreatGrandpa]

Queries and indexes

爲了通過key來獲取datastore中的entity,應用程序可以通過property值來組成一個query最後獲取entity.query(查詢)可以根據特定的kind,或者是entity的property值,key,和ancestor,最後可以返回一個或者多個entity作爲query的結果。query的結果同樣可以按照某種規則進行排序。返回的entity的結果是滿足所有查詢條件的entity。一個查詢也能返回整個entity集(類似於sql中的select *),或者是 projected entities,或者是所有的entity的key.

一個query一般包括以下幾個部分:

entity 的kind

一個或者多個基於entity的屬性值,key,ancestor的過濾條件(類似於sql中的 where )

一個或者多個用於排列結果的規則


當執行這個sql後將返回滿足指定kind和過濾條件以及排序規則的所有entity。

(爲了節約內存和提供查詢的性能,一個query最好限制查詢結果集的數量)

另外可以通過在query中構造ancestor filter來返回一個從指定ancestor到當前entity的entityt group,這種query就叫做ancestor query。使用ancestor query 返回的數據滿足強一致性,相對的,普通的query返回的數據集僅僅是最終一致性的裏面有可能會包含髒數據。





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章