Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
BigData-radio
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ANDRE Quentin
BigData-radio
Commits
7c86926e
Commit
7c86926e
authored
2 years ago
by
Quentin ANDRE
Browse files
Options
Downloads
Patches
Plain Diff
spark
parent
9eb6eb1a
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
spark_streaming.py
+31
-11
31 additions, 11 deletions
spark_streaming.py
with
31 additions
and
11 deletions
spark_streaming.py
+
31
−
11
View file @
7c86926e
...
...
@@ -3,7 +3,7 @@ from geopy.geocoders import Nominatim
import
sys
from
pyspark.sql
import
SparkSession
from
pyspark.sql.types
import
*
from
pyspark.sql.functions
import
split
from
pyspark.sql.functions
import
split
,
from_json
,
col
geolocator
=
Nominatim
(
user_agent
=
"
quentin.andre@imt-atlantique.net
"
)
...
...
@@ -17,27 +17,47 @@ def find_country(lat, long):
return
None
spark
=
SparkSession
.
builder
.
appName
(
"
Spark Structured Streaming from Kafka
"
).
getOrCreate
()
p
lanes
=
spark
.
readStream
.
format
(
"
kafka
"
)
\
sdfP
lanes
=
spark
.
readStream
.
format
(
"
kafka
"
)
\
.
option
(
"
kafka.bootstrap.servers
"
,
"
localhost:9092
"
)
\
.
option
(
"
subscribe
"
,
"
air-traffic
"
)
\
.
option
(
"
startingOffsets
"
,
"
latest
"
)
\
.
load
().
selectExpr
(
"
CAST(value AS
JSON
)
"
)
.
load
().
selectExpr
(
"
CAST(value AS
STRING
)
"
)
taxiFar
esSchema
=
StructType
([
StructField
(
"
on_ground
"
,
BoolType
()),
plan
esSchema
=
StructType
([
StructField
(
"
on_ground
"
,
Bool
ean
Type
()),
StructField
(
"
icao24
"
,
LongType
()),
StructField
(
"
sensors
"
,
LongType
()),
StructField
(
"
vertical_rate
"
,
Timestamp
Type
()),
StructField
(
"
vertical_rate
"
,
Float
Type
()),
StructField
(
"
origin_country
"
,
StringType
()),
StructField
(
"
squawk
"
,
LongType
()),
StructField
(
"
geo_altitude
"
,
FloatType
()),
StructField
(
"
baro_altitude
"
,
FloatType
()),
StructField
(
"
velocity
"
,
FloatType
()),
StructField
(
"
latitude
"
,
FloatType
()),
StructField
(
"
spi
"
,
BooleanType
()),
StructField
(
"
position_source
"
,
IntegerType
()),
StructField
(
"
last_contact
"
,
LongType
()),
StructField
(
"
time_position
"
,
LongType
()),
StructField
(
"
heading
"
,
FloatType
()),
StructField
(
"
time
"
,
LongType
()),
StructField
(
"
longitude
"
,
FloatType
()),
StructField
(
"
callsign
"
,
StringType
()),
])
'''
{
"
on_ground
"
: false,
"
icao24
"
:
"
407182
"
,
"
sensors
"
: null,
"
vertical_rate
"
: 0,
"
origin_country
"
:
"
United Kingdom
"
,
"
squawk
"
:
"
7755
"
,
"
geo_altitude
"
: 8214.36,
"
baro_altitude
"
: 8229.6,
"
velocity
"
: 176.26,
"
latitude
"
: 54.1107,
"
spi
"
: false,
"
position_source
"
: 0,
"
last_contact
"
: 1646922076,
"
time_position
"
: 1646922076,
"
heading
"
: 158.6,
"
time
"
: 1646922077,
"
longitude
"
: -2.8725,
"
callsign
"
:
"
EXS1LY
"
}
'''
'''
{
"
on_ground
"
: false,
"
icao24
"
:
"
407182
"
,
"
sensors
"
: null,
"
vertical_rate
"
: 0,
"
origin_country
"
:
"
United Kingdom
"
,
"
squawk
"
:
"
7755
"
,
"
geo_altitude
"
: 8214.36,
"
baro_altitude
"
: 8229.6,
"
velocity
"
: 176.26,
"
latitude
"
: 54.1107,
"
spi
"
: false,
"
position_source
"
: 0,
"
last_contact
"
: 1646922076,
"
time_position
"
: 1646922076,
"
heading
"
: 158.6,
"
time
"
: 1646922077,
"
longitude
"
: -2.8725,
"
callsign
"
:
"
EXS1LY
"
}
'''
def
parse_data_from_kafka_message
(
sdf
,
schema
):
assert
sdf
.
isStreaming
==
True
,
"
DataFrame doesn
'
t receive streaming data
"
col
=
split
(
sdf
[
'
value
'
],
'
,
'
)
#split attributes to nested array in one Column
'''
col = split(sdf[
'
value
'
],
'
,
'
) #split attributes to nested array in one Column
#now expand col to multiple top-level columns
for idx, field in enumerate(schema):
sdf
=
sdf
.
withColumn
(
field
.
name
,
col
.
getItem
(
idx
).
cast
(
field
.
dataType
))
return
sdf
.
select
([
field
.
name
for
field
in
schema
])
sdfRides
=
parse_data_from_kafka_message
(
sdfRides
,
taxiRidesSchema
)
\ No newline at end of file
sdf = sdf.withColumn(field.name, col.getItem(idx).cast(field.dataType))
'''
return
sdf
.
withColumn
(
"
value
"
,
from_json
(
"
value
"
,
schema
)).
select
([
field
.
name
for
field
in
schema
])
sdfPlanes
=
parse_data_from_kafka_message
(
sdfPlanes
,
planesSchema
)
sdfPlanes
.
show
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment