Skip to content

Combinig cosmos db samples #708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ az acr create --resource-group cosmosdb-springboot-aks-rg --location eastus \
```bash
git clone https://github.com/Azure-Samples/azure-spring-boot-samples.git

cd cosmos/spring-cloud-azure-starter-data-cosmos/spring-cloud-azure-data-cosmos-aks-sample
cd cosmos/spring-cloud-azure-starter-data-cosmos/spring-cloud-azure-data-cosmos-sample
```

1. Use Maven to build the application.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package spring.cloud.azure.starter.data.cosmos.sample.aks;
package spring.cloud.azure.starter.data.cosmos.sample;

import com.azure.cosmos.CosmosException;
import org.springframework.core.Ordered;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package spring.cloud.azure.starter.data.cosmos.sample.aks;
package spring.cloud.azure.starter.data.cosmos.sample;

import org.springframework.boot.context.properties.ConfigurationProperties;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package spring.cloud.azure.starter.data.cosmos.sample.aks;
package spring.cloud.azure.starter.data.cosmos.sample;

import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.DirectConnectionConfig;
Expand All @@ -16,7 +16,6 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.lang.Nullable;

@Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.spring.sample.cosmos;
package spring.cloud.azure.starter.data.cosmos.sample;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -9,54 +9,40 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Optional;

@SpringBootApplication
public class CosmosSampleApplication implements CommandLineRunner {

private static final Logger LOGGER = LoggerFactory.getLogger(CosmosSampleApplication.class);
public class CosmosdbSpringApplication implements CommandLineRunner {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class CosmosdbSpringApplication implements CommandLineRunner {
public class CosmosDBSpringApplication implements CommandLineRunner {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thanks!

private static final Logger LOGGER = LoggerFactory.getLogger(CosmosdbSpringApplication.class);

@Autowired
private UserRepository repository;

public static void main(String[] args) {
SpringApplication.run(CosmosSampleApplication.class, args);
SpringApplication.run(CosmosdbSpringApplication.class, args);
}

@Override
public void run(String... var1) {
this.repository.deleteAll().block();
this.repository.deleteAll();
LOGGER.info("Deleted all data in container.");

final User testUser = new User("testId", "testFirstName", "testLastName", "test address line one");

// Save the User class to Azure Cosmos DB database.
final Mono<User> saveUserMono = repository.save(testUser);

final Flux<User> firstNameUserFlux = repository.findByFirstName("testFirstName");
final User savedUser = repository.save(testUser);

// Nothing happens until we subscribe to these Monos.
// findById will not return the user as user is not present.
final Mono<User> findByIdMono = repository.findById(testUser.getId());
final User findByIdUser = findByIdMono.block();
Assert.isNull(findByIdUser, "User must be null");

final User savedUser = saveUserMono.block();
Assert.state(savedUser != null, "Saved user must not be null");
Assert.state(savedUser.getFirstName().equals(testUser.getFirstName()), "Saved user first name doesn't match");

firstNameUserFlux.collectList().block();

final Optional<User> optionalUserResult = repository.findById(testUser.getId()).blockOptional();
final Optional<User> optionalUserResult = repository.findById(testUser.getEmail());
Assert.isTrue(optionalUserResult.isPresent(), "Cannot find user.");

final User result = optionalUserResult.get();
Assert.state(result.getFirstName().equals(testUser.getFirstName()), "query result firstName doesn't match!");
Assert.state(result.getLastName().equals(testUser.getLastName()), "query result lastName doesn't match!");

LOGGER.info("findOne in User collection get result: {}", result.toString());
LOGGER.info("spring-cloud-azure-data-cosmos-sample successfully run.");
LOGGER.info("spring-cloud-azure-data-cosmos-sample-aks successfully run.");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we adding -aks here?

Copy link
Author

@M-Lipin M-Lipin Jul 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was added accidentally when moving from aks sample. Removed. Thanks!

}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package spring.cloud.azure.starter.data.cosmos.sample.aks;
package spring.cloud.azure.starter.data.cosmos.sample;

import com.azure.spring.data.cosmos.core.mapping.Container;
import com.azure.spring.data.cosmos.core.mapping.PartitionKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package spring.cloud.azure.starter.data.cosmos.sample.aks;
package spring.cloud.azure.starter.data.cosmos.sample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package spring.cloud.azure.starter.data.cosmos.sample.aks;
package spring.cloud.azure.starter.data.cosmos.sample;

import com.azure.spring.data.cosmos.repository.CosmosRepository;
import org.springframework.stereotype.Repository;
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.