Skip to content

Commit

Permalink
Merge pull request #39 from NashTech-Labs/feature/azure-elastic-event…
Browse files Browse the repository at this point in the history
…Hub-search

Added return type as list for mileage ,brand and price
  • Loading branch information
Anshuman-knoldus authored Nov 6, 2023
2 parents c7fbb12 + 15dd9dc commit 42d57f3
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 41 deletions.
22 changes: 6 additions & 16 deletions elastic-search/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<name>elasticsearch snapshot repo</name>
<url>https://snapshots.elastic.co/maven/</url>
</repository>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
Expand All @@ -39,22 +43,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.microsoft.azure</groupId>-->
<!-- <artifactId>azure-spring-boot-starter</artifactId>-->
<!-- <version>2.3.5</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.azure.spring</groupId>-->
<!-- <artifactId>spring-cloud-azure-stream-binder-eventhubs</artifactId>-->
<!-- <version>4.0.0</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>io.projectreactor</groupId>-->
<!-- <artifactId>reactor-test</artifactId>-->
<!-- <version>3.4.11</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->

<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
Expand Down Expand Up @@ -94,6 +83,7 @@
<artifactId>google-cloud-pubsub</artifactId>
<version>1.123.17</version>
</dependency>

</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ public ResponseEntity<CarEntity> getCarDetailsById(@PathVariable("carId") String
}

@GetMapping("/byMileage/{mileage}")
public ResponseEntity<CarEntity> getCarDetailsByMileage(@PathVariable("mileage") String mileage) {
CarEntity carEntityWithCarMileage = service.getCarEntityWithCarMileage(Double.valueOf(mileage));
public ResponseEntity<List<CarEntity>> getCarDetailsByMileage(@PathVariable("mileage") String mileage) {
List<CarEntity> carEntityWithCarMileage = service.getCarEntityWithCarMileage(Double.valueOf(mileage));
return new ResponseEntity<>(carEntityWithCarMileage, HttpStatus.OK);
}

@GetMapping("/byBrand/{brand}")
public ResponseEntity<CarEntity> getCarDetailsByBrand(@PathVariable("brand") String brand) {
CarEntity carEntityWithCarBrand = service.getCarEntityWithBrandName(brand);
public ResponseEntity<List<CarEntity>> getCarDetailsByBrand(@PathVariable("brand") String brand) {
List<CarEntity> carEntityWithCarBrand = service.getCarEntityWithBrandName(brand);
return new ResponseEntity<>(carEntityWithCarBrand, HttpStatus.OK);
}

@GetMapping("/byPrice/{price}")
public ResponseEntity<CarEntity> getCarDetailsByPrice(@PathVariable("price") String price) {
CarEntity carEntityWithCarPrice = service.getCarEntityWithCarPrice(Double.valueOf(price));
public ResponseEntity<List<CarEntity>> getCarDetailsByPrice(@PathVariable("price") String price) {
List<CarEntity> carEntityWithCarPrice = service.getCarEntityWithCarPrice(Double.valueOf(price));
return new ResponseEntity<>(carEntityWithCarPrice, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,10 +19,15 @@ public class AzureConsumer implements CloudConsumer<String> {
private CarService service;

@KafkaListener(topics = "${topic.producer}")
public void consumeEvents(ConsumerRecord<String,String> 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());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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, String> {


CarEntity findByCarId(Integer carId);

CarEntity findByBrand(String brand);
List<CarEntity> findByBrand(String brand);

CarEntity findByMileage(Double mileage);
List<CarEntity> findByMileageGreaterThanEqual(Double mileage);

CarEntity findByPrice(Double price);
List<CarEntity> findByPriceGreaterThanEqual(Double price);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public interface CarService {

public CarEntity getCarEntityWithCarId(Integer carId);

public CarEntity getCarEntityWithBrandName(String brand);
public List<CarEntity> getCarEntityWithBrandName(String brand);

public CarEntity getCarEntityWithCarPrice(Double price);
public List<CarEntity> getCarEntityWithCarPrice(Double price);

public CarEntity getCarEntityWithCarMileage(Double mileage);
public List<CarEntity> getCarEntityWithCarMileage(Double mileage);

public List<CarEntity> getAllCarEntity();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ public CarEntity getCarEntityWithCarId(Integer carId) {
}

@Override
public CarEntity getCarEntityWithBrandName(String brand) {
public List<CarEntity> getCarEntityWithBrandName(String brand) {
return repository.findByBrand(brand);
}

@Override
public CarEntity getCarEntityWithCarPrice(Double price) {
return repository.findByPrice(price);
public List<CarEntity> getCarEntityWithCarPrice(Double price) {
return repository.findByPriceGreaterThanEqual(price);
}

@Override
public CarEntity getCarEntityWithCarMileage(Double mileage) {
return repository.findByMileage(mileage);
public List<CarEntity> getCarEntityWithCarMileage(Double mileage) {
return repository.findByMileageGreaterThanEqual(mileage);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions elastic-search/src/main/resources/application-azure.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -15,7 +15,7 @@ spring:
use.type.headers: true
value.default.type: com.elasticsearch.elasticsearch.entity.CarEntity
topic:
producer: ntdemoeventhub
producer: ${TOPIC}


elastic:
Expand Down
5 changes: 5 additions & 0 deletions elastic-search/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ spring:
profiles:
active:
- azure
main:
allow-bean-definition-overriding: true
application:
description: Elastic Search
version: 1.0.0

0 comments on commit 42d57f3

Please sign in to comment.