Blog

Using Kafka with GridDB’s Time Series Containers

In today’s article we will be discussing Kafka in conjunction with GridDB, which we have done before: Stream Data with GridDB and Kafka Using GridDB as a source for Kafka with JDBC Using SQL Batch Inserts with GridDB v5.5, JDBC, and Kafka Udemy course: Create a working IoT Project – Apache Kafka, Python, GridDB We will focus in this article on a new feature which allows for use of Kafka with GridDB as a sink resource which will make TIME_SERIES containers (meaning we can push time_series data from Kafka topics directly into GridDB with some configuration); prior to v5.6, we were limited to Collection Containers. There will be some similarities with the blog last written about using Kafka with GridDB titled: “Stream Data with GridDB and Kafka”. The differences here are that we have made all the moving parts of kafka and GridDB into Docker containers for easier portability and ease of use and will, as alluded to earlier, be using Time Series containers. If you follow along with this blog, you will learn how use Kafka to stream time series data directly into a GridDB time series container using Docker containers and Kafka. High Level Overview Before we get into how to run this project, let’s briefly go over what this project does and how it works. We will get Kafka and GridDB running inside of docker containers, and once those are ready, we will run a python script which acts as a kafka producer to push up random data to the broker. This simulated iot data will then sit in a Kafka queue (though it’s more accurately a distributed log) until a consumer is available to read those values. In our case, GridDB will act as the sink, meaning it will consume the data topics made by our python script and then save that data into tables which will created by Kafka based on our topics’ schemas set within our Python script. To properly communicate how and where to save the Kafka topics, we will need to set up a GridDB Kafka Sink properties file. But first, we will also need to grab and build the latest version (v5.6) of the GridDB Kafka Connect and somehow share that with our running Kafka installation so that we may save time series data directly into time series containers. Within that properties file, we will need to set the container type to time_series along with various other important details. Getting Started Let’s discuss how to run this project. Prerequisites To follow along with this blog, you will need docker and docker compose for running Kafka and GridDB. We will also need python3 installed to create data to be pushed into Kafka as topics (and then eventually saved into GridDB). We will also need to grab and build the GridDB Kafka Connect jar file. GridDB Kafka Connect (Optional) You can download the latest version here: griddb-kafka-connect. To build, make sure you have maven installed and run: $ mvn clean install The .jar file will be created inside of the target directory under the name: griddb-kafka-connector-0.6.jar. Note: The jar file is also included in the source code provided by this repo (in the next section). If you clone the repo and run this project via docker compose, you do not need to download/build the jar file yourself. Source Code You can find the source code in the griddbnet github page: $ git clone https://github.com/griddbnet/Blogs.git –branch 7_kafka_timeseries Running Project Once you have the source code and docker installed, you can simply run: $ docker compose pull && docker compose up -d And then once it’s done, you can start checking if the Kafka connector has the GridDB sink properties file in place by running the following: $ curl http://localhost:8083/connectors/ [“griddb-kafka-sink”] You can also take a look at the contents of the kafka-sink to see what it contains: $ curl http://localhost:8083/connectors/griddb-kafka-sink Once that’s done, you can run the python script, which acts as a kafka producer. $ python3 -m pip install kafka-python $ python3 scripts/producer.py GridDB Sink Properties In Kafka and other stream/event-driven architectures, the concept of sources and sinks mean to describe the direction of the flow of data. The sink is where data flows in, or where the data ends up — in this case, we want our data payloads to persist inside of GridDB as time series data inside of a time series container. And so we set the properties file as such: connector.class= com.github.griddb.kafka.connect.GriddbSinkConnector name= griddb-kafka-sink cluster.name= myCluster user= admin password= admin notification.member= griddb-server=10001 container.type= TIME_SERIES topics= meter_0,meter_1,meter_2,meter_3 transforms= TimestampConverter transforms.TimestampConverter.type= org.apache.kafka.connect.transforms.TimestampConverter$Value transforms.TimestampConverter.format= yyyy-MM-dd hh=mm=ss transforms.TimestampConverter.field= timestamp transforms.TimestampConverter.target.type= Timestamp As compared to our previous article, the main changes are the container.type designation and the transforms properties. The transforms properties tells our Kafka cluster which string value will be converted into timestamp, along with other useful information to help that process along. The other values are simply allowing for our broker to know where to send the data topics to, which is our GridDB docker container with a hostname of griddb-server. The topics are the name of the data topics and will also be the names of our GridDB time series containers. Python Producer Script There isn’t much to say here that you can’t get from simply reading the (simple) source code. The only thing I will add is that if you wished to docker-ize the docker container as well, you would change server location from localhost to broker:9092 #p=KafkaProducer(bootstrap_servers=[‘localhost:9092’]) p=KafkaProducer(bootstrap_servers=[‘broker:9092′]) One other thing to note is that though we are making time_series data containers with time_series data as the row key, you still need to set your payload data fields as type string (I teased this above when discussing the transform property in the sink section). “schema”: { “fields”: [ { “field”: “timestamp”, “optional”: False, “type”: “string” }, { “field”: “kwh”, “optional”: False, “type”: “double” }, { “field”: “temp”, “optional”: False, “type”: “double” } ], “name”: “iot”, “optional”: False, “type”: “struct” } The key here is that though the type is string, we must set the first field as our targeted timestamp type. And then in the sink for this dataset, we set the transforms.TimestampConverter.field as the name of our field we want to convert to type timestamp. With these things in place, Kafka and GridDB will create your tables with the set schema and the proper container type. Running Kafka in Docker Containers In our previous article about kafka, we simply ran kafka and GridDB on bare metal, meaning simply running the servers throught he CLI with commands. Though it worked well, it’s a bit confusing because you need 3-4 terminals open and need to remember to run things in sequence. For this article, we have prepared a docker compose file which allows you to run download and run everything with 2-3 commands! Confluent Docker Containers First, let’s discuss the docker images provided by Confluent, which is a company which provides support and tools pertaining to Kafka for your large corporation. Despite this though, they provide the docker images freely which we will use in our docker compose file. Essentially what docker compose does is allow us to create a set of “services” (AKA docker containers) which we can run in unison with a simple command, with rules set in which we can set which containers rely on others. For example, we can set the various kafka containers to rely on each other so that they start up in the correct sequence. We opted for this because as explained above, running Kafka is not an easy process — it has many different parts that need to run. For example, to run this seemingly simple project where we push data from python script –> kafka topics –> GridDb it takes 5 services in our Docker compose file. Docker Compose Services The following are all of the services. GridDB Kafka Zookeeper Kafka Broker Kafka Schema Registry Kafka-Connect And another service which we omited but we could include is a kafka data producer. The Kafka zookeeper can be thought of as the brains or the main component of kafka. The Broker is the service which handles the data topics and is often run with many different brokers for failsafes, etc; when we want to point our producer of data topics to Kafka, we point to the broker. The kafka schema registry enforces schemas to be used for your topics. In our case, it’s useful for deserialization of our JSON schema of our data payloads from our python producer. The Kafka Connect container is where we add our third party libraries for use with Kafka: GridDB Kafka connect jar and our GridDB sink properties file. The connect container is a bit unique in that we need to make sure that the container is up and running first and then we push to it a json file with the GridDB sink property instructions. The GridDB Kafka Connect jar file though we push to the file system during docker image start up. Docker Compose Instructions For GridDB there are no special instructions: we simply pull the image from griddbnet and then set some environment variables: griddb-server: image: ‘griddbnet/griddb:5.6.0’ container_name: griddb-server expose: – ‘10001’ – ‘10010’ – ‘10020’ – ‘10040’ – ‘20001’ – ‘41999’ environment: NOTIFICATION_MEMBER: 1 GRIDDB_CLUSTER_NAME: myCluster The zookeeper is in a similar boat: zookeeper: image: ‘confluentinc/cp-zookeeper:7.3.0’ container_name: zookeeper environment: ZOOKEEPER_CLIENT_PORT: 2181 ZOOKEEPER_TICK_TIME: 2000 The broker exposes port 9092 so that we can run our python producer script outside of the context of our docker compose network environment (we just point to localhost:9092). There are also more environment variables necessary for pointing to the zookeeper and other cluster rules broker: image: ‘confluentinc/cp-kafka:7.3.0’ container_name: broker ports: – ‘9092:9092’ depends_on: – zookeeper environment: KAFKA_BROKER_ID: 1 KAFKA_ZOOKEEPER_CONNECT: ‘zookeeper:2181’ KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: ‘PLAINTEXT:PLAINTEXT,PLAINTEXT_INTERNAL:PLAINTEXT’ KAFKA_ADVERTISED_LISTENERS: ‘PLAINTEXT://broker:9092,PLAINTEXT_INTERNAL://broker:29092’ KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1 KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1 You will also notice that the broker, schema registry, kafka connect all “depend” on the zookeeper. It really makes clear to us who is charge of the entire operation. kafka-schema-registry: image: ‘confluentinc/cp-schema-registry:7.3.0’ hostname: kafka-schema-registry container_name: kafka-schema-registry ports: – ‘8082:8082’ environment: SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: ‘PLAINTEXT://broker:9092’ SCHEMA_REGISTRY_HOST_NAME: kafka-schema-registry SCHEMA_REGISTRY_LISTENERS: ‘http://0.0.0.0:8082’ depends_on: – zookeeper The kafka connect also grabs from the confluent docker hub and has tons of environment variables, but it also includes volumes with a shared filesystem with the host machine so that we can share our GridDB Kafka Connect jar file. And lastly, we have scipt at the very bottom of the service which allows us to wait until our kafka-connect HTTP endpoints are available. Once we get a 200 status code as a response, we can run our script which sends our GridDB-Sink properties file. kafka-connect: image: confluentinc/cp-kafka-connect:latest container_name: kafka-connect ports: – ‘8083:8083’ environment: CONNECT_BOOTSTRAP_SERVERS: ‘broker:9092’ CONNECT_REST_PORT: 8083 CONNECT_GROUP_ID: device CONNECT_CONFIG_STORAGE_TOPIC: device-config CONNECT_OFFSET_STORAGE_TOPIC: device-offsets CONNECT_STATUS_STORAGE_TOPIC: device-status CONNECT_KEY_CONVERTER: org.apache.kafka.connect.json.JsonConverter CONNECT_VALUE_CONVERTER: org.apache.kafka.connect.json.JsonConverter CONNECT_INTERNAL_KEY_CONVERTER: org.apache.kafka.connect.json.JsonConverter CONNECT_INTERNAL_VALUE_CONVERTER: org.apache.kafka.connect.json.JsonConverter CONNECT_KEY_CONVERTER_SCHEMAS_ENABLE: true CONNECT_VALUE_CONVERTER_SCHEMAS_ENABLE: true CONNECT_KEY_CONVERTER_SCHEMA_REGISTRY_URL: ‘http://kafka-schema-registry:8082’ CONNECT_VALUE_CONVERTER_SCHEMA_REGISTRY_URL: ‘http://kafka-schema-registry:8082’ CONNECT_REST_ADVERTISED_HOST_NAME: kafka-connect CONNECT_LOG4J_APPENDER_STDOUT_LAYOUT_CONVERSIONPATTERN: ‘[%d] %p %X{connector.context}%m (%c:%L)%n’ CONNECT_CONFIG_STORAGE_REPLICATION_FACTOR: ‘1’ CONNECT_OFFSET_STORAGE_REPLICATION_FACTOR: ‘1’ CONNECT_STATUS_STORAGE_REPLICATION_FACTOR: ‘1’ CONNECT_PLUGIN_PATH: >- /usr/share/java,/etc/kafka-connect/jars CLASSPATH: >- /usr/share/java,/etc/kafka-connect/jars volumes: – ‘./scripts:/scripts’ – ‘./kafka-connect/connectors:/etc/kafka-connect/jars/’ depends_on: – zookeeper – broker – kafka-schema-registry – griddb-server command: – bash – ‘-c’ – > /etc/confluent/docker/run & echo “Waiting for Kafka Connect to start listening on kafka-connect ⏳” while [ $$(curl -s -o /dev/null -w %{http_code} http://kafka-connect:8083/connectors) -eq 000 ] ; do echo -e $$(date) ” Kafka Connect listener HTTP state: ” $$(curl -s -o /dev/null -w %{http_code} http://kafka-connect:8083/connectors) ” (waiting for 200)” sleep 5 done nc -vz kafka-connect 8083 echo -e “\n–\n+> Creating Kafka Connect GridDB sink” /scripts/create-griddb-sink.sh && /scripts/example-sink.sh sleep infinity This properties file will give explicit instructions to Kafka that when topics with certain names are received by the broker, it should push those out to the instructions in the properties file, which in this case are our GridDB container. Conclusion After you run the producer, you should be able to see all of your data inside of your docker griddb server through use of the GridDB CLI: $ docker exec -it griddb-server gs_sh. And with that, we have successfully pushed IoT-like sensor data through Kafka to a GridDB Time Series

More
GridDB with Salesforce Integration

Introduction In today’s competitive landscape, efficient customer support is essential for retaining customers and driving business success. A critical factor in achieving this is minimizing ticket resolution times. By analyzing and optimizing these times, organizations can identify bottlenecks, improve support workflows, and enhance overall customer satisfaction Salesforce, a widely used CRM platform, provides a foundation for managing customer interactions. However, to gain deeper insights into ticket resolution times and identify areas for improvement, organizations can leverage the power of GridDB. This high-performance time series database is designed to handle large volumes of time-sensitive data efficiently. This blog will walk you through creating a dashboard that integrates Salesforce CRM service ticket data with GridDB for time series analysis and visualization of average ticket resolution times. We’ll cover how to extract data using RESTful APIs with Spring Boot, store it in GridDB, query the data for valuable insights, and ultimately visualize the results. Setting Up GridDB Cluster and Spring Boot Integration: For Real-Time Monitoring To effectively perform time series analysis on customer support ticket resolution times, the first step is to set up a GridDB cluster and integrate it with your Spring Boot application. Setting up GridDB Cluster GridDB provides flexible options to meet different requirements. For development, a single-node cluster on your local machine may be sufficient. However, in production, distributed clusters across multiple machines are typically preferred for improved fault tolerance and scalability. For detailed guidance on setting up clusters based on your deployment strategy, refer to the GridDB documentation. To set up a GridDB cluster, follow these steps mentioned here. Setting up Spring Boot Application Once your GridDB cluster is operational, the next step is connecting it to your Spring Boot application. The GridDB Java Client API provides the necessary tools to establish this connection. To simplify the process, you can include the griddb-spring-boot-starter library as a dependency in your project, which offers pre-configured beans for a streamlined connection setup. Project Structure Here’s a suggested project structure for such an application: my-griddb-app │ ├── pom.xml │ ├── src │ │ ├── main │ │ │ ├── java │ │ │ │ └── mycode │ │ │ │ ├── config │ │ │ │ │ └── GridDBConfig.java │ │ │ │ ├── controller │ │ │ │ │ └── ChartController.java │ │ │ │ ├── dto │ │ │ │ │ └── ServiceTicket.java │ │ │ │ ├── MySpringBootApplication.java │ │ │ │ └── service │ │ │ │ ├── ChartService.java │ │ │ │ ├── MetricsCollectionService.java │ │ │ │ └── RestTemplateConfig.java │ │ │ └── resources │ │ │ ├── application.properties │ │ │ └── templates │ │ │ └── charts.html This structure delineates distinct layers for controllers, models, repositories, services, and the application entry point, promoting modularity and maintainability. Additionally, it encompasses resource files such as application properties and logging configurations, alongside testing suites for ensuring robustness. Add GridDB Dependency To enable interaction with GridDB in your Spring Boot project, you must include the GridDB Java Client API dependency. This can be accomplished by adding the appropriate configuration to your project’s build file, such as pom.xml for Maven or the equivalent file for Gradle. Here’s an example of how to configure the dependency in your pom.xml file: <project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>my-griddb-app</artifactId> <version>1.0-SNAPSHOT</version> <name>my-griddb-app</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.4</version> <relativePath /> <!– lookup parent from repository –> </parent> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!– GridDB dependencies –> <dependency> <groupId>com.github.griddb</groupId> <artifactId>gridstore-jdbc</artifactId> <version>5.3.0</version> </dependency> <dependency> <groupId>com.github.griddb</groupId> <artifactId>gridstore</artifactId> <version>5.5.0</version> </dependency> <!– Spring Boot dependencies –> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!– JSON processing –> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.0</version> <!– or the latest version –> </dependency> <!– Lombok –> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> </project> Configure GridDB Connection After adding the GridDB dependency, the next step is to configure the connection details for your GridDB cluster in your Spring Boot application. This is typically done in the application.properties file, which is where you define various settings for your app. Here’s a quick example of how to set up those connection details: GRIDDB_NOTIFICATION_MEMBER=127.0.0.1:10001 GRIDDB_CLUSTER_NAME=myCluster GRIDDB_USER=admin GRIDDB_PASSWORD=admin management.endpoints.web.exposure.include=* server.port=9090 griddb.cluster.host: The hostname or IP address of your GridDB cluster. griddb.cluster.port: The port number on which the GridDB cluster is listening. griddb.cluster.user: The username for accessing the GridDB cluster. griddb.cluster.password: The password for the specified GridDB user (replace with your actual password). server.port=9090: Sets the port on which your Spring Boot application will run. Create GridDB Client Bean To interact with GridDB effectively in your Spring Boot application, you’ll need a dedicated Spring Bean to manage the GridDB connection. This bean will initialize the connection using the parameters specified in your application.properties file and will serve as the central point for interacting with the GridDB cluster throughout your application. Here’s an example of how to define this bean in a Java class named GridDbConfig.java: package mycode.config; import java.util.Properties; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import com.toshiba.mwcloud.gs.GSException; import com.toshiba.mwcloud.gs.GridStore; import com.toshiba.mwcloud.gs.GridStoreFactory; @Configuration @PropertySource(“classpath:application.properties”) public class GridDBConfig { @Value(“${GRIDDB_NOTIFICATION_MEMBER}”) private String notificationMember; @Value(“${GRIDDB_CLUSTER_NAME}”) private String clusterName; @Value(“${GRIDDB_USER}”) private String user; @Value(“${GRIDDB_PASSWORD}”) private String password; @Bean public GridStore gridStore() throws GSException { // Acquiring a GridStore instance Properties properties = new Properties(); properties.setProperty(“notificationMember”, notificationMember); properties.setProperty(“clusterName”, clusterName); properties.setProperty(“user”, user); properties.setProperty(“password”, password); return GridStoreFactory.getInstance().getGridStore(properties); } } Metric Collection To visualize customer support ticket resolution times from Salesforce with GridDB, we first extract the necessary data using Salesforce’s REST API. Once the data is retrieved and stored in GridDB, we can utilize its query features to calculate and effectively visualize ticket resolution times. Here’s how to proceed with the data collection and loading process: Querying Salesforce Data Salesforce provides detailed information on customer support cases through its REST API, such as Id, CaseNumber, Subject, Status, CreatedDate, ClosedDate, and Priority. These fields are pivotal for monitoring ticket resolution times and evaluating your support team’s efficiency. To extract this data, we leverage Salesforce’s REST API, which allows us to perform queries using Salesforce Object Query Language (SOQL). The following steps outline the high-level process: Define the Query: Construct a SOQL query to select the relevant fields from the Case object. This query should target the specific data needed for performance analysis, such as case creation and closure dates. Authenticate and Send the Request: Utilize OAuth tokens to securely authenticate your application with Salesforce. Once authenticated, the query is sent to Salesforce’s API endpoint. Handle the Response: After receiving the API response, parse the returned JSON data to extract the required fields. This parsed data will include the necessary information to calculate ticket resolution times. Loading Data into GridDB Once we have retrieved the necessary data from Salesforce, the next step is to load it into GridDB. Here’s a high-level overview of this process: Data Transformation and Mapping: Convert Salesforce fields (such as CreatedDate, ClosedDate, Priority) to match the attributes in GridDB’s time series schema. This step ensures that the data is formatted correctly for optimal time series storage. We use the following DTO to define the GridDB schema. package mycode.dto; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; import com.toshiba.mwcloud.gs.RowKey; @Data @NoArgsConstructor @AllArgsConstructor public class ServiceTicket { @RowKey public Date createdDate; public String caseNumber; public Date closedDate; public String subject; public String status; public String priority; public double resolutionTime; } Insert Data into GridDB: Iterate over the transformed DTOs and insert each record into the corresponding GridDB container. Ensure that the data is inserted in a way that preserves its time series nature, with timestamps accurately reflecting the case lifecycle (e.g., CreatedDate and ClosedDate). The full implementation for this process is detailed in the following class. package mycode.service; import java.util.ArrayList; import java.util.Date; import java.util.Random; import java.text.ParseException; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneOffset; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.toshiba.mwcloud.gs.*; import mycode.dto.ServiceTicket; @Service public class MetricsCollectionService { @Autowired private GridStore store; @Autowired private RestTemplate restTemplate; @Scheduled(fixedRate = 60000) // Collect metrics every minute public void collectMetrics() throws GSException, JsonMappingException, JsonProcessingException, ParseException { String accessToken = getSalesforceAccessToken(); ArrayList salesforceData = fetchSalesforceData(accessToken); salesforceData.forEach(ticket -> { try { TimeSeries ts = store.putTimeSeries(“serviceTickets”, ServiceTicket.class); ts.put(salesforceData); } catch (GSException e) { e.printStackTrace(); } }); } public ArrayList fetchSalesforceData(String accessToken) throws JsonMappingException, JsonProcessingException, ParseException { String queryUrl = “https://.develop.my.salesforce.com/services/data/v57.0/query”; HttpHeaders headers = new HttpHeaders(); headers.setBearerAuth(accessToken); UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(queryUrl) .queryParam(“q”, “SELECT+Id,+CaseNumber,+Subject,+Status,+CreatedDate,+ClosedDate,+Priority+FROM+Case”); HttpEntity request = new HttpEntity(headers); ResponseEntity response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, request, String.class); if (response.getStatusCode() == HttpStatus.OK) { ObjectMapper objectMapper = new ObjectMapper(); JsonNode rootNode = objectMapper.readTree(response.getBody()); ArrayNode records = (ArrayNode) rootNode.path(“records”); System.out.println(response.getBody()); ArrayList serviceTickets = new ArrayList(); for (JsonNode record : records) { ServiceTicket ticket = new ServiceTicket(); String status = record.get(“Status”).asText(); ticket.setStatus(status); if (“Closed”.equals(status)) { ticket.setCaseNumber(record.get(“CaseNumber”).asText()); ticket.setCreatedDate(objectMapper.convertValue(record.get(“CreatedDate”),Date.class)); ticket.setClosedDate(objectMapper.convertValue(record.get(“ClosedDate”), Date.class)); ticket.setSubject(record.get(“Subject”).asText()); ticket.setPriority(record.get(“Priority”).asText()); ticket.setResolutionTime( calculateResolutionTimeInHours( objectMapper.convertValue(record.get(“CreatedDate”), Date.class), objectMapper.convertValue(record.get(“ClosedDate”), Date.class))); serviceTickets.add(ticket); } } return serviceTickets; } else { throw new RuntimeException(“Failed to fetch data from Salesforce”); } } public static double calculateResolutionTimeInHours(Date createdDate, Date closedDate) { long timeDifferenceMillis = closedDate.getTime() – createdDate.getTime(); return 1 + (100 – 1) * new Random().nextDouble(); } public String getSalesforceAccessToken() throws JsonMappingException, JsonProcessingException { String url = “https://login.salesforce.com/services/oauth2/token”; HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); MultiValueMap body = new LinkedMultiValueMap(); body.add(“grant_type”, “password”); body.add(“client_id”, “ENTER_CLIENT_ID”); body.add(“client_secret”, “ENTER_CLIENT_SECRET”); body.add(“password”, “ENTER_PASSOWRD”); body.add(“redirect_uri”, “ENTER_REDIRECT_URI”); body.add(“username”, “ENTER_USERNAME”); HttpEntity requestEntity = new HttpEntity(body, headers); try { ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class); if (response.getStatusCode() == HttpStatus.OK) { ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(response.getBody()); return jsonNode.get(“access_token”).asText(); } else { throw new RuntimeException(“Failed to retrieve the token”); } } catch (HttpClientErrorException e) { System.out.println(“HTTP Error: ” + e.getStatusCode()); System.out.println(“Response Body: ” + e.getResponseBodyAsString()); throw e; } } } By following above steps, we can effectively extract customer support case data from Salesforce, load it into GridDB. Data Querying in GridDB and Visualization with Thymeleaf Once the data is stored and available in GridDB, the next step is to visualize this data in a way that provides actionable insights. In this section, we’ll explore how to build a dashboard using Spring Boot, Thymeleaf, and Chart.js to render charts that display the average ticket resolution times and trends over time. Here are the steps to achieve this: Building the Chart Controller The ChartController acts as the intermediary between backend data in GridDB and the frontend visualizations displayed on the dashboard. Its responsibilities include handling HTTP requests, interacting with the service layer to fetch data, and passing that data to Thymeleaf templates for rendering. Here’s how the ChartController is implemented: package mycode.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import mycode.service.ChartService; import java.util.HashMap; import java.util.Map; @Controller public class ChartController { @Autowired ChartService chartService; @GetMapping(“/charts”) public String showCharts(Model model) { Map chartData = new HashMap(); try { Map projectionData = chartService.queryData(); chartData.put(“values”, projectionData.get(“time”)); chartData.put(“labels”, projectionData.get(“dates”)); } catch (Exception e) { e.printStackTrace(); } model.addAttribute(“chartData”, chartData); // Returning the name of the Thymeleaf template (without .html extension) return “charts”; } } Implementing the Chart Service The ChartService acts as the business logic layer, encapsulating the operations needed to query GridDB and process the results. This service provides methods to retrieve various metrics, such as the average ticket resolution time or the distribution of tickets by priority. Here’s how the ChartService is implemented: package mycode.service; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.toshiba.mwcloud.gs.Container; import com.toshiba.mwcloud.gs.GridStore; import com.toshiba.mwcloud.gs.Query; import com.toshiba.mwcloud.gs.Row; import com.toshiba.mwcloud.gs.RowSet; @Service public class ChartService { @Autowired GridStore store; public Map queryData() throws Exception {

More
Color Palettes Extraction Using Webcam and AI

In this tutorial, we will explore how to extract color palettes from images captured via a webcam using Node.js, GridDB, and OpenAI. By leveraging Node.js for server-side scripting, GridDB for efficient data storage, and OpenAI for advanced image processing, we will create a seamless pipeline to capture images, analyze them, and generate dynamic color palettes. This guide will walk you through setting up your environment, capturing images from your webcam, and using AI to extract and store color data effectively. Prerequisites Before we dive in, ensure the following software is installed on your machine: Node.js GridDB OpenAI API access Browser with a webcam access Running The Project Clone the source code from this GitHub repository. git clone https://github.com/griddbnet/Blogs.git –branch color-extraction This project also needs to install Node.js and GridDB for this project to run. If the software requirements are installed, change the directory to the apps project directory and then install all the dependencies: cd color-detection-openai cd apps npm install Create a .env file and copy all environment variables from the .env.example file. We need an OpenAI key for this project, please look in the “Getting Started” section to get started. OPENAI_API_KEY=sk-proj-secret VITE_APP_URL=http://localhost:3000 You can change the VITE_APP_URL to your needs and then run the project by running this command: npm run start:build Go to the browser and enter the URL set on VITE_APP_URL, which in this case is http://localhost:3000. Make sure to enable the webcam in your browser, then click the Capture button to take a photo using the web camera. Setting Up the Environment 1. Installing Node.js This project will run on the Node.js platform. You need to install it from here. For this project, we will use the nvm package manager and Node.js v16.20.2 LTS version. # installs nvm (Node Version Manager) curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash # download and install Node.js nvm install 16 # verifies the right Node.js version is in the environment node -v # should print `v16.20.2` # verifies the right NPM version is in the environment npm -v # should print `8.19.4“ To connect Node.js and GridDB database, we need the gridb-node-api npm package which is a Node.js binding developed using GridDB C Client and Node addon API. 2. Setting Up GridDB We will use the GridDB database to save recipes and it’s nutrition analysis. Please look at the guide for detailed installation. We will use Ubuntu 20.04 LTS here. Run GridDB and check if the service is running. Use this command: sudo systemctl status gridstore If not running try to run the database with this command: sudo systemctl start gridstore 3. Get The OpenAI Key To get the OpenAI key, create a project first and then create a key. The important thing is you should save the OpenAI key on the .env file and ensure not to include it in version control by adding it to the .gitignore. OPENAI_API_KEY=sk-proj-secret Another crucial factor is to select models that are accessible for the project. For this project, we will utilize gpt-4o models for image recognition and extracting colors from the image. The AI model’s response is non-deterministic, which means sometimes the response is not exactly what we want. By default this project uses the gpt-4o-mini model, in case the response is not quite right, you can change it to a more powerful model, such as the gpt-4o model. Capturing Images with MediaStream To capture images, we can use MediaStream API. It is an API related to WebRTC which provides support for streaming audio and video data. Before capturing an image from the web camera, we first need to initialize the web camera: const initializeWebcam = () => { navigator.mediaDevices.getUserMedia({ video: true }) .then(stream => { videoRef.current.srcObject = stream }) .catch(error => { console.error(‘getUserMedia error:’, error) }) } And then to capture the image from the video, we can use the drawImage() function: const captureImage = () => { const context = canvasRef.current.getContext(‘2d’) context.drawImage(videoRef.current, 0, 0, canvasRef.current.width, canvasRef.current.height) const base64Image = canvasRef.current.toDataURL(‘image/jpeg’) processImage(base64Image) } The drawImage() function will capture the current frame from the video stream and render it onto the canvas. This allows for further image data manipulation, processing, or conversion. In the provided code, the drawn image on the canvas is converted to a base64-encoded string using the toDataURL() function, which is then sent to a server for processing. Processing Images with OpenAI The image processing on the server is quite simple. The web app will send a base64-encoded image to the /process-image route. app.post(‘/process-image’, async (req, res) => { const { image } = req.body if (!image) { return res.status(400).json({ error: ‘No image provided’ }) } // eslint-disable-next-line no-undef const result = await getColorAnalysis(image) res.json(result.choices[0]) }) Then to get the color analysis from the image, we will use the gpt-4o-mini model from OpenAI. The getColorAnalysis() function will take the base64-encoded image and then process it. async function getColorAnalysis(base64Image) { const response = await openai.chat.completions.create({ model: “gpt-4o-mini-2024-07-18”, messages: [{ role: “system”, content: systemPrompt }, { role: “user”, content: [{ type: “image_url”, image_url: { url: base64Image } }, { type: “text”, text: userPrompt } ] } ], temperature: 0.51, max_tokens: 3000, top_p: 1, frequency_penalty: 0, presence_penalty: 0, }); return response; } OpenAI’s model response is determined by the prompt given. For a color analysis, use the specific prompt: const userPrompt = “Extract the seven most prominent colors from the provided image. Use color clustering techniques to identify and present these colors in Hex values. Answer with the raw array values ONLY. DO NOT FORMAT IT.”; We can get a better result by adding a system prompt to the OpenAI model. This system prompt behaves like a command for the OpenAI model to behave for a specific persona, which is a professional color analyst. const systemPrompt = `You are an AI specialized in colorimetry, the science and technology of color detection and measurement. You possess deep knowledge of the principles of color science, including color spaces, color matching functions, and the use of devices such as spectrophotometers and colorimeters. You provide accurate and detailed analyses of color properties, offer solutions for color consistency issues, and assist in applications ranging from imaging and printing to manufacturing and display technologies. Use your expertise to answer questions, solve problems, and provide color detection and measurement guidance.`; The prompt can also specify the model format response. In this project, we want the array of colors from the image colors analysis. The OpenAI model response should be in the form: [‘#2A2C9B’, ‘#F08A7D’, ‘#8E5DB2’, ‘#E8A1A3’, ‘#4D3B9E’, ‘#7F3C8F’, ‘#B57AB3’] Where each item in the array is a color in the hex format. Storing Data in GridDB We utilize the GridDB database for data storage. Here are the main data fields along with their descriptions: Column Name Type Description id INTEGER Unique identifier for each row. picture BLOB Base64 image encoding. colors STRING List of colors in Hex format. The saveData() function is a wrapper for the insert() function in the libs\griddb.cjs file. It is responsible for saving data into the database. Only two main fields are saved in the database. export async function saveData({ image, genColors }) { const id = generateRandomID() const picture = Buffer(image) const colors = String(genColors) const packetInfo = [parseInt(id), picture, colors] const saveStatus = await GridDB.insert(packetInfo, collectionDb) return saveStatus } The save data function will be executed on the server route /process-image after the color analysis of the image. Every time a user captures an image, it will be automatically sent to the server and the resulting data will be saved to the database. app.post(‘/process-image’, async (req, res) => { const { image } = req.body if (!image) { return res.status(400).json({ error: ‘No image provided’ }) } // eslint-disable-next-line no-undef const result = await getColorAnalysis(image) const colorsArray = result.choices[0].message.content // save data to the database const saveStatus = await saveData(image, colorsArray) console.log(saveStatus) res.json(result.choices[0]) }) Building User Interfaces The UI comprises two primary user interfaces: image capture and color palettes. React.js is utilized in this project to improve component management. Image Capture The image capture user interface is simply an HTML5 video view. This is the snippet code that shows the main HTML tags used: // WebcamContainer.js const WebcamContainer = ({ onColorsExtracted }) => { const captureImage = () => { const context = canvasRef.current.getContext(‘2d’) context.drawImage(videoRef.current, 0, 0, canvasRef.current.width, canvasRef.current.height) const base64Image = canvasRef.current.toDataURL(‘image/jpeg’) processImage(base64Image) } // code processing here return ( Capture Switch Camera ) } export default WebcamContainer When you click the Capture button the captureImage() function will capture the image on a specific video frame and send it for further processing. The full source code for the image capture user interface is in the WebcamContainer.jsx file. Color Palettes The color palette UI can be created using a series of dynamically colored svg rectangles. // eslint-disable-next-line react/prop-types const ColorRectangles = ({ colors }) => { return ( {colors.map((color, index) => ( ))} ) } export default ColorRectangles For example, if the colors array data is: [‘#4B8B3B’, ‘#C4B600’, ‘#7D7D7D’, ‘#E3D4A0’, ‘#2E2E2E’, ‘#F6F1D3’, ‘#A6A6A6’] Then the colors will be rendered on the web as the screenshot below: Server Routes The are four server routes to handle the client request. POST /process-image Process an image for color analysis. GET /colors The /colors route will retrieve all data from the database. GET /colors/:id Retrieve stored color data based on the ID. The data response for the picture field is a Buffer type so to process it in the browser, we need to change it into a readable format first. /** * Extracting the buffer data * Assume the result data name is jsonData */ const bufferData = jsonData[0][1].data; // Converting buffer data to Uint8Array object const uint8Array = new Uint8Array(bufferData); // Converting Uint8Array to UTF-8 string const utf8String = new TextDecoder(‘utf-8’).decode(uint8Array); console.log(utf8String); GET /delete/:id Delete specific data in the database by its ID. For example, to delete data with id 8900: http://localhost:3000/delete/8900 Tools like Postman can be used to test APIs. SQL Data Test To check the data in the database, we can use CLI commands. In this project, we use Ubuntu 20.04 LTS. Login to the GridDB user: sudo su gsadm and then type this command to enter the GridDB shell: gs_sh In this shell, we can list all containers and run any SQL queries. gs[public]> showcontainer gs[public]> select * from ColorPalettes; 1 results (38ms) gs[public]> delete from ColorPalettes where

More
Pairing GridDB Cloud with Grafana Cloud

With the release of a completely free GridDB Cloud, we wanted to pair its free service with Grafana Cloud, another free Cloud-based service which can get you up and running in seconds. For this article, we will walk through the steps of how to display time-series data from your GridDB Cloud shared instance to Grafana Cloud. If you are unfamiliar with GridDB Cloud, you can read our quick start guide here: GridDB Cloud Quick Start Guide — that article will teach you how to sign up, how to begin using GridDB Cloud and more of the basics: who, what, when, where, why. If you are also unfamiliar with Grafana, you can read about its capabilities and strength from their docs: https://grafana.com/docs/grafana/latest/introduction/. If you are curious as to how Grafana can enhance your GridDB experience, I will point you to a previous article we have written here: (Creating Heatmaps of Geometry Data using Grafana & GridDB)[https://griddb.net/en/blog/creating-heatmaps-grafana/]. Essentially, Grafana’s advanced visualization tools allow for some creative ways of analyzing and peering into your data. This article’s goal is simple: showcase how to use the cloud offerings from both vendors to display some GridDB Data in Grafana Cloud. We will go through this process step-by-step and explain any idiosyncrasies along the way. Let’s get started! Implementation First, here’s a link to the Grafana dashboard that we will be using for this article: https://imru.grafana.net/public-dashboards/8a9f9f8ed9d34582aecca867a50c9613. Source code can be found here: $ git clone https://github.com/griddbnet/Blogs.git –branch 4_grafana_cloud Prereqs To follow along, you will need to have access to a free account of both GridDB Cloud and Grafana Cloud. Technical Overview To query our GridDB Cloud data from Grafana, we will be sending HTTP Requests directly from Grafana to our GridDB Cloud. And indeed, any sort of interactions we want to make with our free GridDB Cloud instance will be done via Web API interface; this topic is covered in the quick start linked above, as well as in this article: GridDB WebAPI. You can also of course check out the official docs: GridDB_Web_API_Reference. The specifics of how to form our query and how to create our allowlist to get around GridDB’s firewall will be the subject of our next few sections. Adding Grafana’s IP Addresses to GridDB’s Allowlist In order for our Grafana Cloud instance to send HTTP Requests which are accepted as “safe” by our GridDB Cloud, we need to be able to add all potential IP Addresses to our GridDB Cloud instance. Browsing through the Grafana documentation, we see that they have these lists readily available for these exact scenarios: https://grafana.com/docs/grafana-cloud/account-management/allow-list/. The list we need is called ‘Hosted Grafana’, and if you take a quick peek, you’ll see the list is easily over 100 lines, so then how do we efficiently add all of these to our GridDB Cloud management portal? Well, luckily we have already encountered this scenario in our previous article: Create a Serverless IoT Hub using GridDB Cloud and Microsoft Azure. To solve the issue, we wrote a simple bash script which will take the .txt file as the input and add each ip address to the allowlist of GridDB Cloud’s online portal. Source code and instructions found in that blog in the “#whitelist” section. Here’s the script: #!/bin/bash file=$1 #EXAMPLE #runCurl() { #curl ‘https://cloud57.griddb.com/mfcloud57/dbaas/web-api/contracts/m01wc1a/access-list’ -X POST -H ‘User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:133.0) Gecko/20100101 Firefox/133.0’ -H ‘Accept: application/json, text/plain, */*’ -H ‘Accept-Language: en-US,en;q=0.5’ -H ‘Accept-Encoding: gzip, deflate, br, zstd’ -H ‘Content-Type: application/json;charset=utf-8’ -H ‘Access-Control-Allow-Origin: *’ -H ‘Authorization: Bearer eyJ0eXAiOiJBY2Nlc3MiLCJIUzI1NiJ9.eyJzdWIiOiJkMTg4NjlhZC1mYjUxLTQwMWMtOWQ0Yy03YzI3MGNkZTBmZDkiLCJleHAiOjE3MzEwMTEyMTMsInJvbGUiOiJBZG1pbiIsInN5c3RlbVR5cGUiOjF9.B1MsV9-Nu8m8mJbsp6dKABjJDBjQDdc9aRLffTlTcVM’ -H ‘Origin: https://cloud5197.griddb.com’ -H ‘Connection: keep-alive’ -H ‘Referer: https://cloud5197.griddb.com/mfcloud5197/portal/’ -H ‘Sec-Fetch-Dest: empty’ -H ‘Sec-Fetch-Mode: cors’ -H ‘Sec-Fetch-Site: same-origin’ -H ‘Priority: u=0’ -H ‘Pragma: no-cache’ -H ‘Cache-Control: no-cache’ –data-raw $1 #} runCurl() { $1 } while IFS= read -r line; do for ip in ${line//,/ }; do echo “Whitelisting IP Address: $ip” runCurl $ip done done < "$file" Querying GridDB Cloud from Grafana Cloud Out of the box, Grafana does have a method of sending HTTP Requests to your services, but from what I could tell, they're geared towards specific services (ie. Prometheus) and are limited to HTTP GET Requests. In our case, all of our requests to GridDB Cloud require POST requests, so we needed to find a solution for this: enter the Infinity plugin. Once installed, we will be able to add it as a datasource. Using Infinity as a Data Source From within your Grafana Cloud instance, select Connections --> Data Sources. If you installed the Infinity data source properly, it should show up in this section as an option — please select it. From here, we can add all of our pertinent GridDB Cloud information to forge our connection — we will need to add our basic authentication, and allow our portal’s hostname in the allowed hosts list. Once added, lastly click on the health check section and add your Web API + “/checkConnection” (ie. https://cloud5197.griddb.com:443/griddb/v2/gs_clustermfcloud5197/dbs/ZV8YUlQ8/checkConnection) as a simple sanity check AND health check. Hit Save & Test. We should be able to query our GridDB Cloud Database now! Ingesting Usable Time Series Data Before we query our data, let’s first ensure that we have working data in our GridDB Cloud. If you are following along and have just now made a new account, you can follow our quick start guide to ingest an IoT sample data that can be found on Kaggle. Here is a direct link to the section in the guide: https://griddb.net/en/blog/griddb-cloud-quick-start-guide/#ingest. Here, we are ingesting a csv file and calling the container device1. Forming Our HTTP Requests Now that we can communicate between services, let’s get the data we want. From the Grafana Cloud menu, select Dashboards and then select “new” in the top right corner and then finally Add visualization. From here, select Infinity and you will now have a blank graph and a place to put your query. And now for some options: Type: JSON Parser: Backend Source: URL Format: Data Frame Method: POST Note: Here is a screenshot of the entire query we will form in the ensuing lines of text (screenshot will be displayed again at the end once you can better understand what all of the words mean) The parser and format being what they are allows for us to properly name and label the data being received from GridDB Cloud because of the unusual way in which responds to the requestor with data. Instead of sending back the rows of data in JSON format (which, to be fair, if you’ve got a thousand rows, it’s a lot of unnecessary bloat), GridDB Cloud sends back the information as a JSON file, but the actual rows of data are in array form, with the schema being listed under another JSON key name (columns). As for the URL, you can take a look at the links above about how to form your Web API Request, but here are two we will be using: SQL: https://cloud5197.griddb.com/griddb/v2/gs_clustermfcloud5197/dbs/ZV8YUlQ8/sql/dml/query API: https://cloud5197.griddb.com:443/griddb/v2/gs_clustermfcloud5197/dbs/ZV8YUlQ8/containers/device1/rows In each case, we will have different selectors for our returned data, as well as different body payloads that we will be sending off as a request. First, let’s take a look at the SQL Query. Making a Simple SQL-Select Query First, set your URL to match the query above. The format is as follows: https://[cloud-portal-name].griddb.com/griddb/[clustername]/dbs/[database-name]/sql/dml/query. And then we form our SQL Query within the body of the request. To properly parse this, we set the format as data frame, and then under parsing options & Results fields, we type in “results” in the JSONata/rows selector box and then click add columns. To start, we simply want a couple of columns from our dataset, so we can se the columns like so: Selector: 0 #array index as: ts format-as: Time And then we select the column we want to track in our graph, let’s take a look at temperature Selector: 7 as: temperature format-as: Number Lastly, because it’s a POST request, we must send off something within the body of our request, but in this case, it’s going to be a SQL query. Right under the Method dropdown menu, there’s a button that says Headers, Body, Request Params. Click this. Fro that sub menu, set the Body Type to Raw and set the Body Content Type to JSON. And in the large text box you can add your actual body — or in our case, our SQL query: [ {“stmt” : “select * from device1 LIMIT 1000”} ] Note: I highly recommend using a limit on your query, otherwise Grafana may malfunction trying to show all of your data points. In the graph above, click “zoom in on data” if necessary and your data will be displayed! Cool! More SQL Queries (Group By Range, Aggregations) With the advent of GridDB 5.7, you can make some more complex SQL Queries as well and are not limited to SELECT statements. For example, we can use the SQL Group By Range which allows for us to perform a aggregation operations over a given time span. For example, this is what our query looks like: select ts,temp from device1 WHERE ts BETWEEN TIMESTAMP(‘2020-07-12T01:00:25.984Z’) AND TIMESTAMP(‘2020-07-12T01:22:29.050Z’) GROUP BY RANGE (ts) EVERY (1, SECOND) FILL (LINEAR). So we can simply plug this in to our Infinity plugin and see the results (Hint: you may need to change your column selectors and refresh the graph). You can also do other SQL aggregation queries, really there is no limit; you can read more about that in the GridDB SQL docs: https://griddb.org/docs-en/manuals/GridDB_SQL_Reference.html Using the Group By Range feature is excellent for creating dense graphs even if you don’t have a dense enough dataset! API Query We can also skip SQL and just the API to make queries using simple JSON options in our body request. The URL will be built out as follows: https://[cloud-portal-name].griddb.com/griddb/[clustername]/dbs/[database-name]/containers/[container-name]/rows So enter in your URL and, set the Body Content Type to JSON, and the Body content to some combination of the following: { “offset” : 0, “limit” : 100, “condition” : “temp >= 30”, “sort” : “temp desc” } As you can see, you set the condition and sort options for your dataset. The column options remain the same, except the results of the data is now called ‘rows’, so change that option in the Parsing Options & Result Fields section; you can keep the same column selectors as those stay the same. Conclusion Once you are done querying and adding in the data you like, you can of course save your dashboard for future use. And that’s all! If you have followed along, you have now been able to pair GridDB Cloud with Grafana Cloud to display your

More
GridDB v5.7 Web API Changes

GridDB v5.7.0 has released and we would like to go over some of the new features. You can download the new release directly from GitHub and from the Downloads page. In this article, we specifically want to take a closer look at two of the new features: WebAPI changes and SQL Working Memory limit. GridDB Web API Changes in v5.7.0 The GridDB WebAPI allows for you to interact with your GridDB server via HTTP Requests. The major changes in this release are expanded capability of the SQL functionality, as well as a new way to install the package itself. Packaged within this new release are SQL commands which were previously unavailable, including DDL, DML, and DCL commands. On the github page, you will also find .deb and .rpm files for installation, which will also add installing the web api as a service. The following sections’ information can be found on the project’s github page: https://github.com/griddb/webapi/blob/master/GridDB_Web_API_Reference.md Installation Changes As explained above, you can now install the GridDB Web API as a package, meaning it will auto-populate all necessary directories for you and also create a symlink so that you can launch the service using systemctl, similar to how the GridDB server operates. To do so, grab the .deb file from https://github.com/griddb/webapi/releases/tag/5.7.0 and run: $ sudo dpkg -i https://github.com/griddb/webapi/releases/download/5.7.0/griddb-ce-webapi_5.7.0_amd64.deb Once installed, you can make the changes you deem necessary in the following directory: /var/lib/gridstore/webapi/conf. And then to start: $ sudo systemctl start griddb-webapi.service SQL DDL (Data Definition Language) To me, the standout feature for the Web API is the inclusion of SQL DDL Commands. With this, we can now create tables using the Web API with the familar SQL syntax and are no longer required to use the TQL API for creating our tables. And though the previous methods worked just fine before, the benefits of this addition are two-fold: 1, SQL is a widely-known query language and therefore easier to work with, and 2, TQL commands did not allow for manipulating or creating partitioned tables/containers. Now let’s take a look at creating a table using the new SQL DDL Command. The URL path is as follows: /:cluster/dbs/:database/sql/ddl. Here’s a working example: curl –location ‘http://192.168.50.206:8082/griddb/v2/myCluster/dbs/public/sql/ddl’ \ –header ‘Content-Type: application/json’ \ –header ‘Authorization: ••••••’ \ –data ‘[ {“stmt” : “CREATE TABLE IF NOT EXISTS pyIntPart2 (date TIMESTAMP NOT NULL PRIMARY KEY, value STRING) WITH (expiration_type=’\”PARTITION’\”,expiration_time=10,expiration_time_unit=’\”DAY’\”) PARTITION BY RANGE (date) EVERY (5, DAY);”}, {“stmt” : “ALTER TABLE pyIntPart2 ADD temp STRING”} ]’ Here we are making two statements, one to create a partitioned table with expiry rules (you can read about that here: https://griddb.net/en/blog/griddb-partitioning-and-expiry/, and another to alter that same table and add a new column. Again, this was not possible before because of the nature of partitioned tabled. SQL DML (Data Manipulation Language) With DML commands, we can run SELECT, INSERT, UPDATE, etc commands on our containers. This functionality was partly there prior to the v5.7.0 release, though it was a lot more limited and is no longer recommended to be used. To conduct a DML request, the path is similar to the one above: /:cluster/dbs/:database/sql/dml/query. With this, you can run a SELECT statement to run a lookup of some data from your container. For example curl –location ‘http://192.168.50.206:8082/griddb/v2/myCluster/dbs/public/sql/dml/query’ \ –header ‘Content-Type: application/json’ \ –header ‘Authorization: ••••••’ \ –data ‘[ {“stmt” : “select AVG(total_points_per_game) from Top_NBA_Playoff_Scorers”} ] ‘ Again, if we wanted to query data from a partitioned table using the TQL method, the command will fail: curl –location ‘http://192.168.50.206:8082/griddb/v2/myCluster/dbs/public/tql’ \ –header ‘Content-Type: application/json’ \ –header ‘Authorization: ••••••’ \ –data ‘[ {“name” : “pyIntPart2”, “stmt” : “select *”, “columns” : null} ]’ DML Update We can also update rows by using the same rowkey or we can add new ones by using a fresh key. For example: curl –location ‘http://192.168.50.206:8082/griddb/v2/myCluster/dbs/public/sql/dml/update’ \ –header ‘Content-Type: application/json’ \ –header ‘Authorization: ••••••’ \ –data ‘[ {“stmt” : “INSERT INTO pyIntPart2(date, value, temp) VALUES (NOW(), ‘\”blog_test’\”, ‘\”cold’\”)”} ] ‘ Again, the nice thing about using SQL here instead of the old TQL is that we can use the special GridDB Time Series SQL commands such as NOW(). You can read more about those commands in the docs: https://docs.griddb.net/sqlreference/sql-commands-supported/#time-functions. If we tried to use the NOW() command with the TQL version of an insert, it fails: curl –location –request PUT ‘http://192.168.50.206:8082/griddb/v2/myCluster/dbs/public/containers/pyIntPart2/rows’ \ –header ‘Content-Type: application/json’ \ –header ‘Authorization: ••••••’ \ –data ‘[ [NOW(), “failure”,”hot”] ]’ This command will fail. SQL DCL (Data Control Language) The SQL Data Control Language is mostly concerned with rights/permissions for your database. This one isn’t as exciting but it can be useful if managing many databases or users. For eexample, if you create a new database and a new user, you can grant that user access to that DB with the Web API. curl –location ‘http://192.168.50.206:8082/griddb/v2/myCluster/dbs/public/sql/dcl’ \ –header ‘Content-Type: application/json’ \ –header ‘Authorization: ••••••’ \ –data ‘[ {“stmt” : “REVOKE all on testing1 from israel”}, {“stmt” : “GRANT all on testing1 to israel”} ]’ And you can verify this by using the GridDB CLI tool gs[public]> showuser israel Name : israel Type : General User GrantedDB: public testing1

More
Monitoring Air Quality in California using Home Assistant, Raspberry Pi, and GridDB Cloud

Despite humanity’s (lackluster) efforts, climate change has become an omnipresent, undeniable force. Though there are many side effects that come with a rising global average temperature, today I want to focus on wildfires and their affect on the air quality of all surrounding areas; when a 100-acre-fire is burning up a Californian forest, all of that debris and particle matter get kicked up into the atmosphere, becoming potentially dangerous, inhalable matter. Of course, there are various other factors which can (and do) contribute to the quality of the air we breathe. To me, this means that even if there isn’t a wildfire nearby causing spikes in AQI (Air Quality Index), there is still a reason to be informed and aware of what the air quality is like at any given moment. The Project For this article, we were interested in measuring the air quality inside of our homes. Specifically, we wanted to take live readings of our air quality data, save it into persistent storage, and then notify the inhabitants when the air quality grew passed a certain threshold. To accomplish our goal, we sought to integrate GridDB Cloud with the open source smart home solution known as Home Assistant. If you are unfamiliar, Home Assistant is an “Internet of things (IoT) ecosystem-independent integration platform and central control system for smart home devices, with a focus on local control”. Typically, end users install the software onto their home servers to control aspects of their internet-connected physical devices. As an example, one might install Home Assistant to act as their smart hub to control their smart light bulbs, smart robovac, etc. The beauty of Home Assistant is in its versatility and flexibility. For instance, because it’s completely open source and built for tinkerers/developers, users can roll their own solutions for their own specific use cases, and build their own automations. For our case, we were interested in being able to save all of the raw, unfiltered sensor data into GridDB Cloud at a resolution of 1 reading/second, and then using the Home Assistant to query the values needed to do what we want, eventually being able to notify those who live in the same space as the sensor that the air quality is approaching dangerous levels in some tangible way. Project Specifics and the PM1 Particle The idea goes like this, we use a single board computer to connect to a sensor which will capture raw air quality data. We then send that raw data into GridDB cloud at a resolution of 1 particle/second. Our Home Assistant can then query the data and downsample whatever information is necessary to accomplish our goal. To begin, we have connected an air quality sensor — Adafruit PMSA003I Air Quality Breakout — to a raspberry pi. We then use a python script to read and send the sensor readings up to GridDB Cloud every 1 second via HTTP Request. With our data being saved into persistent storage with GridDB Cloud, we can make HTTP Requests to query our dataset with SQL Select statements. In this case, we want to use Home Assistant to query our dataset to alert us of higher than normal particle matters in the air at our locations. We also want to include an easy to read sensor reading right on our Home Assistant dashboard with rolling averages. As a sidenote: an interesting point about this particular sensor is that it can read matter as small as 1 micron (labeled as PM1). These particles are so small that they can penetrate lung tissue and get directly into your bloodstream; and because this particle is so tiny, it’s only detectable by specialized equipment, including the sensor linked above. Because of the lack of readily available sources of PM1 readings, this particle will be the focus of our queries for notifying the home inhabitants of its increasing levels. Project Requirements If you would like to follow along, you will need the following: Free GridDB Cloud Account a. You can read instructions on how to sign up for a free GridDB cloud account from here: GridDB Free Plan. Accompanying blog: GridDB Cloud Quick Start Air Quality sensor Means of connecting the sensor to a computer (single board or otherwise) Once you have set up the necessary hardware and can connect to your GridDB Cloud database, you can grab the source code from here: GitHub to run the simple python script to send sensor readings. Connecting the Hardware In my case, I bought the air quality sensor attached with a STEMMA Connector; to connect it to the Raspberry Pi 4, I bought a STEMMA Hat and a STEMMA wire. If you do not want to purchase a STEMMA hat, you can also solder the pins onto the sensor and use a breadboard to connect to the Raspberry Pi’s GPIO pins. If you go this route, you may need to alter the python script provided by our source code — you can read more about how to physically connect this air quality sensor through their documentation page: Docs. Software Now let’s focus on the software that makes this project go. Python Script for Container Creation and Pushing Data First and foremost, let’s discuss the script which sends the sensor readings as HTTP requests. It is a modified version of the example script provided directly by ada fruit’s documentation. The data structure of the incoming data readings are already laid out in convenient dictionary matter, so we simply need to iterate through and make the values match up with how we lay out our schema on container creation. So first, here’s the script for container creation: import http.client import json conn = http.client.HTTPSConnection(“cloud5197.griddb.com”) payload = json.dumps({ “container_name”: “aqdata”, “container_type”: “TIME_SERIES”, “rowkey”: True, “columns”: [ { “name”: “ts”, “type”: “TIMESTAMP” }, { “name”: “pm1”, “type”: “DOUBLE” }, { “name”: “pm25”, “type”: “DOUBLE” }, { “name”: “pm10”, “type”: “DOUBLE” }, { “name”: “pm1e”, “type”: “DOUBLE” }, { “name”: “pm25e”, “type”: “DOUBLE” }, { “name”: “pm10e”, “type”: “DOUBLE” }, { “name”: “particles03”, “type”: “DOUBLE” }, { “name”: “particles05”, “type”: “DOUBLE” }, { “name”: “particles10”, “type”: “DOUBLE” }, { “name”: “particles25”, “type”: “DOUBLE” }, { “name”: “particles50”, “type”: “DOUBLE” }, { “name”: “particles100”, “type”: “DOUBLE” } ] }) headers = { ‘Content-Type’: ‘application/json’, ‘Authorization’: ‘Basic <redacted>’ } conn.request(“POST”, “/griddb/v2/gs_clustermfcloud5197/dbs/B2xcGQJy/containers”, payload, headers) res = conn.getresponse() data = res.read() print(data.decode(“utf-8”))</redacted> For the schema, we simply just used a 1:1 mapping to the aqdata dictionary returned by our sensor readings. Even if we don’t intend to use all of these data points, the data readouts are so small, we keep them in. Next, let’s take a look at the script that will push sensor readings to our GridDB Cloud instance. It will read the sensor data every 1 second, and then make an HTTP Request every second to push that data into the Cloud. # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT “”” Example sketch to connect to PM2.5 sensor with either I2C or UART. “”” # pylint: disable=unused-import import time import datetime import board import busio from digitalio import DigitalInOut, Direction, Pull from adafruit_pm25.i2c import PM25_I2C import http.client import json conn = http.client.HTTPSConnection(“cloud5197.griddb.com”) headers = { ‘Content-Type’: ‘application/json’, ‘Authorization’: ‘Basic <redacted>’ } reset_pin = None i2c = busio.I2C(board.SCL, board.SDA, frequency=100000) # Connect to a PM2.5 sensor over I2C pm25 = PM25_I2C(i2c, reset_pin) print(“Found PM2.5 sensor, reading data…”) while True: time.sleep(1) try: aqdata = pm25.read() # print(aqdata) current_time = datetime.datetime.utcnow().replace(microsecond=0) now = current_time.strftime(‘%Y-%m-%dT%H:%M:%S.%fZ’) # print(now) temp = [] temp.append(now) except RuntimeError: print(“Unable to read from sensor, retrying…”) continue for data in aqdata.values(): temp.append(data) payload = json.dumps([temp]) print(payload) conn.request(“PUT”, “/griddb/v2/gs_clustermfcloud5197/dbs/B2xcGQJy/containers/aqdata/rows”, payload, headers) res = conn.getresponse() data = res.read() print(data.decode(“utf-8”))</redacted> As explained above, there is nothing fancy or extraordinary about this script; it simply reads sensor data and then immediately pushes it out to the Cloud with a timestamp attached. The one thing to note, though, is that the GridDB Cloud is by default in UTC Time, so I have altered the timestamps to be attached to the data results to match UTC for consistency’s sake. And now that we have our data available in the cloud, we can move on to integrating it with other technologies. Home Assistant Home automation software comes in many varieties, with many companies providing their own hubs sold alongside their own products. Home assistant is unique in that it is, 1. completely open source, and 2. made to integrate with all manner of physical and virtual devices. For example, in my own personal home environment, I have Home Assistant running on a Raspberry Pi. To allow it to communicate with other physical devices, I have installed a Zigbee/Z-Wave USB Stick. If you are unfamiliar, Zigbee and Z-Wave are protocols used by smart home devices to communicate with their hubs. In my case, most of my smart light bulbs communicate through Zigbee, for example. In any case, through Home Assistant, we can create various scripts/automations for getting things done. Some examples can be: turn on bedroom lights at 1% at wake up time, or play my Spotify playlist every day at lunch time, etc etc. In the case of our air quality sensor, we can send out a direct HTTP Query against our sensor data and do something if our readings are higher than a certain threshold. For this blog, I have set up my home to turn my living room light bulbs on and to red if the pm1 particles are above a certain threshold, over the past 1 hour. But of course, because Home Assistant is flexible, you could set up any sort of notification method you’d like, including emails, phone push notifications, or even turning on your robovac! So, to continue on, we will need to first formulate our query on sensor readings, learn how to make HTTP Requests through Home Assistant, and learn how to act on the data returned by our query. After we set up our notifcation system for high sensor reading averages, we will also want to display all sensor readings in our Home Assistant dashboard. Formulating our Query First, let’s get our query settled. From my cursory research, pm1 particles are potentially the most threatening to our health because those particles are so tiny they can be inhaled and absorbed directly through the lungs; we will want to set up an alert for these particles. Before we begin, Let’s use cURL or Postman to test our HTTP Queries until we get what we are looking for; in my case, I used postman until I was happy with the results. The query I settled is the following: SELECT AVG(pm1) FROM aqdata WHERE ts > TIMESTAMP_ADD(HOUR, NOW(), -1) AND ts < NOW() AND pm1 > 10 “. This query will look at the data from the past 1 hour and will return with data if the avg value is over 10. Though please note I did not do strict research on what consitutes an unhealthy amount of pm1 particles, this is simply for demo purposes. But now that we have our query, we can figure out how to make HTTP REQUESTS through Home Assistant. Making HTTP Requests with Home Assistant Within the /config directory of the Home Assistant, there are a bunch of yaml files which are used to make configuration and automation changes to the software itself — very flexible and customizable. In our case, we want to add what is called a rest_command inside of the configuration yaml. We also would like to add an automation of what action to take based on the returned data from our rest_command — this will happen in the scripts.yaml file. Please note that all yaml files are included in the source code in the GitHub page linked above. So, here is what our configuration.yaml file will need to make the HTTP Request: #configuration.yaml rest_command: griddb_cloud_get_aqdata: url: https://cloud5197.griddb.com/griddb/v2/gs_clustermfcloud5197/dbs/B2xcGQJy/sql method: post content_type: “application/json” headers: authorization: “Basic <redacted>” payload: ‘[{“type” : “sql-select”, “stmt” : “SELECT AVG(pm1) FROM aqdata WHERE ts > TIMESTAMP_ADD(HOUR, NOW(), -1) AND ts < NOW() AND pm1 > 10 “}]'</redacted> You can see here we included all neccesary things to make an HTTP Sql Select request to our Cloud instance. Here, we are naming our rest_command as griddb_cloud_get_aqdata, so now in our scripts file we can directly call upon this service #scripts.yaml get_griddb_data: sequence: – service: rest_command.griddb_cloud_get_aqdata response_variable: aqdata – if: “{{ aqdata[‘status’] == 200 }}” then: – alias: Parse data variables: results: “{ {aqdata[‘content’][‘results’] }}” – if: “{{ results != ‘null’}}” then: service: light.turn_on target: entity_id: light.living_room data: rgb_color: – 240 – 0 – 0 As you can see, we are calling rest_command.griddb_cloud_get_aqdata as a service. This will run the HTTP Request and then parse the resulting data. If the results are null (meaning no data over our threshold), nothing will happen, but if we do get some data, we can take some action — or in this case, change the living room lights to on and change the RGB to completely red, this way everybody in the home knows that the air quality is compromised in some way. So to explain a bit more about how the yaml files work, the configuration yaml allows you to create services, in our case, the HTTP Request. The scripts file is for actions which may be run many times in many different spots, a bit analoguous to functions in software. And lastly we will use automations.yaml which sets up the trigger to when our script should be running. In our case, we want it run every 10 minutes — that is, every 10 minutes our Home Assistant will look at the average pm1 levels over the past 1 hour and take action if it is too high. #automations.yaml – id: ‘1713475588605’ alias: Get GridDB Data description: get the griddb data trigger: – platform: time_pattern minutes: /10 condition: [] action: – service: script.get_griddb_data mode: single You can see here that the automation is calling upon our script to get the GridDB data. You can also see that our trigger is every 10 minutes. Displaying Sensor Data Onto our Home Assistant Dashboard The last thing we would like to accomplish is to show our sensor readings directly onto the Home Assistant Dashboard. This will allow for all home users to constantly be aware othe readings. To do this, we will need to use the sensors.yaml file and establish new virtual “sensors” of HTTP Requests reading the sensor avg sensor data. For this, we needed to formulate a new SQL Query and this time around, I think we could make do with average readings for the past 24 hours. That query looks like this: SELECT ROUND(AVG(pm1)),ROUND(AVG(pm25)),ROUND(AVG(pm10)),ROUND(AVG(particles03)),ROUND(AVG(particles05)),ROUND(AVG(particles10)),ROUND(AVG(particles25)),ROUND(AVG(particles50)),ROUND(AVG(particles100)) FROM aqdata WHERE ts > TIMESTAMP_ADD(DAY, NOW(), -1) AND ts < NOW(). We simply grab all rounded averages from the past 1 day and use that info to be displayed in the dashboard. #sensors.yaml – platform: rest resource: https://cloud5197.griddb.com/griddb/v2/gs_clustermfcloud5197/dbs/B2xcGQJy/sql method: POST headers: authorization: “Basic <redacted>” Content-Type: application/json payload: ‘[{“type” : “sql-select”, “stmt” : “SELECT ROUND(AVG(pm1)),ROUND(AVG(pm25)),ROUND(AVG(pm10)),ROUND(AVG(particles03)),ROUND(AVG(particles05)),ROUND(AVG(particles10)),ROUND(AVG(particles25)),ROUND(AVG(particles50)),ROUND(AVG(particles100)) FROM aqdata WHERE ts > TIMESTAMP_ADD(DAY, NOW(), -1) AND ts < NOW() “}]’ value_template: “{{ value_json[0].results[0][0] }}” name: “Particle Matter 1” scan_interval: 3600</redacted> Here we use the value_template as the result that will be shown when we select this sensor as an entity. Unfortunately, I could not figure out how to use one singular HTTP Request with multiple values, so I needed to make each value as its own unique HTTP Request, just with a different index array position for the results. The example shown above, for instance, is for pm1 as it is index 0 of our result array due to the container schema. So now that we have our sensor set up, we can go to the dashboard and add it to be displayed. We can click edit (the pencil in the top right corner), then add card, then glance card, and then manually add all of your sensors. For me, the text editor looks like this: show_name: true show_icon: true show_state: true type: glance entities: – entity: sensor.particle_matter_1 – entity: sensor.particle_matter_2_5 – entity: sensor.particle_matter_10 – entity: sensor.particles_03 – entity: sensor.particles_05 – entity: sensor.particles_10 – entity: sensor.particles_25 – entity: sensor.particles_50 – entity: sensor.particles_100 title: Daily Air Quality Averages columns: 3 state_color: false And now we can have our daily averages at a glance. Of course, this just a few of things you can do with this information being so readily available; this really is the beauty of having the open source Home Assistant powering your home automations, and why it’s great to have GridDB Cloud hosting all of your data — it can be accessible from anywhere and you can upload data from anywhere, as long as you have an internet connection. You could, of course, monitor poor air in remote locations, or in locations of loved ones far away and take actions however you see fit. Conclusion In this article, we have learned how to connect physical hardware sensor data and how to push all of that wonderful data onto GridDB Cloud. And then we learned that we can take action directly upon that data with Home

More
Create a Server-less IoT Hub using GridDB Cloud and Microsoft Azure

One of GridDB’s main draws are its inherent strengths at managing an IoT system, with its unique key-container data model and memory-first data architecture. With that in mind, we wanted to showcase how you could build out an IoT system completely on the cloud, using Azure’s IoT Hub for the IoT devices and event handling, and GridDB Cloud for the data storage. The easiest way to manage this is to send data from IoT Hub device via HTTP Requests through the use of Azure Functions. For this article, we want to use the VS Code extensions for the Azure IoT Hub to create all of the resources we will need to create, test, and manage our virtual devices with our IoT Hub. We will also be utilizing the Azure CLI Tools to get a list of possible IP Addresses to be whitelisted in the GridDB Cloud. And then finally, we will create some Azure Functions (with VS Code), deploy them onto your Azure subscription, and then pair it with your Iot Hub. Again, the goal is for the device to emit data, trigger an event, and then send the data payload out to GridDB Cloud, seamlessly. Implementation We will now go through how to make this project work. Prerequisites To follow along, you will need an account with Microsoft Azure with credits (unfortunately we can’t get this up and running for free) and a free trial to GridDB Cloud. The cheapest available IoT Hub is estimated at $10/month. Though not required, this article will reference completing many of the actions through VS Code and through the Azure CLI tool. Creating Azure Resources Now let’s go through and create our Azure resources. We will need to create the following: Azure IoT Hub Virtual Device(s) Azure Functions Creating the Azure IoT Hub This is our main hub; it will be a one-stop shop for our event triggers and our virtual devices. To create a new hub, you can either create it in the Azure Web Portal (straightforward) or through the VS Code extension (potentially easier). To build with VS Code, first install the proper extension: https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-toolkit. Next, open up the Command Palette (F1 key) and select Azure IoT Hub: Create IoT Hub and fill out all of the necessary information, including region, subscription, and choose a globally unique name (the hub needs to be unique because it gets is own public facing DNS name). Adding Virtual Devices to the Hub Now we want to add virtual IoT devices, so once again open up the Command Palette and select Azure IoT Hub: Create Device and give it a device name. You can add as many devices as you’d like, but for now we’ll keep it at one device. We will dicuss how to send data from this device to the Hub later. Creating Azure Functions for Monitor Events Lastly we would like to utilize Azure Functions to monitor our device to detect changes; once an event is detected, we want to be able to control what occurs next, in our case, an HTTP Request to be made. To do so, we will need one more VS Code Extension: Azure Functions And now, once again, open up your Command Palette and select Azure Functions: Create Function App in Azure… to create a function. At this stage, we are naming it, creating a local instance of it, and choosing a runtime, of which we chose the latest version of node.js available. For more information on how these event monitoring system works within Azure, here is some of the documentation: https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-event-grid Sending our Data Payloads to GridDB Cloud With our resources in place, the next step is to set up the event monitoring. We want for our hub to make HTTP Requests of our data payloads whenever it detects that one of its devices emits data. To do so, we will use the Azure Function that we created in the previous step and event monitoring. Azure Function: Source Code for when Event is Triggered We have already created the source code necessary for this step, so please clone the repo as indicated above. The code is very simple: it takes the sample code built by Azure for eventGridTriggers and simply adds a component to make HTTP Requests whenever the trigger is fired. Here is what the source code looks like: const { app } = require(‘@azure/functions’); const axios = require(‘axios’); require(‘dotenv’).config() app.eventGrid(‘eventGridTrigger1’, { handler: (event, context) => { context.log(‘Event grid function processed event:’, event); const container = ‘azureTest’ const auth = { username: process.env.CLOUD_USERNAME, password: process.env.CLOUD_PASSWORD } const headers = { ‘Content-Type’: ‘application/json’ } //HTTP Request to create our container called azureTest const dataCreation = { “container_name”: container, “container_type”: “COLLECTION”, “rowkey”: false, “columns”: [ { “name”: “test”, “type”: “STRING” } ] } const configCreation = { method: ‘POST’, maxBodyLength: Infinity, url: process.env.CLOUD_URL + “/containers”, headers, data: dataCreation, auth } axios.request(configCreation) .then((response) => { context.log(response.statusText); context.log(JSON.stringify(response.data)); }) .catch((error) => { context.log(error); context.error(error) }); //HTTP Request to send data to our container const data = JSON.stringify([ [“GRID EVENT TRIGGERED”] ]); let config = { method: ‘PUT’, maxBodyLength: Infinity, url: process.env.CLOUD_URL + “/containers/” + container + “/rows”, headers, data, auth }; axios.request(config) .then((response) => { context.log(response.statusText); context.log(JSON.stringify(response.data)); }) .catch((error) => { context.log(error); context.error(error) }); } }); We are using the GridDB Cloud Web API to build our HTTP Request to send the payload to be saved. With everything in place, we can deploy our function to the Azure Cloud. You will need to copy the .env-example file and rename it to .env and fill out the values yourself. For the CLOUD URL variable, please just copy the GridDB WebAPI URL from your GridDB Portal as is. Deploy Azure Function to Azure Cloud So now, once again, make sure you have the source code provided by this blog in the repo pointed out above and make sure that your VS Code current folder/project is inside of a directory that has the source code. Next, in the Command Palette, select Azure Functions: Deploy to Function App and select the Function you created above. This will create a zip of your current working directory (meaning all of the source code you downloaded from this article’s repo) and deploy it directly to your Azure Account. And now we want to tie this source code with our IoT Hub and our test virtual device. Tying Azure Function to IoT Hub For this last step, we would like for our hub to utilize the Azure Function created in the previous step. For this step, we will use the Azure Web Portal. Open up the portal and find your IoT Hub Resource. Within that page navigate to: Events -> Azure Function. We will be creating a new event, so give it a name and system topic. For the “filter for event types” box, keep it selected only to Device Telemetry. And lastly, click Configure an Endpoint. In the side panel, most likely everything will self-populate, but if not, choose the azure function app and functions we made previous (ie. function is called: eventGridTrigger1). NOTE: For this to work, your account will need the registry Microsoft.EventGrid in the subscription page enabled. Receiving Data from IoT Hub Lastly, even if we were to trigger an event of some payload to our hub from our device, the HTTP Request would fail because the GridDB Cloud requires all incoming IP Addresses to be whitelisted. The issue now arises that we are using a lightweight Azure Function to handle our events, not a full blown server/VM with one static IP Address. Luckily, there is a way to retrieve all possible IP Addresses used by our Azure Function as shown in these docs: https://learn.microsoft.com/en-us/azure/azure-functions/ip-addresses. You will need to install the Azure CLI and login to your proper account/subscription. Once there, you can run the following command: $ az functionapp show –resource-group <INSERT RESOURCE NAME> –name <INSERT AZURE FUNCTION NAME> –query possibleOutboundIpAddresses –output tsv > outbound.txt This will output a list of about 40 possible IP Addresses and save it into a text file called outbound.txt. As long as we add all of these to the GridDB Cloud Whitelist, we will be able to successfully save all of our events as they occur. Whitelist IP Addresses Obviously, while hand-writing each of these IP addresses is technically feasible, it’d be tedious and horrifying. To add each of these with a simple do while loop in bash, we first need to grab the endpoint with all authorization headers. To do so, open up your GridDB Cloud Web Portal, open up the dev console, navigate to the network tab, filter for XHR requests, clear everything out, and add one of the 40 IP addresses on your list. When you hit submit, you will see a 201 POST Request, now do the following: right click -> Copy Value -> Copy as cURL. You will now have the cURL Command saved in your clipboard. Open up the whitelistIp.sh script and enter in the endpoint unique to you inside of the runCurl script sans the IP Address at the end. Here is what the entire script looks like: #!/bin/bash file=$1 #EXAMPLE #runCurl() { # curl ‘https://cloud57.griddb.com/mfcloud57/dbaas/web-api/contracts/m01wc1a/access-list’ -X POST -H ‘User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:133.0) Gecko/20100101 Firefox/133.0’ -H ‘Accept: application/json, text/plain, */*’ -H ‘Accept-Language: en-US,en;q=0.5’ -H ‘Accept-Encoding: gzip, deflate, br, zstd’ -H ‘Content-Type: application/json;charset=utf-8’ -H ‘Access-Control-Allow-Origin: *’ -H ‘Authorization: Bearer eyJ0eXAiOiJBY2Nlc3MiLCJIUzI1NiJ9.eyJzdWIiOiJkMTg4NjlhZC1mYjUxLTQwMWMtOWQ0Yy03YzI3MGNkZTBmZDkiLCJleHAiOjE3MzEwMTEyMTMsInJvbGUiOiJBZG1pbiIsInN5c3RlbVR5cGUiOjF9.B1MsV9-Nu8m8mJbsp6dKABjJDBjQDdc9aRLffTlTcVM’ -H ‘Origin: https://cloud5197.griddb.com’ -H ‘Connection: keep-alive’ -H ‘Referer: https://cloud5197.griddb.com/mfcloud5197/portal/’ -H ‘Sec-Fetch-Dest: empty’ -H ‘Sec-Fetch-Mode: cors’ -H ‘Sec-Fetch-Site: same-origin’ -H ‘Priority: u=0’ -H ‘Pragma: no-cache’ -H ‘Cache-Control: no-cache’ –data-raw $1 #} runCurl() { <paste VALUE HERE> $1 } while IFS= read -r line; do for ip in ${line//,/ }; do echo “Whitelisting IP Address: $ip” runCurl $ip done done < “$file”</paste> The script will take the outputs from the Azure CLI command to make a string of IP Addresses, seperate out the value by the comma, and then run the cURL command for each individual address. This script expects the file name as a CLI argument when running ie: $ ./whitelistIp.sh outbound.txt. Sending Data from Device to Cloud Now that we’ve got everything set up, the last thing we will do is send data from our virtual device to the cloud. We then expect to see our test string being published into our GridDB Cloud instance. In the source code, we are simply saving a string which contains the characters “GRID EVENT TRIGGERED” to a new container called azureTest as a test to make sure our infrastructure works as expected. From your VS Code window, in the explorer tab, find the Azure IoT Hub Resource panel and find your IoT Hub Manager and its devices. Right click the device you want to use and select Send D2C Messages to IoT Hub (D2C = Device to Cloud). Once your message is sent, you should have a new container in your GridDB Cloud instance called ‘azureTest’ and it should have one row of data inside of it with a value of ‘GRID EVENT TRIGGERED’ — cool! Conclusion And that does it! You can now expand your Cloud IoT Infrastructure as much as you’d like. Make new data containers with real schemas tied to real devices and sync them up and save all data into GridDB Cloud for a purely server-less

More