Java – Azure Cosmos DB (SQL) : Save results of select into a text file No.72

I will show Java code that save results of select query into a text file.

▼1. Prerequisites

1-1. Free Azure Cosmos DB Account

You can use trial free Cosmos DB Account
https://azure.microsoft.com/ja-jp/try/cosmosdb/

1-2. Installing Visual Studio Code

sudo snap install --classic code

https://code.visualstudio.com/docs/setup/linux

1-3. Installing Maven

Ref: Apache Kafka Word Count 実装 – Java No.44
line of 2-3

1-4. Creating sample data

Ref: Java Azure Cosmos DB (SQL API) Database の作成方法 No.14

GeneratingData

▼2. Java code for saving results of select query on Cosmos DB into a text file

2-1. Making a directory

Making a directory to create an application.

mkidr cosmosdbtest
cd cosmosdbtest

2-2. Deploying Apache Maven project

cd cosmosdbtest
mvn archetype:generate  -DinteractiveMode=false -DgroupId=org.example.cosmosdb.sql -DartifactId=sqldemo -DarchetypeArtifactId=maven-archetype-quickstart
(a part of stdout)
xxxx
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< org.apache.maven:standalone-pom >--------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]----------------------------
[INFO] 
[INFO] >>> maven-archetype-plugin:3.2.0:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO] 
[INFO] <<< maven-archetype-plugin:3.2.0:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO] 
[INFO] 
[INFO] --- maven-archetype-plugin:3.2.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] -------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] -------------------------------------------------------------------
[INFO] Parameter: basedir, Value: /home/xxx/cosmosdbtest/sqldemo
[INFO] Parameter: package, Value: org.example.cosmosdb.sql
[INFO] Parameter: groupId, Value: org.example.cosmosdb.sql
[INFO] Parameter: artifactId, Value: sqldemo
[INFO] Parameter: packageName, Value: org.example.cosmosdb.sql
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /home/xxx/cosmosdbtest/sqldemo
[INFO] -------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] -------------------------------------------------------------------
[INFO] Total time:  3.524 s
[INFO] Finished at: 2022-11-10T18:50:33+09:00
[INFO] -------------------------------------------------------------------

2-3. Removing the existing App.java and AppTest.java

rm ./sqldemo/src/main/java/org/example/comsosdb/sql/App.java
rm ./sqldemo/src/test/java/org/example/cosmosdb/sql/AppTest.java

2-4. Starting Visual Studio Code

cd ./sqldemo
code .

2-5. Adding Cosmos DB library into pom.xml and save it (Ctrl+S)

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
    
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.azure</groupId>
      <artifactId>azure-cosmos</artifactId>
      <version>4.30.1</version>
    </dependency>
  </dependencies>

2-6. Coding for saving results of select query on Cosmos DB into a text file

cosmosdblocalout.java is created.

package org.example.cosmosdb.sql;

import com.azure.cosmos.*;
import com.azure.cosmos.models.CosmosQueryRequestOptions;
import com.azure.cosmos.util.CosmosPagedIterable;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.*;

public class cosmosdblocalout {
  // select data from Csomos DB and save to local file

  public static class Family {
    public String id;
    public String lastName;
    public boolean isRegistered;

    public String getId() {
      return id;
    }

    public String getLastName() {
      return lastName;
    }

    public Family(String idv, String lastNamev, boolean isv) {
      id = idv;
      lastName = lastNamev;
      isRegistered = isv;
    }

    public String toJson(String idv, String lastNamev, boolean isv) {
      return "{id:" + idv + ",lastName:" + lastNamev + ",isRegistered:" + isv + "}";
    }

    public Family() {
    }
  }

  public static void main(String[] args) {
    String databaseName = "testdb";
    String endpoint = "https://xxx.documents.azure.com:443";
    String masterkey = "xxx";
    String containerName = "coll";

    try (CosmosClient cosmosClient = new CosmosClientBuilder().endpoint(endpoint).key(masterkey)
        .gatewayMode(GatewayConnectionConfig.getDefaultConfig()).consistencyLevel(ConsistencyLevel.SESSION)
        .connectionSharingAcrossClientsEnabled(true).contentResponseOnWriteEnabled(true)
        .userAgentSuffix("Applicationtest").preferredRegions(Collections.singletonList("East US")).buildClient()) {
      CosmosDatabase database = cosmosClient.getDatabase(databaseName);
      CosmosContainer container = database.getContainer(containerName);

      // run select query
      String query = "SELECT * FROM c";
      CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
      options.setMaxDegreeOfParallelism(2);
      CosmosPagedIterable<Family> results = container.queryItems(query, options, Family.class);
      File file = new File("/home/xxx/cosmosdbtest/sqldemo/output.txt");
      try {
        PrintStream ps = new PrintStream(file);
        System.setOut(ps);
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      }
     
      for (Family result : results) {
        System.out.println(result.toJson(result.getId(), result.getLastName(), result.isRegistered));
      }
    } catch (CosmosException e) {
      e.printStackTrace();
      System.err.println(e.getMessage());
    }
}

2-7. Checking output of text file

cat output.txt

{id:1,lastName:Travase,isRegistered:false}
{id:2,lastName:Ken,isRegistered:true}
{id:3,lastName:Sai,isRegistered:false}


▼3. Reference

  1. Azure Cosmos DB Free Account https://docs.microsoft.com/ja-jp/azure/cosmos-db/optimize-dev-test#try-azure-cosmos-db-for-free
  2. https://code.visualstudio.com/docs/setup/linux
  3. Apache Kafka Word Count 実装 – Java No.44
  4. Java Azure Cosmos DB (SQL API) Database の作成方法 No.14

Have a nice day, ahead !!!

Leave a Reply

Your email address will not be published. Required fields are marked *