From e5900475b0d346a9cf1da9ebdc065dde185c649f Mon Sep 17 00:00:00 2001 From: Vimal Kumar <102946997+vimal-knoldus@users.noreply.github.com> Date: Wed, 27 Sep 2023 19:10:43 +0530 Subject: [PATCH 1/8] Update application.yml --- admin-service/src/main/resources/application.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin-service/src/main/resources/application.yml b/admin-service/src/main/resources/application.yml index 62516cde..9904dfa9 100644 --- a/admin-service/src/main/resources/application.yml +++ b/admin-service/src/main/resources/application.yml @@ -1,4 +1,4 @@ spring: profiles: - active: cosmos + active: firestore From 8fcaee26fb4f82503aeb3d0309aa1d759df5415f Mon Sep 17 00:00:00 2001 From: DeepaMittal Date: Tue, 3 Oct 2023 03:25:28 +0530 Subject: [PATCH 2/8] added cors configuration to connect with frontend --- cart-service/pom.xml | 5 +++++ .../nashtech/car/cart/CartApplication.java | 3 +++ .../java/com/nashtech/car/cart/WebConfig.java | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 cart-service/src/main/java/com/nashtech/car/cart/WebConfig.java diff --git a/cart-service/pom.xml b/cart-service/pom.xml index 9ddca2e2..81e2495b 100644 --- a/cart-service/pom.xml +++ b/cart-service/pom.xml @@ -53,6 +53,11 @@ spring-boot-starter-data-jpa + + org.springframework.boot + spring-boot-starter-data-rest + + mysql mysql-connector-java diff --git a/cart-service/src/main/java/com/nashtech/car/cart/CartApplication.java b/cart-service/src/main/java/com/nashtech/car/cart/CartApplication.java index 40ddaab0..b1e4713c 100644 --- a/cart-service/src/main/java/com/nashtech/car/cart/CartApplication.java +++ b/cart-service/src/main/java/com/nashtech/car/cart/CartApplication.java @@ -2,7 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + @SpringBootApplication +@ComponentScan (basePackages = "com.nashtech.car.cart") public class CartApplication { public static void main(String[] args) { SpringApplication.run(CartApplication.class, args); diff --git a/cart-service/src/main/java/com/nashtech/car/cart/WebConfig.java b/cart-service/src/main/java/com/nashtech/car/cart/WebConfig.java new file mode 100644 index 00000000..77a60933 --- /dev/null +++ b/cart-service/src/main/java/com/nashtech/car/cart/WebConfig.java @@ -0,0 +1,19 @@ +package com.nashtech.car.cart; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@ComponentScan +public class WebConfig implements WebMvcConfigurer { + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOrigins("http://localhost:4200/") // Allow requests from Angular app + .allowedMethods("GET", "POST", "PUT", "DELETE") + .maxAge(3600); + } +} From 090af5a62a2d5ca02198d8725cc68d78655ea859 Mon Sep 17 00:00:00 2001 From: Anshuman Shukla Date: Mon, 16 Oct 2023 14:23:59 +0530 Subject: [PATCH 3/8] Added Elastic Search Azure --- elastic-search/.gitignore | 33 ++ elastic-search/docker-compose.yml | 17 + .../k8s-azure/elastic-deployment.yaml | 23 ++ elastic-search/k8s-azure/elastic-svc.yaml | 17 + .../k8s-azure/scripts/azure-setup.sh | 35 ++ elastic-search/mvnw | 308 ++++++++++++++++++ elastic-search/mvnw.cmd | 205 ++++++++++++ elastic-search/pom.xml | 107 ++++++ .../ElasticSearchApplication.java | 14 + .../config/ElasticAzureConfig.java | 40 +++ .../controller/CarEntityController.java | 38 +++ .../elasticsearch/entity/CarEntity.java | 36 ++ .../eventlistener/AzureConsumer.java | 28 ++ .../eventlistener/AzureKafkaProducer.java | 26 ++ .../repository/CarEntityRepository.java | 15 + .../elasticsearch/service/CarService.java | 15 + .../service/impl/AzureCarService.java | 40 +++ .../util/CarEntitySerializer.java | 9 + .../elasticsearch/util/CarMapper.java | 36 ++ .../src/main/resources/application.yaml | 36 ++ .../ElasticSearchApplicationTests.java | 13 + 21 files changed, 1091 insertions(+) create mode 100644 elastic-search/.gitignore create mode 100644 elastic-search/docker-compose.yml create mode 100644 elastic-search/k8s-azure/elastic-deployment.yaml create mode 100644 elastic-search/k8s-azure/elastic-svc.yaml create mode 100644 elastic-search/k8s-azure/scripts/azure-setup.sh create mode 100644 elastic-search/mvnw create mode 100644 elastic-search/mvnw.cmd create mode 100644 elastic-search/pom.xml create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/ElasticSearchApplication.java create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/config/ElasticAzureConfig.java create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/entity/CarEntity.java create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureConsumer.java create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureKafkaProducer.java create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/repository/CarEntityRepository.java create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/CarService.java create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/impl/AzureCarService.java create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/util/CarEntitySerializer.java create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/util/CarMapper.java create mode 100644 elastic-search/src/main/resources/application.yaml create mode 100644 elastic-search/src/test/java/com/elasticsearch/elasticsearch/ElasticSearchApplicationTests.java diff --git a/elastic-search/.gitignore b/elastic-search/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/elastic-search/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/elastic-search/docker-compose.yml b/elastic-search/docker-compose.yml new file mode 100644 index 00000000..77e1dfae --- /dev/null +++ b/elastic-search/docker-compose.yml @@ -0,0 +1,17 @@ +version: '3' +services: + elasticsearch: + image: docker.elastic.co/elasticsearch/elasticsearch:7.6.0 + container_name: elasticsearch + environment: + - node.name=elasticsearch + - cluster.name=mycluster + - discovery.type=single-node + ports: + - "9200:9200" + - "9300:9300" + ulimits: + memlock: + soft: -1 + hard: -1 + mem_limit: 1g diff --git a/elastic-search/k8s-azure/elastic-deployment.yaml b/elastic-search/k8s-azure/elastic-deployment.yaml new file mode 100644 index 00000000..1309f63d --- /dev/null +++ b/elastic-search/k8s-azure/elastic-deployment.yaml @@ -0,0 +1,23 @@ +apiVersion: elasticsearch.k8s.elastic.co/v1 +kind: Elasticsearch +metadata: + name: elasticsearch +spec: + version: 7.6.0 + nodeSets: + - name: default + count: 1 + config: + node.master: true + node.data: true + node.ingest: true + node.store.allow_mmap: false + xpack.security.authc: + anonymous: + username: anonymous + roles: superuser + authz_exception: false + http: + tls: + selfSignedCertificate: + disabled: true \ No newline at end of file diff --git a/elastic-search/k8s-azure/elastic-svc.yaml b/elastic-search/k8s-azure/elastic-svc.yaml new file mode 100644 index 00000000..8ea26cc8 --- /dev/null +++ b/elastic-search/k8s-azure/elastic-svc.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Service +metadata: + name: elasticsearch-es-http +spec: + selector: + app.kubernetes.io/name: MyApp + ports: + - protocol: TCP + port: 9200 + targetPort: 9200 + clusterIP: 10.0.232.81 + type: LoadBalancer +status: + loadBalancer: + ingress: + - ip: 192.0.2.127 \ No newline at end of file diff --git a/elastic-search/k8s-azure/scripts/azure-setup.sh b/elastic-search/k8s-azure/scripts/azure-setup.sh new file mode 100644 index 00000000..fcec77ab --- /dev/null +++ b/elastic-search/k8s-azure/scripts/azure-setup.sh @@ -0,0 +1,35 @@ +# Variables to be used +resourceGroupName="az-nashtech-resource-group" +resourceLocation="EastUS" +clusterName="ntdemocluster" + +#Scripts + +#Login to Azure in Local +az login + +#Create Resource Group +az group create --name $resourceGroupName --location $resourceLocation + +#Create an AKS Cluster with default setting +az aks create --resource-group $resourceGroupName --name $clusterName + +#Get the Kubernetes Configuration to run further commands +az aks get-credentials --resource-group $resourceGroupName --name $clusterName + +#Run this command to install CRDS +kubectl create -f https://download.elastic.co/downloads/eck/2.9.0/crds.yaml + +# Download the required repo for elastic +kubectl apply -f https://download.elastic.co/downloads/eck/2.9.0/operator.yaml + +#Apply the deployment File +kubectl apply -f elastic-deployment.yaml + +# Once Pod is up take the Cluster Ip and add it into the elastic-svc.yaml file +kubectl apply -f elastic-svc.yaml + + + + + diff --git a/elastic-search/mvnw b/elastic-search/mvnw new file mode 100644 index 00000000..66df2854 --- /dev/null +++ b/elastic-search/mvnw @@ -0,0 +1,308 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Apache Maven Wrapper startup batch script, version 3.2.0 +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /usr/local/etc/mavenrc ] ; then + . /usr/local/etc/mavenrc + fi + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "$(uname)" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + JAVA_HOME="$(/usr/libexec/java_home)"; export JAVA_HOME + else + JAVA_HOME="/Library/Java/Home"; export JAVA_HOME + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=$(java-config --jre-home) + fi +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$JAVA_HOME" ] && + JAVA_HOME=$(cygpath --unix "$JAVA_HOME") + [ -n "$CLASSPATH" ] && + CLASSPATH=$(cygpath --path --unix "$CLASSPATH") +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$JAVA_HOME" ] && [ -d "$JAVA_HOME" ] && + JAVA_HOME="$(cd "$JAVA_HOME" || (echo "cannot cd into $JAVA_HOME."; exit 1); pwd)" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="$(which javac)" + if [ -n "$javaExecutable" ] && ! [ "$(expr "\"$javaExecutable\"" : '\([^ ]*\)')" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=$(which readlink) + if [ ! "$(expr "$readLink" : '\([^ ]*\)')" = "no" ]; then + if $darwin ; then + javaHome="$(dirname "\"$javaExecutable\"")" + javaExecutable="$(cd "\"$javaHome\"" && pwd -P)/javac" + else + javaExecutable="$(readlink -f "\"$javaExecutable\"")" + fi + javaHome="$(dirname "\"$javaExecutable\"")" + javaHome=$(expr "$javaHome" : '\(.*\)/bin') + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="$(\unset -f command 2>/dev/null; \command -v java)" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=$(cd "$wdir/.." || exit 1; pwd) + fi + # end of workaround + done + printf '%s' "$(cd "$basedir" || exit 1; pwd)" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + # Remove \r in case we run on Windows within Git Bash + # and check out the repository with auto CRLF management + # enabled. Otherwise, we may read lines that are delimited with + # \r\n and produce $'-Xarg\r' rather than -Xarg due to word + # splitting rules. + tr -s '\r\n' ' ' < "$1" + fi +} + +log() { + if [ "$MVNW_VERBOSE" = true ]; then + printf '%s\n' "$1" + fi +} + +BASE_DIR=$(find_maven_basedir "$(dirname "$0")") +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}; export MAVEN_PROJECTBASEDIR +log "$MAVEN_PROJECTBASEDIR" + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +wrapperJarPath="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" +if [ -r "$wrapperJarPath" ]; then + log "Found $wrapperJarPath" +else + log "Couldn't find $wrapperJarPath, downloading it ..." + + if [ -n "$MVNW_REPOURL" ]; then + wrapperUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar" + else + wrapperUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar" + fi + while IFS="=" read -r key value; do + # Remove '\r' from value to allow usage on windows as IFS does not consider '\r' as a separator ( considers space, tab, new line ('\n'), and custom '=' ) + safeValue=$(echo "$value" | tr -d '\r') + case "$key" in (wrapperUrl) wrapperUrl="$safeValue"; break ;; + esac + done < "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.properties" + log "Downloading from: $wrapperUrl" + + if $cygwin; then + wrapperJarPath=$(cygpath --path --windows "$wrapperJarPath") + fi + + if command -v wget > /dev/null; then + log "Found wget ... using wget" + [ "$MVNW_VERBOSE" = true ] && QUIET="" || QUIET="--quiet" + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget $QUIET "$wrapperUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" + else + wget $QUIET --http-user="$MVNW_USERNAME" --http-password="$MVNW_PASSWORD" "$wrapperUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + log "Found curl ... using curl" + [ "$MVNW_VERBOSE" = true ] && QUIET="" || QUIET="--silent" + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl $QUIET -o "$wrapperJarPath" "$wrapperUrl" -f -L || rm -f "$wrapperJarPath" + else + curl $QUIET --user "$MVNW_USERNAME:$MVNW_PASSWORD" -o "$wrapperJarPath" "$wrapperUrl" -f -L || rm -f "$wrapperJarPath" + fi + else + log "Falling back to using Java to download" + javaSource="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.java" + javaClass="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.class" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaSource=$(cygpath --path --windows "$javaSource") + javaClass=$(cygpath --path --windows "$javaClass") + fi + if [ -e "$javaSource" ]; then + if [ ! -e "$javaClass" ]; then + log " - Compiling MavenWrapperDownloader.java ..." + ("$JAVA_HOME/bin/javac" "$javaSource") + fi + if [ -e "$javaClass" ]; then + log " - Running MavenWrapperDownloader.java ..." + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$wrapperUrl" "$wrapperJarPath") || rm -f "$wrapperJarPath" + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +# If specified, validate the SHA-256 sum of the Maven wrapper jar file +wrapperSha256Sum="" +while IFS="=" read -r key value; do + case "$key" in (wrapperSha256Sum) wrapperSha256Sum=$value; break ;; + esac +done < "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.properties" +if [ -n "$wrapperSha256Sum" ]; then + wrapperSha256Result=false + if command -v sha256sum > /dev/null; then + if echo "$wrapperSha256Sum $wrapperJarPath" | sha256sum -c > /dev/null 2>&1; then + wrapperSha256Result=true + fi + elif command -v shasum > /dev/null; then + if echo "$wrapperSha256Sum $wrapperJarPath" | shasum -a 256 -c > /dev/null 2>&1; then + wrapperSha256Result=true + fi + else + echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." + echo "Please install either command, or disable validation by removing 'wrapperSha256Sum' from your maven-wrapper.properties." + exit 1 + fi + if [ $wrapperSha256Result = false ]; then + echo "Error: Failed to validate Maven wrapper SHA-256, your Maven wrapper might be compromised." >&2 + echo "Investigate or delete $wrapperJarPath to attempt a clean download." >&2 + echo "If you updated your Maven version, you need to update the specified wrapperSha256Sum property." >&2 + exit 1 + fi +fi + +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$JAVA_HOME" ] && + JAVA_HOME=$(cygpath --path --windows "$JAVA_HOME") + [ -n "$CLASSPATH" ] && + CLASSPATH=$(cygpath --path --windows "$CLASSPATH") + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=$(cygpath --path --windows "$MAVEN_PROJECTBASEDIR") +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $*" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +# shellcheck disable=SC2086 # safe args +exec "$JAVACMD" \ + $MAVEN_OPTS \ + $MAVEN_DEBUG_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/elastic-search/mvnw.cmd b/elastic-search/mvnw.cmd new file mode 100644 index 00000000..95ba6f54 --- /dev/null +++ b/elastic-search/mvnw.cmd @@ -0,0 +1,205 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Apache Maven Wrapper startup batch script, version 3.2.0 +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* +if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %* +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set WRAPPER_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar" + +FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET WRAPPER_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET WRAPPER_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %WRAPPER_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%WRAPPER_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM If specified, validate the SHA-256 sum of the Maven wrapper jar file +SET WRAPPER_SHA_256_SUM="" +FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperSha256Sum" SET WRAPPER_SHA_256_SUM=%%B +) +IF NOT %WRAPPER_SHA_256_SUM%=="" ( + powershell -Command "&{"^ + "$hash = (Get-FileHash \"%WRAPPER_JAR%\" -Algorithm SHA256).Hash.ToLower();"^ + "If('%WRAPPER_SHA_256_SUM%' -ne $hash){"^ + " Write-Output 'Error: Failed to validate Maven wrapper SHA-256, your Maven wrapper might be compromised.';"^ + " Write-Output 'Investigate or delete %WRAPPER_JAR% to attempt a clean download.';"^ + " Write-Output 'If you updated your Maven version, you need to update the specified wrapperSha256Sum property.';"^ + " exit 1;"^ + "}"^ + "}" + if ERRORLEVEL 1 goto error +) + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% ^ + %JVM_CONFIG_MAVEN_PROPS% ^ + %MAVEN_OPTS% ^ + %MAVEN_DEBUG_OPTS% ^ + -classpath %WRAPPER_JAR% ^ + "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^ + %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" +if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%"=="on" pause + +if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE% + +cmd /C exit /B %ERROR_CODE% diff --git a/elastic-search/pom.xml b/elastic-search/pom.xml new file mode 100644 index 00000000..a308f8cc --- /dev/null +++ b/elastic-search/pom.xml @@ -0,0 +1,107 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.1.1 + + + com.elasticsearch + elastic-search + 0.0.1-SNAPSHOT + jar + elastic-search + Demo project for Spring Boot + + 17 + 4.9.0 + + + + es-snapshots + elasticsearch snapshot repo + https://snapshots.elastic.co/maven/ + + + + + org.springframework.boot + spring-boot-starter-data-elasticsearch + + + org.elasticsearch.client + elasticsearch-rest-high-level-client + 7.17.13 + + + org.springframework.boot + spring-boot-starter-web + + + + + + + + + + + + + + + + + + + org.springframework.kafka + spring-kafka + + + com.fasterxml.jackson.core + jackson-databind + 2.15.2 + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + com.azure.spring + spring-cloud-azure-dependencies + ${spring-cloud-azure.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/ElasticSearchApplication.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/ElasticSearchApplication.java new file mode 100644 index 00000000..0496098b --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/ElasticSearchApplication.java @@ -0,0 +1,14 @@ +package com.elasticsearch.elasticsearch; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; + +@SpringBootApplication +public class ElasticSearchApplication { + + public static void main(String[] args) { + SpringApplication.run(ElasticSearchApplication.class, args); + } + +} diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/config/ElasticAzureConfig.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/config/ElasticAzureConfig.java new file mode 100644 index 00000000..8494bbc0 --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/config/ElasticAzureConfig.java @@ -0,0 +1,40 @@ +package com.elasticsearch.elasticsearch.config; + +import co.elastic.clients.json.jackson.JacksonJsonpMapper; +import co.elastic.clients.transport.ElasticsearchTransport; +import co.elastic.clients.transport.rest_client.RestClientTransport; +import co.elastic.clients.util.ContentType; +import org.apache.http.HttpHeaders; +import org.apache.http.HttpHost; +import org.apache.http.HttpResponseInterceptor; +import org.apache.http.message.BasicHeader; + +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestClientBuilder; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; + +import java.util.List; + +@Profile("azure") +@Configuration +public class ElasticAzureConfig { + @Value("${azure.elastic.hostname}") + private String hostName; + + @Value("${azure.elastic.port}") + private int port; + + @Bean + public RestClient getResClient() { + RestClientBuilder.HttpClientConfigCallback httpClientConfigCallback = + httpClientBuilder -> httpClientBuilder.setDefaultHeaders( + List.of(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON))) + .addInterceptorLast((HttpResponseInterceptor) (response, context) -> + response.addHeader("X-Elastic-Product", "Elasticsearch")); + return RestClient.builder(new HttpHost(hostName, port)).setHttpClientConfigCallback(httpClientConfigCallback).build(); + } + +} diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java new file mode 100644 index 00000000..2d8870d1 --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java @@ -0,0 +1,38 @@ +package com.elasticsearch.elasticsearch.controller; + +import com.elasticsearch.elasticsearch.entity.CarEntity; +import com.elasticsearch.elasticsearch.service.CarService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.io.IOException; +import java.util.List; + +@RestController +@RequestMapping("/apis/car") +public class CarEntityController { + @Autowired + private CarService service; + /*@Autowired + private AzureKafkaProducer producer;*/ + + @GetMapping("/all") + public ResponseEntity> getAllCarEntity() { + + return new ResponseEntity<>(service.getAllCarEntity(), HttpStatus.OK); + } + + @GetMapping("/{carId}") + public ResponseEntity getCarDetailsById(@PathVariable("carId") String carId) throws IOException { + CarEntity carEntityWithCarId = service.getCarEntityWithCarId(Integer.valueOf(carId)); + return new ResponseEntity<>(carEntityWithCarId, HttpStatus.OK); + } + + @PostMapping("/save") + public CarEntity saveCarEntity(@RequestBody CarEntity carEntity) { + // producer.send(carEntity); + return service.saveCarEntity(carEntity); + } +} diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/entity/CarEntity.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/entity/CarEntity.java new file mode 100644 index 00000000..a7ed0d74 --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/entity/CarEntity.java @@ -0,0 +1,36 @@ +package com.elasticsearch.elasticsearch.entity; +import lombok.Data; +import org.springframework.data.annotation.Id; +import org.springframework.data.elasticsearch.annotations.Document; +import org.springframework.data.elasticsearch.annotations.Field; +import org.springframework.data.elasticsearch.annotations.FieldType; + + +@Data +@Document(indexName = "car-details") +public class CarEntity { + + @Id + private String id; + + @Field(type = FieldType.Integer, name = "carId") + private Integer carId; + + @Field(type = FieldType.Text, name = "model") + private String model; + + @Field(type = FieldType.Text, name = "brand") + private String brand; + + @Field(type = FieldType.Long, name = "year") + private Long year; + + @Field(type = FieldType.Text, name = "color") + private String color; + + @Field(type = FieldType.Double, name = "name") + private Double mileage; + + @Field(type = FieldType.Double, name = "name") + private Double price; +} \ No newline at end of file diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureConsumer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureConsumer.java new file mode 100644 index 00000000..910d3cf1 --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureConsumer.java @@ -0,0 +1,28 @@ +/* +package com.elasticsearch.elasticsearch.eventlistener; + +import com.elasticsearch.elasticsearch.entity.CarEntity; +import com.elasticsearch.elasticsearch.service.CarService; +import com.elasticsearch.elasticsearch.util.CarMapper; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; +import org.springframework.kafka.annotation.KafkaListener; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +@Profile("azure") +public class AzureConsumer { + @Autowired + private CarService service; + + @KafkaListener(topics = "${topic.producer}") + public void consumeEvent(String event) { + log.info("Received message from kafka queue: {}", event); + CarEntity carEntity = CarMapper.mapStringToEntity(event); + service.saveCarEntity(carEntity); + log.info(carEntity.toString()); + } +} +*/ diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureKafkaProducer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureKafkaProducer.java new file mode 100644 index 00000000..df7b9848 --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureKafkaProducer.java @@ -0,0 +1,26 @@ +/* +package com.elasticsearch.elasticsearch.eventlistener; + +import com.elasticsearch.elasticsearch.entity.CarEntity; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Profile; +import org.springframework.kafka.core.KafkaTemplate; +import org.springframework.stereotype.Service; +@Slf4j +@Service +@RequiredArgsConstructor +@Profile("azure") +public class AzureKafkaProducer { + + private final KafkaTemplate kafkaTemplate; + @Value(("${topic.producer}")) + private String topicName; + + public void send(CarEntity message){ + this.kafkaTemplate.send(topicName,message); + log.info("Published the message [{}] to the kafka queue: [{}]",message,topicName); + } +} +*/ diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/repository/CarEntityRepository.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/repository/CarEntityRepository.java new file mode 100644 index 00000000..e60cee99 --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/repository/CarEntityRepository.java @@ -0,0 +1,15 @@ +package com.elasticsearch.elasticsearch.repository; + +import com.elasticsearch.elasticsearch.entity.CarEntity; +import org.springframework.data.elasticsearch.annotations.Query; +import org.springframework.data.elasticsearch.core.SearchHits; +import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CarEntityRepository extends ElasticsearchRepository { + + CarEntity findByCarId(Integer carId); + @Query(value = "?0") + SearchHits searchByCustomQuery(String query); +} diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/CarService.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/CarService.java new file mode 100644 index 00000000..3aa73a13 --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/CarService.java @@ -0,0 +1,15 @@ +package com.elasticsearch.elasticsearch.service; + +import com.elasticsearch.elasticsearch.entity.CarEntity; + +import java.util.List; + + +public interface CarService { + + public CarEntity getCarEntityWithCarId(Integer carId); + + public List getAllCarEntity(); + + public CarEntity saveCarEntity(CarEntity entity); +} diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/impl/AzureCarService.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/impl/AzureCarService.java new file mode 100644 index 00000000..620e578e --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/impl/AzureCarService.java @@ -0,0 +1,40 @@ +package com.elasticsearch.elasticsearch.service.impl; + +import com.elasticsearch.elasticsearch.entity.CarEntity; +import com.elasticsearch.elasticsearch.repository.CarEntityRepository; +import com.elasticsearch.elasticsearch.service.CarService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; +import org.springframework.data.elasticsearch.core.SearchHit; +import org.springframework.data.elasticsearch.core.SearchHits; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +@Service +@Profile("azure") +public class AzureCarService implements CarService { + @Autowired + private CarEntityRepository repository; + + @Override + public CarEntity getCarEntityWithCarId(Integer carId) { + CarEntity byCarId = repository.findByCarId(carId); + return byCarId; + } + + @Override + public List getAllCarEntity() { + return StreamSupport.stream(repository.findAll().spliterator(), false).collect(Collectors.toList()); + } + + @Override + public CarEntity saveCarEntity(CarEntity entity) { + + return repository.save(entity); + } + +} + diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/util/CarEntitySerializer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/util/CarEntitySerializer.java new file mode 100644 index 00000000..a54558ba --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/util/CarEntitySerializer.java @@ -0,0 +1,9 @@ +package com.elasticsearch.elasticsearch.util; + +import com.elasticsearch.elasticsearch.entity.CarEntity; +import org.springframework.kafka.support.serializer.JsonSerializer; + + +public class CarEntitySerializer extends JsonSerializer { + +} diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/util/CarMapper.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/util/CarMapper.java new file mode 100644 index 00000000..7dc0312e --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/util/CarMapper.java @@ -0,0 +1,36 @@ +package com.elasticsearch.elasticsearch.util; + +import com.elasticsearch.elasticsearch.entity.CarEntity; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; + +import java.util.Map; + +@Slf4j +public class CarMapper { + + public static CarEntity mapStringToEntity(String payload) { + + ObjectMapper objectMapper = new ObjectMapper(); + CarEntity carEntity = new CarEntity(); + try { + Map payloadMap = objectMapper.readValue(payload, new TypeReference>() { + }); + log.info("After conversion : {}", payloadMap); + carEntity.setId((String) payloadMap.get("id")); + carEntity.setCarId((Integer) payloadMap.get("carId")); + carEntity.setBrand((String) payloadMap.get("brand")); + carEntity.setModel((String) payloadMap.get("model")); + carEntity.setYear(Long.valueOf((Integer) payloadMap.get("year"))); + carEntity.setColor((String) payloadMap.get("color")); + carEntity.setMileage((Double) payloadMap.get("mileage")); + carEntity.setPrice((Double) payloadMap.get("price")); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + + return carEntity; + } +} diff --git a/elastic-search/src/main/resources/application.yaml b/elastic-search/src/main/resources/application.yaml new file mode 100644 index 00000000..ec8c9363 --- /dev/null +++ b/elastic-search/src/main/resources/application.yaml @@ -0,0 +1,36 @@ +spring: + profiles: + active: + - azure + + +#spring: +# kafka: +# bootstrap-servers: event-hub-elastic.servicebus.windows.net:9093 +# client-id: elastic-search +# properties: +# sasl.jaas.config: org.apache.kafka.common.security.plain.PlainLoginModule required username="$ConnectionString" password="Endpoint=sb://event-hub-elastic.servicebus.windows.net/;SharedAccessKeyName=sendandrecieve;SharedAccessKey=w8+oeEd7hWLHAVtB9PWfvYyfXoZ0bx1z4+AEhDsw0Gs="; +# sasl.mechanism: PLAIN +# security.protocol: SASL_SSL +# producer: +# value-serializer: com.elasticsearch.elasticsearch.util.CarEntitySerializer +# consumer: +# group-id: $Default +# properties: +# spring.json: +# use.type.headers: true +# value.default.type: com.elasticsearch.elasticsearch.entity.AzureCarEntity +#topic: +# producer: car-event-hub + +azure: + elastic: + hostname: ${HOSTNAME:localhost} + port: ${PORT:9200} +# GCP IP: http://35.201.207.148:9200/ +# AZure IP: http://20.204.229.0:9200/ + + + + + diff --git a/elastic-search/src/test/java/com/elasticsearch/elasticsearch/ElasticSearchApplicationTests.java b/elastic-search/src/test/java/com/elasticsearch/elasticsearch/ElasticSearchApplicationTests.java new file mode 100644 index 00000000..25dfeec8 --- /dev/null +++ b/elastic-search/src/test/java/com/elasticsearch/elasticsearch/ElasticSearchApplicationTests.java @@ -0,0 +1,13 @@ +package com.elasticsearch.elasticsearch; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ElasticSearchApplicationTests { + + @Test + void contextLoads() { + } + +} From 2c003fce75652742ecc890b68e422ec6071c44fa Mon Sep 17 00:00:00 2001 From: Anshuman Shukla Date: Mon, 16 Oct 2023 14:35:25 +0530 Subject: [PATCH 4/8] Added listener of event Hub --- .../controller/CarEntityController.java | 7 ++-- .../eventlistener/AzureKafkaProducer.java | 2 - .../eventlistener/CloudConsumer.java | 6 +++ .../{ => impl}/AzureConsumer.java | 7 ++-- .../src/main/resources/application.yaml | 37 +++++++++---------- 5 files changed, 30 insertions(+), 29 deletions(-) create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/CloudConsumer.java rename elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/{ => impl}/AzureConsumer.java (81%) diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java index 2d8870d1..0e09fee0 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java @@ -1,6 +1,7 @@ package com.elasticsearch.elasticsearch.controller; import com.elasticsearch.elasticsearch.entity.CarEntity; +import com.elasticsearch.elasticsearch.eventlistener.AzureKafkaProducer; import com.elasticsearch.elasticsearch.service.CarService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -15,8 +16,8 @@ public class CarEntityController { @Autowired private CarService service; - /*@Autowired - private AzureKafkaProducer producer;*/ + @Autowired + private AzureKafkaProducer producer; @GetMapping("/all") public ResponseEntity> getAllCarEntity() { @@ -32,7 +33,7 @@ public ResponseEntity getCarDetailsById(@PathVariable("carId") String @PostMapping("/save") public CarEntity saveCarEntity(@RequestBody CarEntity carEntity) { - // producer.send(carEntity); + producer.send(carEntity); return service.saveCarEntity(carEntity); } } diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureKafkaProducer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureKafkaProducer.java index df7b9848..42583c70 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureKafkaProducer.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureKafkaProducer.java @@ -1,4 +1,3 @@ -/* package com.elasticsearch.elasticsearch.eventlistener; import com.elasticsearch.elasticsearch.entity.CarEntity; @@ -23,4 +22,3 @@ public void send(CarEntity message){ log.info("Published the message [{}] to the kafka queue: [{}]",message,topicName); } } -*/ diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/CloudConsumer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/CloudConsumer.java new file mode 100644 index 00000000..467d872b --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/CloudConsumer.java @@ -0,0 +1,6 @@ +package com.elasticsearch.elasticsearch.eventlistener; + +public interface CloudConsumer { + + void consumeEvent(String event); +} diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureConsumer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/AzureConsumer.java similarity index 81% rename from elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureConsumer.java rename to elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/AzureConsumer.java index 910d3cf1..6e0b371b 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureConsumer.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/AzureConsumer.java @@ -1,7 +1,7 @@ -/* -package com.elasticsearch.elasticsearch.eventlistener; +package com.elasticsearch.elasticsearch.eventlistener.impl; import com.elasticsearch.elasticsearch.entity.CarEntity; +import com.elasticsearch.elasticsearch.eventlistener.CloudConsumer; import com.elasticsearch.elasticsearch.service.CarService; import com.elasticsearch.elasticsearch.util.CarMapper; import lombok.extern.slf4j.Slf4j; @@ -13,7 +13,7 @@ @Slf4j @Service @Profile("azure") -public class AzureConsumer { +public class AzureConsumer implements CloudConsumer { @Autowired private CarService service; @@ -25,4 +25,3 @@ public void consumeEvent(String event) { log.info(carEntity.toString()); } } -*/ diff --git a/elastic-search/src/main/resources/application.yaml b/elastic-search/src/main/resources/application.yaml index ec8c9363..4d458e9a 100644 --- a/elastic-search/src/main/resources/application.yaml +++ b/elastic-search/src/main/resources/application.yaml @@ -2,26 +2,23 @@ spring: profiles: active: - azure - - -#spring: -# kafka: -# bootstrap-servers: event-hub-elastic.servicebus.windows.net:9093 -# client-id: elastic-search -# properties: -# sasl.jaas.config: org.apache.kafka.common.security.plain.PlainLoginModule required username="$ConnectionString" password="Endpoint=sb://event-hub-elastic.servicebus.windows.net/;SharedAccessKeyName=sendandrecieve;SharedAccessKey=w8+oeEd7hWLHAVtB9PWfvYyfXoZ0bx1z4+AEhDsw0Gs="; -# sasl.mechanism: PLAIN -# security.protocol: SASL_SSL -# producer: -# value-serializer: com.elasticsearch.elasticsearch.util.CarEntitySerializer -# consumer: -# group-id: $Default -# properties: -# spring.json: -# use.type.headers: true -# value.default.type: com.elasticsearch.elasticsearch.entity.AzureCarEntity -#topic: -# producer: car-event-hub + kafka: + bootstrap-servers: ntdemoevtnamespace.servicebus.windows.net:9093 + client-id: elastic-search + properties: + sasl.jaas.config: org.apache.kafka.common.security.plain.PlainLoginModule required username="$ConnectionString" password="Endpoint=sb://ntdemoevtnamespace.servicebus.windows.net/;SharedAccessKeyName=nashtech-elastic;SharedAccessKey=tOmMza3/36snbideCnUFuX0zm5wcPhiKZ+AEhJTd6vU=;EntityPath=ntdemoeventhub"; + sasl.mechanism: PLAIN + security.protocol: SASL_SSL + producer: + value-serializer: com.elasticsearch.elasticsearch.util.CarEntitySerializer + consumer: + group-id: $Default + properties: + spring.json: + use.type.headers: true + value.default.type: com.elasticsearch.elasticsearch.entity.CarEntity +topic: + producer: ntdemoeventhub azure: elastic: From 53409a621b8a2570338cbe8e2efdcbdf2c78d27a Mon Sep 17 00:00:00 2001 From: Vaibhavp Date: Mon, 16 Oct 2023 19:16:07 +0530 Subject: [PATCH 5/8] Gcp Implementation. --- elastic-search/pom.xml | 20 +++++ ...ticAzureConfig.java => ElasticConfig.java} | 14 +--- .../elasticsearch/config/PubSubConfig.java | 52 ++++++++++++ .../controller/CarEntityController.java | 33 ++++++-- .../eventlistener/AzzureCloudConsumer.java | 9 +++ .../eventlistener/CloudConsumer.java | 6 -- .../eventlistener/GcpCloudConsumer.java | 26 ++++++ .../eventlistener/impl/AzureConsumer.java | 4 +- .../eventlistener/impl/GcpConsumer.java | 81 +++++++++++++++++++ .../repository/CarEntityRepository.java | 13 +-- .../elasticsearch/service/CarService.java | 8 +- ...ureCarService.java => CarServiceImpl.java} | 27 ++++--- ...pplication.yaml => application-azure.yaml} | 11 +-- .../src/main/resources/application-gcp.yaml | 17 ++++ .../src/main/resources/application.yml | 5 ++ 15 files changed, 278 insertions(+), 48 deletions(-) rename elastic-search/src/main/java/com/elasticsearch/elasticsearch/config/{ElasticAzureConfig.java => ElasticConfig.java} (77%) create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/config/PubSubConfig.java create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzzureCloudConsumer.java delete mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/CloudConsumer.java create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/GcpCloudConsumer.java create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/GcpConsumer.java rename elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/impl/{AzureCarService.java => CarServiceImpl.java} (64%) rename elastic-search/src/main/resources/{application.yaml => application-azure.yaml} (88%) create mode 100644 elastic-search/src/main/resources/application-gcp.yaml create mode 100644 elastic-search/src/main/resources/application.yml diff --git a/elastic-search/pom.xml b/elastic-search/pom.xml index a308f8cc..a6f2351d 100644 --- a/elastic-search/pom.xml +++ b/elastic-search/pom.xml @@ -74,6 +74,26 @@ spring-boot-starter-test test + + org.springframework.cloud + spring-cloud-gcp-pubsub + 1.2.8.RELEASE + + + com.google.cloud + spring-cloud-gcp-starter + 4.8.0 + + + org.springframework.cloud + spring-cloud-gcp-starter-pubsub + 1.2.8.RELEASE + + + com.google.cloud + google-cloud-pubsub + 1.123.17 + diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/config/ElasticAzureConfig.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/config/ElasticConfig.java similarity index 77% rename from elastic-search/src/main/java/com/elasticsearch/elasticsearch/config/ElasticAzureConfig.java rename to elastic-search/src/main/java/com/elasticsearch/elasticsearch/config/ElasticConfig.java index 8494bbc0..2efc3b45 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/config/ElasticAzureConfig.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/config/ElasticConfig.java @@ -1,30 +1,25 @@ package com.elasticsearch.elasticsearch.config; -import co.elastic.clients.json.jackson.JacksonJsonpMapper; -import co.elastic.clients.transport.ElasticsearchTransport; -import co.elastic.clients.transport.rest_client.RestClientTransport; import co.elastic.clients.util.ContentType; import org.apache.http.HttpHeaders; import org.apache.http.HttpHost; import org.apache.http.HttpResponseInterceptor; import org.apache.http.message.BasicHeader; - import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Profile; import java.util.List; -@Profile("azure") @Configuration -public class ElasticAzureConfig { - @Value("${azure.elastic.hostname}") +public class ElasticConfig { + + @Value("${elastic.hostname}") private String hostName; - @Value("${azure.elastic.port}") + @Value("${elastic.port}") private int port; @Bean @@ -36,5 +31,4 @@ public RestClient getResClient() { response.addHeader("X-Elastic-Product", "Elasticsearch")); return RestClient.builder(new HttpHost(hostName, port)).setHttpClientConfigCallback(httpClientConfigCallback).build(); } - } diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/config/PubSubConfig.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/config/PubSubConfig.java new file mode 100644 index 00000000..41bbf58f --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/config/PubSubConfig.java @@ -0,0 +1,52 @@ +package com.elasticsearch.elasticsearch.config; + +import com.google.api.gax.core.CredentialsProvider; +import com.google.auth.oauth2.ServiceAccountCredentials; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.gcp.core.DefaultGcpProjectIdProvider; +import org.springframework.cloud.gcp.core.GcpProjectIdProvider; +import org.springframework.cloud.gcp.pubsub.core.PubSubTemplate; +import org.springframework.cloud.gcp.pubsub.support.DefaultPublisherFactory; +import org.springframework.cloud.gcp.pubsub.support.DefaultSubscriberFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.core.io.ClassPathResource; + +@Configuration +@Profile("gcp") +public class PubSubConfig { + + @Value("${pubSub.projectId}") + private String projectId; + + @Value("${pubSub.credentials}") + private String credentialsPath; + + @Value("${pubSub.topic}") + private String topicName; + + @Bean + public PubSubTemplate pubSubTemplate() { + CredentialsProvider credentialsProvider = () -> ServiceAccountCredentials.fromStream( + new ClassPathResource(credentialsPath).getInputStream()); + + GcpProjectIdProvider gcpProjectIdProvider = new DefaultGcpProjectIdProvider() { + @Override + public String getProjectId() { + return projectId; + } + }; + + DefaultSubscriberFactory subscriberFactory = + new DefaultSubscriberFactory(gcpProjectIdProvider); + subscriberFactory.setCredentialsProvider(credentialsProvider); + + DefaultPublisherFactory publisherFactory = + new DefaultPublisherFactory(gcpProjectIdProvider); + publisherFactory.setCredentialsProvider(credentialsProvider); + + return new PubSubTemplate(publisherFactory, subscriberFactory); + } + +} \ No newline at end of file diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java index 0e09fee0..0a0c95f0 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java @@ -1,39 +1,56 @@ package com.elasticsearch.elasticsearch.controller; import com.elasticsearch.elasticsearch.entity.CarEntity; -import com.elasticsearch.elasticsearch.eventlistener.AzureKafkaProducer; import com.elasticsearch.elasticsearch.service.CarService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.io.IOException; import java.util.List; @RestController @RequestMapping("/apis/car") public class CarEntityController { + @Autowired private CarService service; - @Autowired - private AzureKafkaProducer producer; + +// @Autowired +// private AzureKafkaProducer producer; @GetMapping("/all") public ResponseEntity> getAllCarEntity() { - return new ResponseEntity<>(service.getAllCarEntity(), HttpStatus.OK); } - @GetMapping("/{carId}") - public ResponseEntity getCarDetailsById(@PathVariable("carId") String carId) throws IOException { + @GetMapping("/byId/{carId}") + public ResponseEntity getCarDetailsById(@PathVariable("carId") String carId) { CarEntity carEntityWithCarId = service.getCarEntityWithCarId(Integer.valueOf(carId)); return new ResponseEntity<>(carEntityWithCarId, HttpStatus.OK); } + @GetMapping("/byMileage/{mileage}") + public ResponseEntity getCarDetailsByMileage(@PathVariable("mileage") String mileage) { + CarEntity carEntityWithCarMileage = service.getCarEntityWithCarMileage(Double.valueOf(mileage)); + return new ResponseEntity<>(carEntityWithCarMileage, HttpStatus.OK); + } + + @GetMapping("/byBrand/{brand}") + public ResponseEntity getCarDetailsByBrand(@PathVariable("brand") String brand) { + CarEntity carEntityWithCarBrand = service.getCarEntityWithBrandName(brand); + return new ResponseEntity<>(carEntityWithCarBrand, HttpStatus.OK); + } + + @GetMapping("/byPrice/{price}") + public ResponseEntity getCarDetailsByPrice(@PathVariable("price") String price) { + CarEntity carEntityWithCarPrice = service.getCarEntityWithCarPrice(Double.valueOf(price)); + return new ResponseEntity<>(carEntityWithCarPrice, HttpStatus.OK); + } + @PostMapping("/save") public CarEntity saveCarEntity(@RequestBody CarEntity carEntity) { - producer.send(carEntity); + // producer.send(carEntity); return service.saveCarEntity(carEntity); } } diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzzureCloudConsumer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzzureCloudConsumer.java new file mode 100644 index 00000000..e4d7bb3b --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzzureCloudConsumer.java @@ -0,0 +1,9 @@ +package com.elasticsearch.elasticsearch.eventlistener; + +import org.springframework.context.annotation.Profile; + +@Profile("azure") +public interface AzzureCloudConsumer { + + void consumeEvent(String event); +} diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/CloudConsumer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/CloudConsumer.java deleted file mode 100644 index 467d872b..00000000 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/CloudConsumer.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.elasticsearch.elasticsearch.eventlistener; - -public interface CloudConsumer { - - void consumeEvent(String event); -} diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/GcpCloudConsumer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/GcpCloudConsumer.java new file mode 100644 index 00000000..86c9423e --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/GcpCloudConsumer.java @@ -0,0 +1,26 @@ +package com.elasticsearch.elasticsearch.eventlistener; + +import com.google.cloud.pubsub.v1.Subscriber; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.gcp.pubsub.core.PubSubTemplate; +import org.springframework.cloud.gcp.pubsub.support.BasicAcknowledgeablePubsubMessage; +import org.springframework.context.annotation.Profile; + +@Profile("gcp") +public abstract class GcpCloudConsumer { + + @Autowired + private PubSubTemplate pubSubTemplate; + + public abstract String subscription(); + + protected abstract void consume(BasicAcknowledgeablePubsubMessage message); + + public java.util.function.Consumer messageConsumer() { + return this::consume; + } + + public Subscriber consumeMessage() { + return this.pubSubTemplate.subscribe(this.subscription(), this.messageConsumer()); + } +} \ No newline at end of file diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/AzureConsumer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/AzureConsumer.java index 6e0b371b..9c9d8f64 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/AzureConsumer.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/AzureConsumer.java @@ -1,7 +1,7 @@ package com.elasticsearch.elasticsearch.eventlistener.impl; import com.elasticsearch.elasticsearch.entity.CarEntity; -import com.elasticsearch.elasticsearch.eventlistener.CloudConsumer; +import com.elasticsearch.elasticsearch.eventlistener.AzzureCloudConsumer; import com.elasticsearch.elasticsearch.service.CarService; import com.elasticsearch.elasticsearch.util.CarMapper; import lombok.extern.slf4j.Slf4j; @@ -13,7 +13,7 @@ @Slf4j @Service @Profile("azure") -public class AzureConsumer implements CloudConsumer { +public class AzureConsumer implements AzzureCloudConsumer { @Autowired private CarService service; diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/GcpConsumer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/GcpConsumer.java new file mode 100644 index 00000000..6da223fb --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/GcpConsumer.java @@ -0,0 +1,81 @@ +package com.elasticsearch.elasticsearch.eventlistener.impl; + +import com.elasticsearch.elasticsearch.entity.CarEntity; +import com.elasticsearch.elasticsearch.eventlistener.GcpCloudConsumer; +import com.elasticsearch.elasticsearch.service.CarService; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.pubsub.v1.PubsubMessage; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.cloud.gcp.pubsub.core.PubSubTemplate; +import org.springframework.cloud.gcp.pubsub.support.BasicAcknowledgeablePubsubMessage; +import org.springframework.context.annotation.Profile; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import java.util.List; + +@Slf4j +@Component +@Profile("gcp") +public class GcpConsumer extends GcpCloudConsumer { + + @Value("${pubSub.subscriptionId}") + private String subscription; + + @Autowired + private PubSubTemplate pubSubTemplate; + + @Autowired + private CarService service; + + private final ObjectMapper objectMapper; + + public GcpConsumer(ObjectMapper objectMapper) { + this.objectMapper = objectMapper; + } + + @Override + public String subscription() { + return this.subscription; + } + + @Override + protected void consume(BasicAcknowledgeablePubsubMessage basicAcknowledgeablePubsubMessage) { + PubsubMessage message = basicAcknowledgeablePubsubMessage.getPubsubMessage(); + log.info("Message received from {}", basicAcknowledgeablePubsubMessage.getProjectSubscriptionName()); + try { + log.info("Message: {}", message.getData().toStringUtf8()); + processMessage(basicAcknowledgeablePubsubMessage); + } catch (Exception ex) { + log.error("Error Occurred while receiving pubsub message:::::", ex); + } + basicAcknowledgeablePubsubMessage.ack(); + } + + @EventListener(ApplicationReadyEvent.class) + public void subscribe() { + log.info("Subscribing {} to {} ", this.getClass().getSimpleName(), this.subscription()); + pubSubTemplate.subscribe(this.subscription(), this.messageConsumer()); + } + + private void processMessage(BasicAcknowledgeablePubsubMessage message) { + CarEntity[] gcHubMessageArray; + String eventMsgString = ""; + try { + eventMsgString = message.getPubsubMessage().getData().toStringUtf8(); + gcHubMessageArray = objectMapper.readValue(eventMsgString, CarEntity[].class); + CarEntity[] gcpElasticCarEntityList = gcHubMessageArray; + for (CarEntity carEntity : gcpElasticCarEntityList) { + service.saveCarEntity(carEntity); + } + } catch (IllegalArgumentException | JsonProcessingException e) { + log.error("Json exception in parsing message: {}", eventMsgString, e); + message.ack(); + } + } +} \ No newline at end of file diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/repository/CarEntityRepository.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/repository/CarEntityRepository.java index e60cee99..f6c6e4c7 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/repository/CarEntityRepository.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/repository/CarEntityRepository.java @@ -1,15 +1,18 @@ package com.elasticsearch.elasticsearch.repository; import com.elasticsearch.elasticsearch.entity.CarEntity; -import org.springframework.data.elasticsearch.annotations.Query; -import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; @Repository -public interface CarEntityRepository extends ElasticsearchRepository { +public interface CarEntityRepository extends ElasticsearchRepository { + CarEntity findByCarId(Integer carId); - @Query(value = "?0") - SearchHits searchByCustomQuery(String query); + + CarEntity findByBrand(String brand); + + CarEntity findByMileage(Double mileage); + + CarEntity findByPrice(Double price); } diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/CarService.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/CarService.java index 3aa73a13..a63f5d9a 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/CarService.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/CarService.java @@ -4,12 +4,18 @@ import java.util.List; - public interface CarService { public CarEntity getCarEntityWithCarId(Integer carId); + public CarEntity getCarEntityWithBrandName(String brand); + + public CarEntity getCarEntityWithCarPrice(Double price); + + public CarEntity getCarEntityWithCarMileage(Double mileage); + public List getAllCarEntity(); public CarEntity saveCarEntity(CarEntity entity); + } diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/impl/AzureCarService.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/impl/CarServiceImpl.java similarity index 64% rename from elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/impl/AzureCarService.java rename to elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/impl/CarServiceImpl.java index 620e578e..e92b2edb 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/impl/AzureCarService.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/impl/CarServiceImpl.java @@ -4,9 +4,6 @@ import com.elasticsearch.elasticsearch.repository.CarEntityRepository; import com.elasticsearch.elasticsearch.service.CarService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Profile; -import org.springframework.data.elasticsearch.core.SearchHit; -import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.stereotype.Service; import java.util.List; @@ -14,15 +11,29 @@ import java.util.stream.StreamSupport; @Service -@Profile("azure") -public class AzureCarService implements CarService { +public class CarServiceImpl implements CarService { + @Autowired private CarEntityRepository repository; @Override public CarEntity getCarEntityWithCarId(Integer carId) { - CarEntity byCarId = repository.findByCarId(carId); - return byCarId; + return repository.findByCarId(carId); + } + + @Override + public CarEntity getCarEntityWithBrandName(String brand) { + return repository.findByBrand(brand); + } + + @Override + public CarEntity getCarEntityWithCarPrice(Double price) { + return repository.findByPrice(price); + } + + @Override + public CarEntity getCarEntityWithCarMileage(Double mileage) { + return repository.findByMileage(mileage); } @Override @@ -32,9 +43,7 @@ public List getAllCarEntity() { @Override public CarEntity saveCarEntity(CarEntity entity) { - return repository.save(entity); } - } diff --git a/elastic-search/src/main/resources/application.yaml b/elastic-search/src/main/resources/application-azure.yaml similarity index 88% rename from elastic-search/src/main/resources/application.yaml rename to elastic-search/src/main/resources/application-azure.yaml index 4d458e9a..424bcabb 100644 --- a/elastic-search/src/main/resources/application.yaml +++ b/elastic-search/src/main/resources/application-azure.yaml @@ -1,7 +1,4 @@ spring: - profiles: - active: - - azure kafka: bootstrap-servers: ntdemoevtnamespace.servicebus.windows.net:9093 client-id: elastic-search @@ -20,10 +17,10 @@ spring: topic: producer: ntdemoeventhub -azure: - elastic: - hostname: ${HOSTNAME:localhost} - port: ${PORT:9200} + +elastic: + hostname: ${HOSTNAME:localhost} + port: ${PORT:9200} # GCP IP: http://35.201.207.148:9200/ # AZure IP: http://20.204.229.0:9200/ diff --git a/elastic-search/src/main/resources/application-gcp.yaml b/elastic-search/src/main/resources/application-gcp.yaml new file mode 100644 index 00000000..d901331d --- /dev/null +++ b/elastic-search/src/main/resources/application-gcp.yaml @@ -0,0 +1,17 @@ +pubSub: + topic: java-competency + subscriptionId: java-competency-sub + projectId: oval-crawler-398007 + credentials: oval-crawler-398007-406ca3b3a9ba.json + +elastic: + hostname: ${HOSTNAME:35.201.207.148} + port: ${PORT:9200} + + + + + + + + diff --git a/elastic-search/src/main/resources/application.yml b/elastic-search/src/main/resources/application.yml new file mode 100644 index 00000000..ae8be897 --- /dev/null +++ b/elastic-search/src/main/resources/application.yml @@ -0,0 +1,5 @@ +spring: + profiles: + active: + - gcp + From 49c155d7edeb9a61aabe62b5224b1200fa758287 Mon Sep 17 00:00:00 2001 From: Vaibhavp Date: Tue, 17 Oct 2023 17:56:22 +0530 Subject: [PATCH 6/8] Worked on review comments. --- .../controller/CarEntityController.java | 14 +++------- .../eventlistener/AzureKafkaProducer.java | 24 ----------------- .../eventlistener/AzzureCloudConsumer.java | 9 ------- .../eventlistener/CloudConsumer.java | 7 +++++ .../eventlistener/GcpCloudConsumer.java | 26 ------------------ .../eventlistener/impl/AzureConsumer.java | 4 +-- .../eventlistener/impl/GcpConsumer.java | 27 ++++++++++--------- .../src/main/resources/application.yml | 2 +- 8 files changed, 28 insertions(+), 85 deletions(-) delete mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureKafkaProducer.java delete mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzzureCloudConsumer.java create mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/CloudConsumer.java delete mode 100644 elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/GcpCloudConsumer.java diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java index 0a0c95f0..622577c4 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java @@ -5,7 +5,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import java.util.List; @@ -16,9 +19,6 @@ public class CarEntityController { @Autowired private CarService service; -// @Autowired -// private AzureKafkaProducer producer; - @GetMapping("/all") public ResponseEntity> getAllCarEntity() { return new ResponseEntity<>(service.getAllCarEntity(), HttpStatus.OK); @@ -47,10 +47,4 @@ public ResponseEntity getCarDetailsByPrice(@PathVariable("price") Str CarEntity carEntityWithCarPrice = service.getCarEntityWithCarPrice(Double.valueOf(price)); return new ResponseEntity<>(carEntityWithCarPrice, HttpStatus.OK); } - - @PostMapping("/save") - public CarEntity saveCarEntity(@RequestBody CarEntity carEntity) { - // producer.send(carEntity); - return service.saveCarEntity(carEntity); - } } diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureKafkaProducer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureKafkaProducer.java deleted file mode 100644 index 42583c70..00000000 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzureKafkaProducer.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.elasticsearch.elasticsearch.eventlistener; - -import com.elasticsearch.elasticsearch.entity.CarEntity; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Profile; -import org.springframework.kafka.core.KafkaTemplate; -import org.springframework.stereotype.Service; -@Slf4j -@Service -@RequiredArgsConstructor -@Profile("azure") -public class AzureKafkaProducer { - - private final KafkaTemplate kafkaTemplate; - @Value(("${topic.producer}")) - private String topicName; - - public void send(CarEntity message){ - this.kafkaTemplate.send(topicName,message); - log.info("Published the message [{}] to the kafka queue: [{}]",message,topicName); - } -} diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzzureCloudConsumer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzzureCloudConsumer.java deleted file mode 100644 index e4d7bb3b..00000000 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/AzzureCloudConsumer.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.elasticsearch.elasticsearch.eventlistener; - -import org.springframework.context.annotation.Profile; - -@Profile("azure") -public interface AzzureCloudConsumer { - - void consumeEvent(String event); -} diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/CloudConsumer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/CloudConsumer.java new file mode 100644 index 00000000..7fb0b2d7 --- /dev/null +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/CloudConsumer.java @@ -0,0 +1,7 @@ +package com.elasticsearch.elasticsearch.eventlistener; + +public interface CloudConsumer { + + void consumeEvent(T event); + +} diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/GcpCloudConsumer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/GcpCloudConsumer.java deleted file mode 100644 index 86c9423e..00000000 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/GcpCloudConsumer.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.elasticsearch.elasticsearch.eventlistener; - -import com.google.cloud.pubsub.v1.Subscriber; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cloud.gcp.pubsub.core.PubSubTemplate; -import org.springframework.cloud.gcp.pubsub.support.BasicAcknowledgeablePubsubMessage; -import org.springframework.context.annotation.Profile; - -@Profile("gcp") -public abstract class GcpCloudConsumer { - - @Autowired - private PubSubTemplate pubSubTemplate; - - public abstract String subscription(); - - protected abstract void consume(BasicAcknowledgeablePubsubMessage message); - - public java.util.function.Consumer messageConsumer() { - return this::consume; - } - - public Subscriber consumeMessage() { - return this.pubSubTemplate.subscribe(this.subscription(), this.messageConsumer()); - } -} \ No newline at end of file diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/AzureConsumer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/AzureConsumer.java index 9c9d8f64..20279a54 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/AzureConsumer.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/AzureConsumer.java @@ -1,7 +1,7 @@ package com.elasticsearch.elasticsearch.eventlistener.impl; import com.elasticsearch.elasticsearch.entity.CarEntity; -import com.elasticsearch.elasticsearch.eventlistener.AzzureCloudConsumer; +import com.elasticsearch.elasticsearch.eventlistener.CloudConsumer; import com.elasticsearch.elasticsearch.service.CarService; import com.elasticsearch.elasticsearch.util.CarMapper; import lombok.extern.slf4j.Slf4j; @@ -13,7 +13,7 @@ @Slf4j @Service @Profile("azure") -public class AzureConsumer implements AzzureCloudConsumer { +public class AzureConsumer implements CloudConsumer { @Autowired private CarService service; diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/GcpConsumer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/GcpConsumer.java index 6da223fb..9864236a 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/GcpConsumer.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/GcpConsumer.java @@ -1,7 +1,7 @@ package com.elasticsearch.elasticsearch.eventlistener.impl; import com.elasticsearch.elasticsearch.entity.CarEntity; -import com.elasticsearch.elasticsearch.eventlistener.GcpCloudConsumer; +import com.elasticsearch.elasticsearch.eventlistener.CloudConsumer; import com.elasticsearch.elasticsearch.service.CarService; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -16,13 +16,10 @@ import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; -import java.util.Arrays; -import java.util.List; - @Slf4j @Component @Profile("gcp") -public class GcpConsumer extends GcpCloudConsumer { +public class GcpConsumer implements CloudConsumer { @Value("${pubSub.subscriptionId}") private String subscription; @@ -39,13 +36,23 @@ public GcpConsumer(ObjectMapper objectMapper) { this.objectMapper = objectMapper; } - @Override + public String subscription() { return this.subscription; } + public java.util.function.Consumer messageConsumer() { + return this::consumeEvent; + } + + @EventListener(ApplicationReadyEvent.class) + public void subscribe() { + log.info("Subscribing {} to {} ", this.getClass().getSimpleName(), this.subscription()); + pubSubTemplate.subscribe(this.subscription(), this.messageConsumer()); + } + @Override - protected void consume(BasicAcknowledgeablePubsubMessage basicAcknowledgeablePubsubMessage) { + public void consumeEvent(BasicAcknowledgeablePubsubMessage basicAcknowledgeablePubsubMessage) { PubsubMessage message = basicAcknowledgeablePubsubMessage.getPubsubMessage(); log.info("Message received from {}", basicAcknowledgeablePubsubMessage.getProjectSubscriptionName()); try { @@ -57,12 +64,6 @@ protected void consume(BasicAcknowledgeablePubsubMessage basicAcknowledgeablePub basicAcknowledgeablePubsubMessage.ack(); } - @EventListener(ApplicationReadyEvent.class) - public void subscribe() { - log.info("Subscribing {} to {} ", this.getClass().getSimpleName(), this.subscription()); - pubSubTemplate.subscribe(this.subscription(), this.messageConsumer()); - } - private void processMessage(BasicAcknowledgeablePubsubMessage message) { CarEntity[] gcHubMessageArray; String eventMsgString = ""; diff --git a/elastic-search/src/main/resources/application.yml b/elastic-search/src/main/resources/application.yml index ae8be897..f975fd11 100644 --- a/elastic-search/src/main/resources/application.yml +++ b/elastic-search/src/main/resources/application.yml @@ -1,5 +1,5 @@ spring: profiles: active: - - gcp + - azure From 52a00b0ffee5e43e7977ccfbd8d7c6a233ce6024 Mon Sep 17 00:00:00 2001 From: Vaibhavp Date: Tue, 17 Oct 2023 18:16:25 +0530 Subject: [PATCH 7/8] Added check while saving inorder to avoid duplicate entries. --- .../eventlistener/impl/GcpConsumer.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/GcpConsumer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/GcpConsumer.java index 9864236a..bde96f67 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/GcpConsumer.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/GcpConsumer.java @@ -2,6 +2,7 @@ import com.elasticsearch.elasticsearch.entity.CarEntity; import com.elasticsearch.elasticsearch.eventlistener.CloudConsumer; +import com.elasticsearch.elasticsearch.repository.CarEntityRepository; import com.elasticsearch.elasticsearch.service.CarService; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -27,6 +28,9 @@ public class GcpConsumer implements CloudConsumer Date: Sat, 28 Oct 2023 13:30:38 +0530 Subject: [PATCH 8/8] Added return type as list for mileage ,brand and price --- elastic-search/pom.xml | 22 +++++-------------- .../controller/CarEntityController.java | 12 +++++----- .../elasticsearch/entity/CarEntity.java | 10 +++++++-- .../eventlistener/impl/AzureConsumer.java | 14 ++++++++---- .../repository/CarEntityRepository.java | 8 ++++--- .../elasticsearch/service/CarService.java | 6 ++--- .../service/impl/CarServiceImpl.java | 10 ++++----- .../elasticsearch/util/CarMapper.java | 2 ++ .../src/main/resources/application-azure.yaml | 4 ++-- .../src/main/resources/application.yml | 5 +++++ 10 files changed, 52 insertions(+), 41 deletions(-) diff --git a/elastic-search/pom.xml b/elastic-search/pom.xml index a6f2351d..e591bbe9 100644 --- a/elastic-search/pom.xml +++ b/elastic-search/pom.xml @@ -24,6 +24,10 @@ elasticsearch snapshot repo https://snapshots.elastic.co/maven/ + + central + https://repo.maven.apache.org/maven2 + @@ -39,22 +43,7 @@ org.springframework.boot spring-boot-starter-web - - - - - - - - - - - - - - - - + org.springframework.kafka spring-kafka @@ -94,6 +83,7 @@ google-cloud-pubsub 1.123.17 + diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java index 622577c4..4bbca2da 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/controller/CarEntityController.java @@ -31,20 +31,20 @@ public ResponseEntity getCarDetailsById(@PathVariable("carId") String } @GetMapping("/byMileage/{mileage}") - public ResponseEntity getCarDetailsByMileage(@PathVariable("mileage") String mileage) { - CarEntity carEntityWithCarMileage = service.getCarEntityWithCarMileage(Double.valueOf(mileage)); + public ResponseEntity> getCarDetailsByMileage(@PathVariable("mileage") String mileage) { + List carEntityWithCarMileage = service.getCarEntityWithCarMileage(Double.valueOf(mileage)); return new ResponseEntity<>(carEntityWithCarMileage, HttpStatus.OK); } @GetMapping("/byBrand/{brand}") - public ResponseEntity getCarDetailsByBrand(@PathVariable("brand") String brand) { - CarEntity carEntityWithCarBrand = service.getCarEntityWithBrandName(brand); + public ResponseEntity> getCarDetailsByBrand(@PathVariable("brand") String brand) { + List carEntityWithCarBrand = service.getCarEntityWithBrandName(brand); return new ResponseEntity<>(carEntityWithCarBrand, HttpStatus.OK); } @GetMapping("/byPrice/{price}") - public ResponseEntity getCarDetailsByPrice(@PathVariable("price") String price) { - CarEntity carEntityWithCarPrice = service.getCarEntityWithCarPrice(Double.valueOf(price)); + public ResponseEntity> getCarDetailsByPrice(@PathVariable("price") String price) { + List carEntityWithCarPrice = service.getCarEntityWithCarPrice(Double.valueOf(price)); return new ResponseEntity<>(carEntityWithCarPrice, HttpStatus.OK); } } diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/entity/CarEntity.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/entity/CarEntity.java index a7ed0d74..843719a3 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/entity/CarEntity.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/entity/CarEntity.java @@ -28,9 +28,15 @@ public class CarEntity { @Field(type = FieldType.Text, name = "color") private String color; - @Field(type = FieldType.Double, name = "name") + @Field(type = FieldType.Double, name = "mileage") private Double mileage; - @Field(type = FieldType.Double, name = "name") + @Field(type = FieldType.Double, name = "price") private Double price; + + @Field(type = FieldType.Integer, name = "quantity") + private Integer quantity; + + @Field(type = FieldType.Double, name = "tax") + private Double tax; } \ No newline at end of file diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/AzureConsumer.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/AzureConsumer.java index 20279a54..a05fc085 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/AzureConsumer.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/eventlistener/impl/AzureConsumer.java @@ -5,6 +5,7 @@ import com.elasticsearch.elasticsearch.service.CarService; import com.elasticsearch.elasticsearch.util.CarMapper; import lombok.extern.slf4j.Slf4j; +import org.apache.kafka.clients.consumer.ConsumerRecord; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; import org.springframework.kafka.annotation.KafkaListener; @@ -18,10 +19,15 @@ public class AzureConsumer implements CloudConsumer { private CarService service; @KafkaListener(topics = "${topic.producer}") + public void consumeEvents(ConsumerRecord event) { + + log.info("Received message from kafka queue: {}", event.value()); + CarEntity carEntity = CarMapper.mapStringToEntity(event.value().toString()); + service.saveCarEntity(carEntity); + log.info(carEntity.toString()); + } public void consumeEvent(String event) { - log.info("Received message from kafka queue: {}", event); - CarEntity carEntity = CarMapper.mapStringToEntity(event); - service.saveCarEntity(carEntity); - log.info(carEntity.toString()); + log.info("Received message from kafka queue: {}", event.toString()); + } } diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/repository/CarEntityRepository.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/repository/CarEntityRepository.java index f6c6e4c7..2c06dbd2 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/repository/CarEntityRepository.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/repository/CarEntityRepository.java @@ -4,15 +4,17 @@ import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface CarEntityRepository extends ElasticsearchRepository { CarEntity findByCarId(Integer carId); - CarEntity findByBrand(String brand); + List findByBrand(String brand); - CarEntity findByMileage(Double mileage); + List findByMileageGreaterThanEqual(Double mileage); - CarEntity findByPrice(Double price); + List findByPriceGreaterThanEqual(Double price); } diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/CarService.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/CarService.java index a63f5d9a..a32c3771 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/CarService.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/CarService.java @@ -8,11 +8,11 @@ public interface CarService { public CarEntity getCarEntityWithCarId(Integer carId); - public CarEntity getCarEntityWithBrandName(String brand); + public List getCarEntityWithBrandName(String brand); - public CarEntity getCarEntityWithCarPrice(Double price); + public List getCarEntityWithCarPrice(Double price); - public CarEntity getCarEntityWithCarMileage(Double mileage); + public List getCarEntityWithCarMileage(Double mileage); public List getAllCarEntity(); diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/impl/CarServiceImpl.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/impl/CarServiceImpl.java index e92b2edb..121719df 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/impl/CarServiceImpl.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/service/impl/CarServiceImpl.java @@ -22,18 +22,18 @@ public CarEntity getCarEntityWithCarId(Integer carId) { } @Override - public CarEntity getCarEntityWithBrandName(String brand) { + public List getCarEntityWithBrandName(String brand) { return repository.findByBrand(brand); } @Override - public CarEntity getCarEntityWithCarPrice(Double price) { - return repository.findByPrice(price); + public List getCarEntityWithCarPrice(Double price) { + return repository.findByPriceGreaterThanEqual(price); } @Override - public CarEntity getCarEntityWithCarMileage(Double mileage) { - return repository.findByMileage(mileage); + public List getCarEntityWithCarMileage(Double mileage) { + return repository.findByMileageGreaterThanEqual(mileage); } @Override diff --git a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/util/CarMapper.java b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/util/CarMapper.java index 7dc0312e..94fed49d 100644 --- a/elastic-search/src/main/java/com/elasticsearch/elasticsearch/util/CarMapper.java +++ b/elastic-search/src/main/java/com/elasticsearch/elasticsearch/util/CarMapper.java @@ -27,6 +27,8 @@ public static CarEntity mapStringToEntity(String payload) { carEntity.setColor((String) payloadMap.get("color")); carEntity.setMileage((Double) payloadMap.get("mileage")); carEntity.setPrice((Double) payloadMap.get("price")); + carEntity.setQuantity((Integer) payloadMap.get("quantity")); + carEntity.setTax((Double) payloadMap.get("tax")); } catch (JsonProcessingException e) { e.printStackTrace(); } diff --git a/elastic-search/src/main/resources/application-azure.yaml b/elastic-search/src/main/resources/application-azure.yaml index 424bcabb..59584236 100644 --- a/elastic-search/src/main/resources/application-azure.yaml +++ b/elastic-search/src/main/resources/application-azure.yaml @@ -3,7 +3,7 @@ spring: bootstrap-servers: ntdemoevtnamespace.servicebus.windows.net:9093 client-id: elastic-search properties: - sasl.jaas.config: org.apache.kafka.common.security.plain.PlainLoginModule required username="$ConnectionString" password="Endpoint=sb://ntdemoevtnamespace.servicebus.windows.net/;SharedAccessKeyName=nashtech-elastic;SharedAccessKey=tOmMza3/36snbideCnUFuX0zm5wcPhiKZ+AEhJTd6vU=;EntityPath=ntdemoeventhub"; + sasl.jaas.config: org.apache.kafka.common.security.plain.PlainLoginModule required username="$ConnectionString" password="${PRIMARY_CONNECTION}"; sasl.mechanism: PLAIN security.protocol: SASL_SSL producer: @@ -15,7 +15,7 @@ spring: use.type.headers: true value.default.type: com.elasticsearch.elasticsearch.entity.CarEntity topic: - producer: ntdemoeventhub + producer: ${TOPIC} elastic: diff --git a/elastic-search/src/main/resources/application.yml b/elastic-search/src/main/resources/application.yml index f975fd11..0450ec0d 100644 --- a/elastic-search/src/main/resources/application.yml +++ b/elastic-search/src/main/resources/application.yml @@ -2,4 +2,9 @@ spring: profiles: active: - azure + main: + allow-bean-definition-overriding: true +application: + description: Elastic Search + version: 1.0.0