Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
inventory-process-api
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Balasubrahmanyam Gudavalli
inventory-process-api
Commits
52e2752a
Commit
52e2752a
authored
Jun 15, 2023
by
Balasubrahmanyam Gudavalli
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: Spark streaming data into Mongo DB.
parent
59c5745f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
7 deletions
+77
-7
InventoryMapper.java
.../java/com/nisum/inventory/repository/InventoryMapper.java
+20
-0
InventoryService.java
...in/java/com/nisum/inventory/service/InventoryService.java
+57
-7
No files found.
src/main/java/com/nisum/inventory/repository/InventoryMapper.java
0 → 100644
View file @
52e2752a
package
com
.
nisum
.
inventory
.
repository
;
import
com.nisum.inventory.collection.ProcessedInventory
;
import
com.nisum.inventory.dto.RawInventory
;
import
org.mapstruct.Mapper
;
import
org.mapstruct.Mapping
;
import
org.mapstruct.factory.Mappers
;
@Mapper
public
interface
InventoryMapper
{
InventoryMapper
INSTANCE
=
Mappers
.
getMapper
(
InventoryMapper
.
class
);
@Mapping
(
target
=
"id"
,
source
=
"id"
)
ProcessedInventory
dtoToEntity
(
RawInventory
rawInventory
,
String
id
);
}
src/main/java/com/nisum/inventory/service/InventoryService.java
View file @
52e2752a
package
com
.
nisum
.
inventory
.
service
;
package
com
.
nisum
.
inventory
.
service
;
import
com.mongodb.MongoClientSettings
;
import
com.mongodb.client.MongoClient
;
import
com.mongodb.client.MongoClients
;
import
com.mongodb.client.MongoCollection
;
import
com.mongodb.client.MongoDatabase
;
import
com.mongodb.client.result.InsertOneResult
;
import
com.nisum.inventory.collection.ProcessedInventory
;
import
com.nisum.inventory.dto.*
;
import
com.nisum.inventory.dto.*
;
import
com.nisum.inventory.repository.InventoryMapper
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.io.FileUtils
;
import
org.apache.commons.io.FileUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
...
@@ -11,8 +19,12 @@ import org.apache.spark.sql.streaming.StreamingQuery;
...
@@ -11,8 +19,12 @@ import org.apache.spark.sql.streaming.StreamingQuery;
import
org.apache.spark.sql.streaming.StreamingQueryException
;
import
org.apache.spark.sql.streaming.StreamingQueryException
;
import
org.apache.spark.sql.streaming.Trigger
;
import
org.apache.spark.sql.streaming.Trigger
;
import
org.apache.spark.sql.types.*
;
import
org.apache.spark.sql.types.*
;
import
org.bson.codecs.configuration.CodecRegistries
;
import
org.bson.codecs.configuration.CodecRegistry
;
import
org.bson.codecs.pojo.PojoCodecProvider
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.core.io.buffer.DataBufferUtils
;
import
org.springframework.core.io.buffer.DataBufferUtils
;
import
org.springframework.data.mongodb.core.ReactiveMongoOperations
;
import
org.springframework.http.codec.multipart.FilePart
;
import
org.springframework.http.codec.multipart.FilePart
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Flux
;
...
@@ -36,6 +48,7 @@ public class InventoryService implements Serializable {
...
@@ -36,6 +48,7 @@ public class InventoryService implements Serializable {
@Autowired
@Autowired
private
StructType
schema
;
private
StructType
schema
;
public
Flux
<
KafkaPublisherResponse
>
saveAndProcess
(
Flux
<
FilePart
>
filePartFlux
)
{
public
Flux
<
KafkaPublisherResponse
>
saveAndProcess
(
Flux
<
FilePart
>
filePartFlux
)
{
return
filePartFlux
.
flatMap
(
filePart
->
return
filePartFlux
.
flatMap
(
filePart
->
...
@@ -156,18 +169,57 @@ public class InventoryService implements Serializable {
...
@@ -156,18 +169,57 @@ public class InventoryService implements Serializable {
df
.
printSchema
();
df
.
printSchema
();
Dataset
<
R
ow
>
inventoryList
=
df
Dataset
<
R
awInventory
>
inventoryList
=
df
.
filter
(
functions
.
col
(
"key"
).
equalTo
(
batchId
))
.
filter
(
functions
.
col
(
"key"
).
equalTo
(
batchId
))
.
selectExpr
(
"CAST(value as String) as message"
)
.
selectExpr
(
"CAST(value as String) as message"
)
.
select
(
functions
.
from_json
(
functions
.
col
(
"message"
),
schema
).
as
(
"data"
))
.
select
(
functions
.
from_json
(
functions
.
col
(
"message"
),
schema
).
as
(
"data"
))
.
select
(
functions
.
explode
(
functions
.
col
(
"data.inventoryList"
)).
as
(
"list"
))
.
select
(
functions
.
explode
(
functions
.
col
(
"data.inventoryList"
)).
as
(
"list"
))
.
filter
(
functions
.
col
(
"list.name"
).
equalTo
(
"Handcrafted Trees Mug"
))
.
filter
(
.
select
(
"list.*"
);
functions
.
col
(
"list.price"
).
leq
(
15
)
.
and
(
functions
.
col
(
"list.itemType"
).
contains
(
"mug"
))
.
and
(
functions
.
col
(
"list.name"
).
contains
(
"Coffee"
))
)
.
select
(
"list.*"
)
.
as
(
Encoders
.
bean
(
RawInventory
.
class
));
StreamingQuery
streamingQuery
=
inventoryList
.
writeStream
()
.
trigger
(
Trigger
.
Once
())
.
foreach
(
new
ForeachWriter
<
RawInventory
>()
{
private
MongoCollection
<
ProcessedInventory
>
processedInventoryMongoCollection
;
private
MongoClient
client
;
@Override
public
boolean
open
(
long
partitionId
,
long
epochId
)
{
String
uri
=
"mongodb://127.0.0.1:27017"
;
client
=
MongoClients
.
create
(
uri
);
CodecRegistry
pojoCodecRegistry
=
CodecRegistries
.
fromRegistries
(
MongoClientSettings
.
getDefaultCodecRegistry
(),
CodecRegistries
.
fromProviders
(
PojoCodecProvider
.
builder
().
automatic
(
true
).
build
()));
// Accessing the database
MongoDatabase
mongoDatabase
=
client
.
getDatabase
(
"inventorydb"
).
withCodecRegistry
(
pojoCodecRegistry
);
processedInventoryMongoCollection
=
mongoDatabase
.
getCollection
(
"inventory"
,
ProcessedInventory
.
class
);
return
true
;
}
StreamingQuery
streamingQuery
=
inventoryList
@Override
public
void
process
(
RawInventory
value
)
{
InsertOneResult
insertOneResult
=
processedInventoryMongoCollection
.
insertOne
(
InventoryMapper
.
INSTANCE
.
dtoToEntity
(
value
,
UUID
.
randomUUID
().
toString
()));
log
.
info
(
"Inserted Result : {} "
,
insertOneResult
);
}
@Override
public
void
close
(
Throwable
errorOrNull
)
{
client
.
close
();
}
}).
start
();
/*StreamingQuery streamingQuery=inventoryList
.writeStream()
.writeStream()
.trigger(Trigger.Once())
.trigger(Trigger.Once())
.format("json")
.format("json")
...
@@ -175,7 +227,7 @@ public class InventoryService implements Serializable {
...
@@ -175,7 +227,7 @@ public class InventoryService implements Serializable {
.option("checkpointLocation", "src/main/resources/data/checkpointLocation/StreamingJob")
.option("checkpointLocation", "src/main/resources/data/checkpointLocation/StreamingJob")
.option("path","src/main/resources/data/"+batchId)
.option("path","src/main/resources/data/"+batchId)
.outputMode(OutputMode.Append())
.outputMode(OutputMode.Append())
.
start
();
.start();
*/
streamingQuery
.
awaitTermination
();
streamingQuery
.
awaitTermination
();
...
@@ -185,6 +237,4 @@ public class InventoryService implements Serializable {
...
@@ -185,6 +237,4 @@ public class InventoryService implements Serializable {
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment