按种类查询的属性

按种类查询属性。

深入探索

如需查看包含此代码示例的详细文档,请参阅以下内容:

代码示例

C#

如需了解如何安装和使用 Datastore 模式客户端库,请参阅 Datastore 模式客户端库。 如需了解详情,请参阅 Datastore 模式 C# API 参考文档

如需向 Datastore 模式进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

Key key = _db.CreateKeyFactory("__kind__").CreateKey("Task");
Query query = new Query("__property__")
{
    Filter = Filter.HasAncestor(key)
};
var properties = new List<string>();
foreach (Entity entity in _db.RunQuery(query).Entities)
{
    string kind = entity.Key.Path[0].Name;
    string property = entity.Key.Path[1].Name;
    var representations = entity["property_representation"]
        .ArrayValue.Values.Select(x => x.StringValue)
        .OrderBy(x => x);
    properties.Add($"{property}:" +
        string.Join(",", representations));
};

Go

如需了解如何安装和使用 Datastore 模式客户端库,请参阅 Datastore 模式客户端库。 如需了解详情,请参阅 Datastore 模式 Go API 参考文档

如需向 Datastore 模式进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

kindKey := datastore.NameKey("__kind__", "Task", nil)
query := datastore.NewQuery("__property__").Ancestor(kindKey)

type Prop struct {
	Repr []string `datastore:"property_representation"`
}

var props []Prop
keys, err := client.GetAll(ctx, query, &props)

Java

如需了解如何安装和使用 Datastore 模式客户端库,请参阅 Datastore 模式客户端库。 如需了解详情,请参阅 Datastore 模式 Java API 参考文档

如需向 Datastore 模式进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

Key key = datastore.newKeyFactory().setKind("__kind__").newKey("Task");
Query<Entity> query =
    Query.newEntityQueryBuilder()
        .setKind("__property__")
        .setFilter(PropertyFilter.hasAncestor(key))
        .build();
QueryResults<Entity> results = datastore.run(query);
Map<String, Collection<String>> representationsByProperty = new HashMap<>();
while (results.hasNext()) {
  Entity result = results.next();
  String propertyName = result.getKey().getName();
  List<StringValue> representations = result.getList("property_representation");
  Collection<String> currentRepresentations = representationsByProperty.get(propertyName);
  if (currentRepresentations == null) {
    currentRepresentations = new HashSet<>();
    representationsByProperty.put(propertyName, currentRepresentations);
  }
  for (StringValue value : representations) {
    currentRepresentations.add(value.get());
  }
}

Node.js

如需了解如何安装和使用 Datastore 模式客户端库,请参阅 Datastore 模式客户端库。 如需了解详情,请参阅 Datastore 模式 Node.js API 参考文档

如需向 Datastore 模式进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

async function runPropertyByKindQuery() {
  const ancestorKey = datastore.key(['__kind__', 'Account']);

  const query = datastore
    .createQuery('__property__')
    .hasAncestor(ancestorKey);
  const [entities] = await datastore.runQuery(query);

  const representationsByProperty = {};

  entities.forEach(entity => {
    const key = entity[datastore.KEY];
    const propertyName = key.name;
    const propertyType = entity.property_representation;

    representationsByProperty[propertyName] = propertyType;
  });

  console.log('Task property representations:');
  for (const key in representationsByProperty) {
    console.log(key, representationsByProperty[key]);
  }

  return representationsByProperty;
}

PHP

如需了解如何安装和使用 Datastore 模式客户端库,请参阅 Datastore 模式客户端库。 如需了解详情,请参阅 Datastore 模式 PHP API 参考文档

如需向 Datastore 模式进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

$ancestorKey = $datastore->key('__kind__', 'Task');
$query = $datastore->query()
    ->kind('__property__')
    ->hasAncestor($ancestorKey);
$result = $datastore->runQuery($query);
/* @var array<string,string> $properties */
$properties = [];
/* @var Entity $entity */
foreach ($result as $entity) {
    $propertyName = $entity->key()->path()[1]['name'];
    $propertyType = $entity['property_representation'];
    $properties[$propertyName] = $propertyType;
}
// Example values of $properties: ['description' => ['STRING']]

Python

如需了解如何安装和使用 Datastore 模式客户端库,请参阅 Datastore 模式客户端库。 如需了解详情,请参阅 Datastore 模式 Python API 参考文档

如需向 Datastore 模式进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

from google.cloud import datastore

# For help authenticating your client, visit
# https://cloud.google.com/docs/authentication/getting-started
client = datastore.Client()

ancestor = client.key("__kind__", "Task")
query = client.query(kind="__property__", ancestor=ancestor)

representations_by_property = {}

for entity in query.fetch():
    property_name = entity.key.name
    property_types = entity["property_representation"]

    representations_by_property[property_name] = property_types

Ruby

如需了解如何安装和使用 Datastore 模式客户端库,请参阅 Datastore 模式客户端库。 如需了解详情,请参阅 Datastore 模式 Ruby API 参考文档

如需向 Datastore 模式进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

ancestor_key = datastore.key "__kind__", "Task"
query = datastore.query("__property__")
                 .ancestor(ancestor_key)

entities = datastore.run query
representations = entities.each_with_object({}) do |entity, memo|
  property_name = entity.key.name
  property_types = entity["property_representation"]
  memo[property_name] = property_types
end

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器