com.google.cloud.datastore.DatastoreOptions Java Examples

The following examples show how to use com.google.cloud.datastore.DatastoreOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DatastoreCleanupTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void deleteExpiredDatastoreTestNamespaces() {
  final Datastore datastore = DatastoreOptions.newBuilder()
      .setProjectId("styx-oss-test")
      .build()
      .getService();

  var expiredNamespaces = new ArrayList<String>();
  datastore.run(KeyQuery.newKeyQueryBuilder().setKind("__namespace__").build())
      .forEachRemaining(k -> {
        if (k.hasName() && isExpiredTestNamespace(k.getName(), NOW)) {
          expiredNamespaces.add(k.getName());
        }
      });

  for (var namespace : expiredNamespaces) {
    log.info("Deleting expired datastore test namespace: {}", namespace);
    try {
      deleteDatastoreNamespace(datastore, namespace);
    } catch (Exception e) {
      log.error("Failed to delete expired datastore test namespace: {}", namespace);
    }
  }
}
 
Example #2
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@Override
public void init(FilterConfig config) throws ServletException {
  // initialize local copy of datastore session variables

  datastore = DatastoreOptions.getDefaultInstance().getService();
  keyFactory = datastore.newKeyFactory().setKind("SessionVariable");
  // Delete all sessions unmodified for over two days
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  Query<Entity> query = Query.newEntityQueryBuilder()
      .setKind("SessionVariable")
      .setFilter(PropertyFilter.le("lastModified", dt.minusDays(2).toString(dtf)))
      .build();
  QueryResults<Entity> resultList = datastore.run(query);
  while (resultList.hasNext()) {
    Entity stateEntity = resultList.next();
    datastore.delete(stateEntity.getKey());
  }
}
 
Example #3
Source File: EntityManagerFactoryTest.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateEntityManager_ConnectionParameters7() throws FileNotFoundException {
  ConnectionParameters parameters = new ConnectionParameters();
  final String projectId = "my-project";
  final String namespace = "my-namespace";
  final String credentialsFile = System.getenv(TestUtils.ENV_CREDENTIALS);
  if (Utility.isNullOrEmpty(credentialsFile)) {
    System.out.printf("Enviornment variable %s is not set, skipping the test case%n",
        TestUtils.ENV_CREDENTIALS);
    return;
  }
  parameters.setProjectId(projectId);
  parameters.setNamespace(namespace);
  parameters.setJsonCredentialsStream(new FileInputStream(credentialsFile));
  parameters.setJsonCredentialsFile("nonexistentfile.json");
  EntityManagerFactory emf = EntityManagerFactory.getInstance();
  DefaultEntityManager em = (DefaultEntityManager) emf.createEntityManager(parameters);
  DatastoreOptions options = em.getDatastore().getOptions();
  assertEquals(ConnectionParameters.DEFAULT_SERVICE_URL, options.getHost());
  assertNotNull(options.getProjectId());
  assertTrue(options.getProjectId().length() > 0);
  assertEquals(ServiceAccountCredentials.class, options.getCredentials().getClass());
  assertEquals(projectId, options.getProjectId());
  assertEquals(namespace, options.getNamespace());
}
 
Example #4
Source File: EntityManagerFactoryTest.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateEntityManager_ConnectionParameters6() throws FileNotFoundException {
  ConnectionParameters parameters = new ConnectionParameters();
  final String projectId = "my-project";
  final String namespace = "my-namespace";
  final String credentialsFile = System.getenv(TestUtils.ENV_CREDENTIALS);
  if (Utility.isNullOrEmpty(credentialsFile)) {
    System.out.printf("Enviornment variable %s is not set, skipping the test case%n",
        TestUtils.ENV_CREDENTIALS);
    return;
  }
  parameters.setProjectId(projectId);
  parameters.setNamespace(namespace);
  parameters.setJsonCredentialsStream(new FileInputStream(credentialsFile));
  EntityManagerFactory emf = EntityManagerFactory.getInstance();
  DefaultEntityManager em = (DefaultEntityManager) emf.createEntityManager(parameters);
  DatastoreOptions options = em.getDatastore().getOptions();
  assertEquals(ConnectionParameters.DEFAULT_SERVICE_URL, options.getHost());
  assertNotNull(options.getProjectId());
  assertTrue(options.getProjectId().length() > 0);
  assertEquals(ServiceAccountCredentials.class, options.getCredentials().getClass());
  assertEquals(projectId, options.getProjectId());
  assertEquals(namespace, options.getNamespace());
}
 
Example #5
Source File: EntityManagerFactoryTest.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateEntityManager_ConnectionParameters5() {
  ConnectionParameters parameters = new ConnectionParameters();
  final String projectId = "my-project";
  final String namespace = "my-namespace";
  final String credentialsFile = System.getenv(TestUtils.ENV_CREDENTIALS);
  if (Utility.isNullOrEmpty(credentialsFile)) {
    System.out.printf("Enviornment variable %s is not set, skipping the test case%n",
        TestUtils.ENV_CREDENTIALS);
    return;
  }
  parameters.setProjectId(projectId);
  parameters.setNamespace(namespace);
  parameters.setJsonCredentialsFile(credentialsFile);
  EntityManagerFactory emf = EntityManagerFactory.getInstance();
  DefaultEntityManager em = (DefaultEntityManager) emf.createEntityManager(parameters);
  DatastoreOptions options = em.getDatastore().getOptions();
  assertEquals(ConnectionParameters.DEFAULT_SERVICE_URL, options.getHost());
  assertNotNull(options.getProjectId());
  assertTrue(options.getProjectId().length() > 0);
  assertEquals(ServiceAccountCredentials.class, options.getCredentials().getClass());
  assertEquals(projectId, options.getProjectId());
  assertEquals(namespace, options.getNamespace());
}
 
Example #6
Source File: GcpDatastoreEmulatorAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void testDatastoreOptionsCorrectlySet() {
	new ApplicationContextRunner()
			.withConfiguration(AutoConfigurations.of(
					GcpDatastoreEmulatorAutoConfiguration.class))
			.withPropertyValues(
					"spring.cloud.gcp.datastore.emulator.port=8182",
					"spring.cloud.gcp.datastore.emulator.enabled=true",
					"spring.cloud.gcp.datastore.emulator.consistency=0.8")
			.run((context) -> {
				LocalDatastoreHelper helper = context.getBean(LocalDatastoreHelper.class);
				DatastoreOptions datastoreOptions = helper.getOptions();
				assertThat(datastoreOptions.getHost()).isEqualTo("localhost:8182");
				assertThat(helper.getConsistency()).isEqualTo(0.8D);
			});
}
 
Example #7
Source File: EntityManagerFactory.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
/**
 * Creates and returns an {@link EntityManager} using the specified connection parameters.
 * 
 * @param parameters
 *          the connection parameters
 * @return a new {@link EntityManager} created using the specified connection parameters.
 * @throws EntityManagerException
 *           if any error occurs while creating the EntityManager.
 */
public EntityManager createEntityManager(ConnectionParameters parameters) {
  try {
    DatastoreOptions.Builder datastoreOptionsBuilder = DatastoreOptions.newBuilder();
    datastoreOptionsBuilder.setHost(parameters.getServiceURL());
    datastoreOptionsBuilder.setTransportOptions(getHttpTransportOptions(parameters));
    String projectId = parameters.getProjectId();
    if (!Utility.isNullOrEmpty(projectId)) {
      datastoreOptionsBuilder.setProjectId(projectId);
    }
    String namespace = parameters.getNamespace();
    if (namespace != null) {
      datastoreOptionsBuilder.setNamespace(namespace);
    }
    datastoreOptionsBuilder.setCredentials(getCredentials(parameters));
    Datastore datastore = datastoreOptionsBuilder.build().getService();
    return new DefaultEntityManager(datastore);
  } catch (Exception exp) {
    throw new EntityManagerFactoryException(exp);
  }
}
 
Example #8
Source File: EntityManagerFactoryTest.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateEntityManager_ConnectionParameters4() {
  if (TestUtils.isCI()) {
    // TODO
    return;
  }
  ConnectionParameters parameters = new ConnectionParameters();
  final String projectId = "my-project";
  final String namespace = "my-namespace";
  parameters.setProjectId(projectId);
  parameters.setNamespace(namespace);
  EntityManagerFactory emf = EntityManagerFactory.getInstance();
  DefaultEntityManager em = (DefaultEntityManager) emf.createEntityManager(parameters);
  DatastoreOptions options = em.getDatastore().getOptions();
  assertEquals(ConnectionParameters.DEFAULT_SERVICE_URL, options.getHost());
  assertNotNull(options.getProjectId());
  assertTrue(options.getProjectId().length() > 0);
  assertNotNull(options.getCredentials());
  assertEquals(projectId, options.getProjectId());
  assertEquals(namespace, options.getNamespace());
}
 
Example #9
Source File: EntityManagerFactoryTest.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateLocalEntityManager1() {
  try {
    EntityManagerFactory emf = EntityManagerFactory.getInstance();
    EntityManager em = emf.createLocalEntityManager("localhost:9999");
    DefaultEntityManager dem = (DefaultEntityManager) em;
    DatastoreOptions options = dem.getDatastore().getOptions();
    assertEquals("localhost:9999", options.getHost());
    assertNotNull(options.getProjectId());
    assertEquals("", options.getNamespace());
  } catch (EntityManagerFactoryException exp) {
    if (!TestUtils.isCI()) {
      throw exp;
    }
  }
}
 
Example #10
Source File: EntityManagerFactoryTest.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateEntityManager_ConnectionParameters3() {
  ConnectionParameters parameters = new ConnectionParameters();
  final String serviceURL = "http://localhost:9999";
  final String projectId = "my-project";
  final String namespace = "my-namespace";
  parameters.setServiceURL(serviceURL);
  parameters.setProjectId(projectId);
  parameters.setNamespace(namespace);
  EntityManagerFactory emf = EntityManagerFactory.getInstance();
  DefaultEntityManager em = (DefaultEntityManager) emf.createEntityManager(parameters);
  DatastoreOptions options = em.getDatastore().getOptions();
  assertEquals(serviceURL, options.getHost());
  assertNotNull(options.getProjectId());
  assertTrue(options.getProjectId().length() > 0);
  assertNotNull(options.getCredentials());
  assertEquals(projectId, options.getProjectId());
  assertEquals(namespace, options.getNamespace());
}
 
Example #11
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@Override
public void init(FilterConfig config) throws ServletException {
  // initialize local copy of datastore session variables

  datastore = DatastoreOptions.getDefaultInstance().getService();
  keyFactory = datastore.newKeyFactory().setKind("SessionVariable");
  // Delete all sessions unmodified for over two days
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  Query<Entity> query = Query.newEntityQueryBuilder()
      .setKind("SessionVariable")
      .setFilter(PropertyFilter.le("lastModified", dt.minusDays(2).toString(dtf)))
      .build();
  QueryResults<Entity> resultList = datastore.run(query);
  while (resultList.hasNext()) {
    Entity stateEntity = resultList.next();
    datastore.delete(stateEntity.getKey());
  }
}
 
Example #12
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@Override
public void init(FilterConfig config) throws ServletException {
  // initialize local copy of datastore session variables

  datastore = DatastoreOptions.getDefaultInstance().getService();
  keyFactory = datastore.newKeyFactory().setKind("SessionVariable");
  // Delete all sessions unmodified for over two days
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  Query<Entity> query = Query.newEntityQueryBuilder()
      .setKind("SessionVariable")
      .setFilter(PropertyFilter.le("lastModified", dt.minusDays(2).toString(dtf)))
      .build();
  QueryResults<Entity> resultList = datastore.run(query);
  while (resultList.hasNext()) {
    Entity stateEntity = resultList.next();
    datastore.delete(stateEntity.getKey());
  }
}
 
Example #13
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@Override
public void init(FilterConfig config) throws ServletException {
  // initialize local copy of datastore session variables

  datastore = DatastoreOptions.getDefaultInstance().getService();
  keyFactory = datastore.newKeyFactory().setKind("SessionVariable");
  // Delete all sessions unmodified for over two days
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  Query<Entity> query = Query.newEntityQueryBuilder()
      .setKind("SessionVariable")
      .setFilter(PropertyFilter.le("lastModified", dt.minusDays(2).toString(dtf)))
      .build();
  QueryResults<Entity> resultList = datastore.run(query);
  while (resultList.hasNext()) {
    Entity stateEntity = resultList.next();
    datastore.delete(stateEntity.getKey());
  }
}
 
Example #14
Source File: Main.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Starts the webapp on localhost:8080.
 */
public static void main(String[] args) {
  port(8080);
  String kind = "DemoUser";
  if (args != null) {
    for (String arg : args) {
      if (arg.startsWith("kind=")) {
        kind = arg.substring("kind=".length());
      }
    }
  }
  UserController userController = new UserController(new UserService(
      DatastoreOptions.getDefaultInstance().getService(), kind));
}
 
Example #15
Source File: RemoteSessionIntegrationTest.java    From jetty-runtime with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  datastore = DatastoreOptions.newBuilder()
      .setProjectId(System.getProperty("app.deploy.project"))
      .build()
      .getService();
  keyFactory = datastore.newKeyFactory().setKind("GCloudSession");
  objectMapper = new ObjectMapper();
}
 
Example #16
Source File: GoogleCloudIdempotentImportExecutorExtension.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
private synchronized Datastore getDatastore() throws IOException {
  if (datastore == null) {
    datastore = DatastoreOptions.newBuilder()
        .setProjectId(GoogleCloudUtils.getProjectId())
        .setCredentials(GoogleCredentials.getApplicationDefault())
        .build()
        .getService();
  }

  return datastore;
}
 
Example #17
Source File: QuickstartSample.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String... args) throws Exception {
  // Instantiates a client
  Datastore datastore = DatastoreOptions.getDefaultInstance().getService();

  // The kind for the new entity
  String kind = "Task";
  // The name/ID for the new entity
  String name = "sampletask1";
  // The Cloud Datastore key for the new entity
  Key taskKey = datastore.newKeyFactory().setKind(kind).newKey(name);

  // Prepares the new entity
  Entity task = Entity.newBuilder(taskKey)
      .set("description", "Buy milk")
      .build();

  // Saves the entity
  datastore.put(task);

  System.out.printf("Saved %s: %s%n", task.getKey().getName(), task.getString("description"));

  //Retrieve entity
  Entity retrieved = datastore.get(taskKey);

  System.out.printf("Retrieved %s: %s%n", taskKey.getName(), retrieved.getString("description"));

}
 
Example #18
Source File: QuickstartSampleIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static final void deleteTestEntity() {
  Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
  String kind = "Task";
  String name = "sampletask1";
  Key taskKey = datastore.newKeyFactory().setKind(kind).newKey(name);
  datastore.delete(taskKey);
}
 
Example #19
Source File: Persistence.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("JavadocMethod")
public static Datastore getDatastore() {
  if (datastore.get() == null) {
    datastore.set(
        DatastoreOptions.newBuilder().setProjectId("your-project-id-here").build().getService());
  }

  return datastore.get();
}
 
Example #20
Source File: DatastoreStore.java    From tomcat-runtime with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Initiate a connection to the Datastore.</p>
 *
 */
@Override
protected synchronized void startInternal() throws LifecycleException {
  log.debug("Initialization of the Datastore Store");

  this.clock = Clock.systemUTC();
  this.datastore = DatastoreOptions.newBuilder().setNamespace(namespace).build().getService();

  super.startInternal();
}
 
Example #21
Source File: UpdateEntity.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example #22
Source File: CreateEntity.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #23
Source File: ITQuerySnippets.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() {
  datastore = DatastoreOptions.getDefaultInstance().getService();
  Key key1 = Key.newBuilder(datastore.getOptions().getProjectId(), KIND, "key1").build();
  Key key2 = Key.newBuilder(datastore.getOptions().getProjectId(), KIND, "key2").build();
  entity1 = Entity.newBuilder(key1).set("description", "entity1").build();
  entity2 = Entity.newBuilder(key2).set("description", "entity2").build();
  datastore.put(entity1, entity2);
}
 
Example #24
Source File: DatastoreIntegrationTestConfiguration.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Bean
public Datastore datastore() {
	DatastoreOptions.Builder builder = DatastoreOptions.newBuilder()
			.setProjectId(this.projectId)
			.setHeaderProvider(new UserAgentHeaderProvider(this.getClass()))
			.setCredentials(this.credentials);
	if (this.namespacePrefix != null) {
		builder.setNamespace(this.namespacePrefix + System.currentTimeMillis());
	}
	return builder.build().getService();
}
 
Example #25
Source File: GcpDatastoreAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testDatastoreEmulatorCredentialsConfig() {
	this.contextRunner.run((context) -> {
		CredentialsProvider defaultCredentialsProvider = context.getBean(CredentialsProvider.class);
		assertThat(defaultCredentialsProvider).isNotInstanceOf(NoCredentialsProvider.class);

		DatastoreOptions datastoreOptions = getDatastoreBean(context).getOptions();
		assertThat(datastoreOptions.getCredentials()).isInstanceOf(NoCredentials.class);
	});
}
 
Example #26
Source File: EntityManagerFactoryTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateLocalEntityManager2() {
  EntityManagerFactory emf = EntityManagerFactory.getInstance();
  EntityManager em = emf.createLocalEntityManager("localhost:9999", "cool-project");
  DefaultEntityManager dem = (DefaultEntityManager) em;
  DatastoreOptions options = dem.getDatastore().getOptions();
  assertEquals("localhost:9999", options.getHost());
  assertEquals("cool-project", options.getProjectId());
  assertEquals("", options.getNamespace());
}
 
Example #27
Source File: GcpDatastoreAutoConfigurationTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testDatastoreOptionsCorrectlySet() {
	this.contextRunner.run((context) -> {
		DatastoreOptions datastoreOptions = getDatastoreBean(context).getOptions();
		assertThat(datastoreOptions.getProjectId()).isEqualTo("test-project");
		assertThat(datastoreOptions.getNamespace()).isEqualTo("testNamespace");
		assertThat(datastoreOptions.getHost()).isEqualTo("localhost:8081");
	});
}
 
Example #28
Source File: GcpDatastoreAutoConfiguration.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private Datastore getDatastore(String namespace) {
	DatastoreOptions.Builder builder = DatastoreOptions.newBuilder()
			.setProjectId(this.projectId)
			.setHeaderProvider(new UserAgentHeaderProvider(this.getClass()))
			.setCredentials(this.credentials);
	if (namespace != null) {
		builder.setNamespace(namespace);
	}

	if (this.host != null) {
		builder.setHost(this.host);
	}
	return builder.build().getService();
}
 
Example #29
Source File: EntityManagerFactoryTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateLocalEntityManager3() {
  EntityManagerFactory emf = EntityManagerFactory.getInstance();
  EntityManager em = emf.createLocalEntityManager("localhost:9999", "cool-project",
      "scret-namespace");
  DefaultEntityManager dem = (DefaultEntityManager) em;
  DatastoreOptions options = dem.getDatastore().getOptions();
  assertEquals("localhost:9999", options.getHost());
  assertEquals("cool-project", options.getProjectId());
  assertEquals("scret-namespace", options.getNamespace());
}
 
Example #30
Source File: EntityManagerFactoryTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateEntityManager_ConnectionParameters1() {
  if (TestUtils.isCI()) {
    // TODO
    return;
  }
  ConnectionParameters parameters = new ConnectionParameters();
  EntityManagerFactory emf = EntityManagerFactory.getInstance();
  DefaultEntityManager em = (DefaultEntityManager) emf.createEntityManager(parameters);
  DatastoreOptions options = em.getDatastore().getOptions();
  assertEquals(ConnectionParameters.DEFAULT_SERVICE_URL, options.getHost());
  assertNotNull(options.getProjectId());
  assertTrue(options.getProjectId().length() > 0);

}