本文整理了Java中com.google.cloud.Timestamp.now()
方法的一些代码示例,展示了Timestamp.now()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Timestamp.now()
方法的具体详情如下:
包路径:com.google.cloud.Timestamp
类名称:Timestamp
方法名:now
[英]Creates an instance with current time.
[中]使用当前时间创建实例。
代码示例来源:origin: googleapis/google-cloud-java
/**
* Returns the current count of all documents, including the changes from the current changeMap.
*/
private int currentSize() {
ChangeSet changeSet = extractChanges(Timestamp.now());
return documentSet.size() + changeSet.adds.size() - changeSet.deletes.size();
}
代码示例来源:origin: google/data-transfer-project
private Entity createEntity(Key key, Map<String, Object> data) throws IOException {
Entity.Builder builder = Entity.newBuilder(key).set(CREATED_FIELD, Timestamp.now());
for (Entry<String, Object> entry : data.entrySet()) {
if (entry.getValue() instanceof String) {
builder.set(entry.getKey(), (String) entry.getValue()); // StringValue
} else if (entry.getValue() instanceof Integer) {
builder.set(entry.getKey(), (Integer) entry.getValue()); // LongValue
} else if (entry.getValue() instanceof Double) {
builder.set(entry.getKey(), (Double) entry.getValue()); // DoubleValue
} else if (entry.getValue() instanceof Boolean) {
builder.set(entry.getKey(), (Boolean) entry.getValue()); // BooleanValue
} else if (entry.getValue() instanceof Timestamp) {
builder.set(entry.getKey(), (Timestamp) entry.getValue()); // TimestampValue
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
out.writeObject(entry.getValue());
}
builder.set(entry.getKey(), Blob.copyFrom(bos.toByteArray())); // BlobValue
}
}
return builder.build();
}
代码示例来源:origin: google/data-transfer-project
@Override
public <T extends DataModel> void create(UUID jobId, String key, T model) throws IOException {
Preconditions.checkNotNull(jobId);
Transaction transaction = datastore.newTransaction();
Key fullKey = getDataKey(jobId, key);
Entity shouldNotExist = transaction.get(fullKey);
if (shouldNotExist != null) {
transaction.rollback();
throw new IOException(
"Record already exists for key: " + fullKey.getName() + ". Record: " + shouldNotExist);
}
String serialized = objectMapper.writeValueAsString(model);
Entity entity = Entity.newBuilder(fullKey)
.set(CREATED_FIELD, Timestamp.now())
.set(model.getClass().getName(), serialized)
.build();
try {
transaction.put(entity);
} catch (DatastoreException e) {
throw new IOException(
"Could not create initial record for jobID: " + jobId + ". Record: " + entity, e);
}
transaction.commit();
}
代码示例来源:origin: google/data-transfer-project
@Override
public <T extends DataModel> void update(UUID jobId, String key, T model) {
Transaction transaction = datastore.newTransaction();
Key entityKey = getDataKey(jobId, key);
try {
Entity previousEntity = transaction.get(entityKey);
if (previousEntity == null) {
throw new IOException("Could not find record for data key: " + entityKey.getName());
}
String serialized = objectMapper.writeValueAsString(model);
Entity entity = Entity.newBuilder(entityKey)
.set(CREATED_FIELD, Timestamp.now())
.set(model.getClass().getName(), serialized)
.build();
transaction.put(entity);
transaction.commit();
} catch (IOException t) {
transaction.rollback();
throw new RuntimeException("Failed atomic update of key: " + key, t);
}
}
代码示例来源:origin: googleapis/google-cloud-java
public static void main(String... args) {
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
Key key = keyFactory.newKey("keyName");
Entity entity = datastore.get(key);
if (entity != null) {
System.out.println("Updating access_time for " + entity.getString("name"));
entity = Entity.newBuilder(entity).set("access_time", Timestamp.now()).build();
datastore.update(entity);
}
}
}
代码示例来源:origin: googleapis/google-cloud-java
@Test
public void serialization() throws Exception {
reserializeAndAssert(TimestampBound.strong());
reserializeAndAssert(TimestampBound.ofExactStaleness(10, TimeUnit.NANOSECONDS));
reserializeAndAssert(TimestampBound.ofMaxStaleness(100, TimeUnit.DAYS));
reserializeAndAssert(TimestampBound.ofMinReadTimestamp(Timestamp.now()));
reserializeAndAssert(TimestampBound.ofReadTimestamp(Timestamp.now()));
}
代码示例来源:origin: googleapis/google-cloud-java
public static void main(String... args) {
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
Key key = keyFactory.newKey("keyName");
Entity entity =
Entity.newBuilder(key)
.set("name", "John Doe")
.set("age", 30)
.set("access_time", Timestamp.now())
.build();
datastore.put(entity);
}
}
代码示例来源:origin: googleapis/google-cloud-java
@Override
public void run(Transaction tx, Key userKey, String content) {
Entity user = tx.get(userKey);
if (user == null) {
System.out.println("Adding a new user.");
user = Entity.newBuilder(userKey).set("count", 1).build();
tx.add(user);
} else {
user = Entity.newBuilder(user).set("count", user.getLong("count") + 1L).build();
tx.update(user);
}
IncompleteKey commentKey = IncompleteKey.newBuilder(userKey, COMMENT_KIND).build();
FullEntity<IncompleteKey> comment =
FullEntity.newBuilder(commentKey)
.set("content", content)
.set("timestamp", Timestamp.now())
.build();
tx.addWithDeferredIdAllocation(comment);
System.out.printf("Adding a comment to user '%s'.%n", userKey.getName());
}
代码示例来源:origin: googleapis/google-cloud-java
@Before
public void setUp() {
builder = new Builder();
builder.set("blob", BLOB).set("boolean", true).set("timestamp", TIMESTAMP);
builder.set("double", 1.25).set("key", KEY).set("string", "hello world");
builder.set("long", 125).setNull("null").set("entity", ENTITY).set("latLng", LAT_LNG);
builder.set("partialEntity", PARTIAL_ENTITY).set("stringValue", StringValue.of("bla"));
builder.set("list1", NullValue.of(), StringValue.of("foo"), LatLngValue.of(LAT_LNG));
builder.set("list2", ImmutableList.of(LongValue.of(10), DoubleValue.of(2)));
builder.set("list3", Collections.singletonList(BooleanValue.of(true)));
builder.set(
"blobList", BLOB, Blob.copyFrom(new byte[] {3, 4}), Blob.copyFrom(new byte[] {5, 6}));
builder.set("booleanList", true, false, true);
builder.set("timestampList", Timestamp.now(), Timestamp.now(), Timestamp.now());
builder.set("doubleList", 12.3, 4.56, .789);
builder.set(
"keyList",
KEY,
Key.newBuilder("ds2", "k2", "n2").build(),
Key.newBuilder("ds3", "k3", "n3").build());
builder.set("entityList", ENTITY, PARTIAL_ENTITY);
builder.set("stringList", "s1", "s2", "s3");
builder.set("longList", 1, 23, 456);
builder.set("latLngList", LAT_LNG, LAT_LNG);
}
代码示例来源:origin: googleapis/google-cloud-java
tester.addEqualityGroup(
Value.timestamp(Value.COMMIT_TIMESTAMP), Value.timestamp(Value.COMMIT_TIMESTAMP));
Timestamp now = Timestamp.now();
tester.addEqualityGroup(Value.timestamp(now), Value.timestamp(now));
tester.addEqualityGroup(Value.timestamp(Timestamp.ofTimeMicroseconds(0)));
代码示例来源:origin: googleapis/google-cloud-java
reserializeAndAssert(Value.timestamp(Timestamp.now()));
reserializeAndAssert(Value.timestampArray(Arrays.asList(null, Timestamp.now())));
代码示例来源:origin: GoogleCloudPlatform/java-docs-samples
/**
* Adds a task entity to the Datastore.
*
* @param description The task description
* @return The {@link Key} of the entity
* @throws DatastoreException if the ID allocation or put fails
*/
Key addTask(String description) {
Key key = datastore.allocateId(keyFactory.newKey());
Entity task = Entity.newBuilder(key)
.set("description", StringValue.newBuilder(description).setExcludeFromIndexes(true).build())
.set("created", Timestamp.now())
.set("done", false)
.build();
datastore.put(task);
return key;
}
// [END datastore_add_entity]
代码示例来源:origin: com.google.cloud/google-cloud-firestore
/**
* Returns the current count of all documents, including the changes from the current changeMap.
*/
private int currentSize() {
ChangeSet changeSet = extractChanges(Timestamp.now());
return documentSet.size() + changeSet.adds.size() - changeSet.deletes.size();
}
代码示例来源:origin: GoogleCloudPlatform/java-docs-samples
final Timestamp timestamp = Timestamp.now();
代码示例来源:origin: org.apache.beam/beam-sdks-java-io-google-cloud-platform
.withInstanceId("123")
.withDatabaseId("aaa")
.withTimestamp(Timestamp.now())
.withTable("users")
.withColumns("id", "name")
代码示例来源:origin: org.apache.beam/beam-sdks-java-io-google-cloud-platform
@Test
public void dates() throws Exception {
Mutation timestamp =
Mutation.newInsertOrUpdateBuilder("test").set("one").to(Timestamp.now()).build();
Mutation nullTimestamp =
Mutation.newInsertOrUpdateBuilder("test").set("one").to((Timestamp) null).build();
Mutation.newInsertOrUpdateBuilder("test")
.set("one")
.toTimestampArray(Arrays.asList(Timestamp.now(), null))
.build();
Mutation dateArray =
内容来源于网络,如有侵权,请联系作者删除!