本文整理汇总了Java中com.google.datastore.v1.Key.PathElement类的典型用法代码示例。如果您正苦于以下问题:Java PathElement类的具体用法?Java PathElement怎么用?Java PathElement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PathElement类属于com.google.datastore.v1.Key包,在下文中一共展示了PathElement类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: processElement
import com.google.datastore.v1.Key.PathElement; //导入依赖的package包/类
@Override
public void processElement(ProcessContext c) {
String[] columns = c.element().split(",");
Builder builder = Key.newBuilder();
PathElement pathElement = builder.addPathBuilder().setKind("Product").setName(columns[0]).build();
Key key = builder.setPath(0, pathElement).build();
Entity.Builder entityBuilder = Entity.newBuilder();
entityBuilder.setKey(key);
entityBuilder.getMutableProperties().put("ProductCode", makeValue(columns[0]).build());
entityBuilder.getMutableProperties().put("CategoryCode", makeValue(columns[1]).build());
entityBuilder.getMutableProperties().put("ProductName", makeValue(columns[2]).build());
entityBuilder.getMutableProperties().put("Price", makeValue(Integer.valueOf(columns[3])).build());
String imageURL = "";
if (columns.length > 4) {
imageURL = columns[4];
}
entityBuilder.getMutableProperties().put("ImageURL", makeValue(imageURL).build());
Entity entity = entityBuilder.build();
c.output(entity);
}
示例2: comparePathElement
import com.google.datastore.v1.Key.PathElement; //导入依赖的package包/类
private int comparePathElement(PathElement thisElement, PathElement otherElement) {
int result = thisElement.getKind().compareTo(otherElement.getKind());
if (result != 0) {
return result;
}
if (thisElement.getIdTypeCase() == IdTypeCase.ID) {
if (otherElement.getIdTypeCase() != IdTypeCase.ID) {
return -1;
}
return Long.valueOf(thisElement.getId()).compareTo(otherElement.getId());
}
if (otherElement.getIdTypeCase() == IdTypeCase.ID) {
return 1;
}
return thisElement.getName().compareTo(otherElement.getName());
}
示例3: compare
import com.google.datastore.v1.Key.PathElement; //导入依赖的package包/类
@Override
public int compare(Key thisKey, Key otherKey) {
if (!thisKey.getPartitionId().equals(otherKey.getPartitionId())) {
throw new IllegalArgumentException("Cannot compare keys with different partition ids.");
}
Iterator<PathElement> thisPath = thisKey.getPathList().iterator();
Iterator<PathElement> otherPath = otherKey.getPathList().iterator();
while (thisPath.hasNext()) {
if (!otherPath.hasNext()) {
return 1;
}
int result = comparePathElement(thisPath.next(), otherPath.next());
if (result != 0) {
return result;
}
}
return otherPath.hasNext() ? -1 : 0;
}
示例4: keyToString
import com.google.datastore.v1.Key.PathElement; //导入依赖的package包/类
@Nullable
private String keyToString(Key k) {
StringBuilder sb = new StringBuilder();
List<String> paths = new ArrayList<>();
for (PathElement p : k.getPathList()) {
if (!Strings.isNullOrEmpty(p.getName())) {
paths.add(String.format("%s, \'%s\'", p.getKind(), p.getName().replace("'", "\'")));
} else {
paths.add(String.format("%s, %s", p.getKind(), Long.toString(p.getId())));
}
}
return "key(" + String.join(", ", paths) + ")";
}
示例5: makeEntity
import com.google.datastore.v1.Key.PathElement; //导入依赖的package包/类
public Entity makeEntity(KV<Integer, Iterable<TableRow>> content) {
Key key = Key.newBuilder()
.addPath(PathElement.newBuilder().setKind(this.kind).setId(content.getKey())).build();
String keyword = "";
List<Value> list = new ArrayList<>();
for (TableRow row : content.getValue()) {
String utterance = row.get("utterance").toString();
if (utterance == null || utterance.length() < 1) {
continue;
}
String word = row.get("keyword").toString();
if (keyword.equals(row.get("keyword")) == false) {
keyword = word;
}
if (list.size() > 1000) {
LOG.info("Truncated the text." + "keyword_id = " + content.getKey() + ", keyword = " + word);
break;
}
list.add(Value.newBuilder().setStringValue(utterance).build());
}
Entity.Builder entityBuilder = Entity.newBuilder();
entityBuilder.setKey(key);
Map<String, Value> propertyMap = new HashMap<String, Value>();
propertyMap.put("KeywordID", Value.newBuilder().setIntegerValue(content.getKey()).build());
propertyMap.put("Keyword", Value.newBuilder().setStringValue(keyword).build());
ArrayValue array = ArrayValue.newBuilder().addAllValues(list).build();
propertyMap.put("Candidates", Value.newBuilder().setArrayValue(array).build());
entityBuilder.putAllProperties(propertyMap);
return entityBuilder.build();
}
示例6: isValidKey
import com.google.datastore.v1.Key.PathElement; //导入依赖的package包/类
/**
* Returns true if a Cloud Datastore key is complete. A key is complete if its last element
* has either an id or a name.
*/
static boolean isValidKey(Key key) {
List<PathElement> elementList = key.getPathList();
if (elementList.isEmpty()) {
return false;
}
PathElement lastElement = elementList.get(elementList.size() - 1);
return (lastElement.getId() != 0 || !lastElement.getName().isEmpty());
}
示例7: isValidKey
import com.google.datastore.v1.Key.PathElement; //导入依赖的package包/类
static boolean isValidKey(Key key) {
List<PathElement> elementList = key.getPathList();
if (elementList.isEmpty()) {
return false;
}
PathElement lastElement = elementList.get(elementList.size() - 1);
return (lastElement.getId() != 0 || !lastElement.getName().isEmpty());
}
示例8: getKeyString
import com.google.datastore.v1.Key.PathElement; //导入依赖的package包/类
public String getKeyString(Entity e) {
String out = "";
for (PathElement pElm : e.getKey().getPathList()) {
String part;
if (pElm.getName() != null) {
part = pElm.getName();
} else {
part = ((Long) pElm.getId()).toString();
}
out += "," + part;
}
return out.substring(1);
}
示例9: testEntityBQTransform_toTableRow
import com.google.datastore.v1.Key.PathElement; //导入依赖的package包/类
@Test
public void testEntityBQTransform_toTableRow() throws IOException {
Entity e;
TableRow r;
EntityBQTransform ebt;
e = Entity.newBuilder()
.setKey(Key.newBuilder()
.setPartitionId(PartitionId.newBuilder()
.setProjectId("my-awesome-project"))
.addPath(PathElement.newBuilder()
.setKind("SomeKind")
.setName("myKey")))
.putProperties("firstName", Value.newBuilder()
.setStringValue("Colin").build())
.putProperties("age", Value.newBuilder()
.setIntegerValue(25).build())
// Weight is purposly set as a string to test strict v non strict cast
.putProperties("weight", Value.newBuilder()
.setStringValue("195").build())
.putProperties("vacations", Value.newBuilder().setArrayValue(ArrayValue.newBuilder()
.addValues(Value.newBuilder().setEntityValue(Entity.newBuilder()
.putProperties("place", Value.newBuilder().setStringValue("Atlanta").build())
.putProperties("time", Value.newBuilder().setTimestampValue(
Timestamp.newBuilder().setNanos(1000).setSeconds(1498692500).build()).build())
))
.addValues(Value.newBuilder().setEntityValue(Entity.newBuilder()
.putProperties("place", Value.newBuilder().setStringValue("New York").build())
.putProperties("time", Value.newBuilder().setTimestampValue(
Timestamp.newBuilder().setNanos(1000).setSeconds(1498692500).build()).build())
))).build())
.build();
// handle strict case first
ebt = EntityBQTransform.newBuilder()
.setStrictCast(true)
.setRowSchema(exampleTable().getFields())
.build();
r = ebt.toTableRow(e);
Assert.assertEquals("Colin", r.get("firstName"));
Assert.assertEquals(25L, r.get("age"));
Assert.assertEquals("key(SomeKind, 'myKey')", r.get("__key__"));
Assert.assertEquals(null, r.get("weight"));
// handle non strict casting
ebt = EntityBQTransform.newBuilder()
.setStrictCast(false)
.setRowSchema(exampleTable().getFields())
.build();
r = ebt.toTableRow(e);
Assert.assertEquals("Colin", r.get("firstName"));
Assert.assertEquals(25L, r.get("age"));
Assert.assertEquals("key(SomeKind, 'myKey')", r.get("__key__"));
Assert.assertEquals(195.0, r.get("weight"));
}
示例10: makeKey
import com.google.datastore.v1.Key.PathElement; //导入依赖的package包/类
/**
* Make a key from the specified path of kind/id-or-name pairs
* and/or Keys.
*
* <p>The id-or-name values must be either String, Long, Integer or Short.
*
* <p>The last id-or-name value may be omitted, in which case an entity without
* an id is created (for use with automatic id allocation).
*
* <p>The PartitionIds of all Keys in the path must be equal. The returned
* Key.Builder will use this PartitionId.
*/
public static Key.Builder makeKey(Object... elements) {
Key.Builder key = Key.newBuilder();
PartitionId partitionId = null;
for (int pathIndex = 0; pathIndex < elements.length; pathIndex += 2) {
PathElement.Builder pathElement = PathElement.newBuilder();
Object element = elements[pathIndex];
if (element instanceof Key) {
Key subKey = (Key) element;
if (partitionId == null) {
partitionId = subKey.getPartitionId();
} else if (!partitionId.equals(subKey.getPartitionId())) {
throw new IllegalArgumentException("Partition IDs did not match, found: "
+ partitionId + " and " + subKey.getPartitionId());
}
key.addAllPath(((Key) element).getPathList());
// We increment by 2, but since we got a Key argument we're only consuming 1 element in this
// iteration of the loop. Decrement the index so that when we jump by 2 we end up in the
// right spot.
pathIndex--;
} else {
String kind;
try {
kind = (String) element;
} catch (ClassCastException e) {
throw new IllegalArgumentException("Expected string or Key, got: " + element.getClass());
}
pathElement.setKind(kind);
if (pathIndex + 1 < elements.length) {
Object value = elements[pathIndex + 1];
if (value instanceof String) {
pathElement.setName((String) value);
} else if (value instanceof Long) {
pathElement.setId((Long) value);
} else if (value instanceof Integer) {
pathElement.setId((Integer) value);
} else if (value instanceof Short) {
pathElement.setId((Short) value);
} else {
throw new IllegalArgumentException(
"Expected string or integer, got: " + value.getClass());
}
}
key.addPath(pathElement);
}
}
if (partitionId != null && !partitionId.equals(PartitionId.getDefaultInstance())) {
key.setPartitionId(partitionId);
}
return key;
}