Process IoT Device Data Using Datasets(Scala)
Loading...

How to Process IoT Device JSON Data Using Datasets

Datasets in Apache Spark 2 and 3 provide high-level domain specific APIs as well as provide structure and compile-time type safety. You can read your JSON data file into a DataFrame, which is essentially a generic row of JVM objects, and convert them into a type-specific collection of JVM objects.

This notebook shows you how to read a JSON file, convert your semi-structured JSON data into a collection of Datasets[T], where T is a class, and provides an introduction to some high-level Spark Dataset APIs.

Reading JSON as a Dataset

Use the Scala case class DeviceIoTData to convert the JSON device data into a Scala object. In addition to the IP address, the file contains the geographic information for each device entry:

  • ISO-3166-1 two and three letter codes
  • Country Name
  • Latitude and longitude

For each IP address, you can obtain these attributes from a webservice at https://ipstack.com/. With these attributes, you can create map visualizations of the data.

{"device_id": 198164, "device_name": "sensor-pad-198164owomcJZ", "ip": "80.55.20.25", "cca2": "PL", "cca3": "POL", "cn": "Poland", "latitude": 53.080000, "longitude": 18.620000, "scale": "Celsius", "temp": 21, "humidity": 65, "battery_level": 8, "c02_level": 1408, "lcd": "red", "timestamp" :1458081226051 }

Create a case class to represent your IoT Device Data

case class DeviceIoTData (battery_level: Long, c02_level: Long, cca2: String, cca3: String, cn: String, device_id: Long, device_name: String, humidity: Long, ip: String, latitude: Double, lcd: String, longitude: Double, scale:String, temp: Long, timestamp: Long)
defined class DeviceIoTData
//read the JSON file and create the dataset from the case class DeviceIoTData
// ds is now a collection of JVM Scala objects DeviceIoTData
val ds = spark.read.json(s"/databricks-datasets/iot/iot_devices.json").as[DeviceIoTData]
ds: org.apache.spark.sql.Dataset[DeviceIoTData] = [battery_level: bigint, c02_level: bigint ... 13 more fields]

Count the number of devices

ds.count()
res56: Long = 198164

Display your Dataset

display(ds)
8868USUSAUnited States1meter-gauge-1xbYRYcj5168.161.225.138green-97Celsius341458444054093
71473NONORNorway2sensor-pad-2n2Pea70213.161.254.162.47red6.15Celsius111458444054119
21556ITITAItaly3device-mac-36TWSKiT4488.36.5.142.83red12.83Celsius191458444054120
61080USUSAUnited States4sensor-pad-4mzWkz3266.39.173.15444.06yellow-121.32Celsius281458444054121
4931PHPHLPhilippines5therm-stick-5gimpUrBB62203.82.41.914.58green120.97Celsius251458444054122
31210USUSAUnited States6sensor-pad-6al7RTAobR51204.116.105.6735.93yellow-85.46Celsius271458444054122
31129CNCHNChina7meter-gauge-7GeDoanM26220.173.179.122.82yellow108.32Celsius181458444054123
01536JPJPNJapan8sensor-pad-8xUD6pzsQI35210.173.177.135.69red139.69Celsius271458444054123
3807JPJPNJapan9device-mac-9GcjZ2pw85118.23.68.22735.69green139.69Celsius131458444054124
71470USUSAUnited States10sensor-pad-10BsywSYUF56208.109.163.21833.61red-111.89Celsius261458444054125
31544ITITAItaly11meter-gauge-11dlMTZty8588.213.191.3442.83red12.83Celsius161458444054125
01260USUSAUnited States12sensor-pad-12Y2kIm0o9268.28.91.2238yellow-97Celsius121458444054126
61007ININDIndia13meter-gauge-13GrojanSGBz9259.144.114.25028.6yellow77.2Celsius131458444054127
11346NONORNorway14sensor-pad-14QL93sBR0j90193.156.90.20059.95yellow10.75Celsius161458444054127
91259USUSAUnited States15device-mac-15se6mZ7067.185.72.147.41yellow-122Celsius131458444054128
41425USUSAUnited States16sensor-pad-16aXmIJZtdO5368.85.85.10638red-97Celsius151458444054128
01466USUSAUnited States17meter-gauge-17zb8Fghhl98161.188.212.25439.95red-75.16Celsius311458444054129
41096CNCHNChina18sensor-pad-18XULN9Xv25221.3.128.24225.04yellow102.72Celsius311458444054130
91531USUSAUnited States19meter-gauge-19eg1BpfCO7564.124.180.21538red-97Celsius291458444054130
71155USUSAUnited States20sensor-pad-20gFNfBgqr3366.153.162.6633.94yellow-78.92Celsius101458444054131
5939ATAUTAustria21device-mac-21sjz5h44193.200.142.25448.2green16.37Celsius301458444054131
71522JPJPNJapan22sensor-pad-22oWV2D58221.113.129.8335.69red139.69Celsius241458444054132
51245ININDIndia23meter-gauge-230IupA4759.90.65.112.98yellow77.58Celsius231458444054133
81511CACANCanada24sensor-pad-24PytzD00Cp7369.90.67.143.85red-79.33Celsius281458444054133
4880USUSAUnited States25therm-stick-25kK6VyzIFB7824.154.45.9041.1green-80.76Celsius271458444054134
91300JPJPNJapan26sensor-pad-26rAyCZQOQH927210.158.147.1135.69yellow139.69Celsius101458444054135
51597KRKORRepublic of Korea27device-mac-27P5wf273218.239.168.137.57red126.98Celsius101458444054135
31502KRKORRepublic of Korea28sensor-pad-28Tsudcoikw64211.238.224.7737.29red127.01Celsius251458444054136
61095NLNLDNetherlands29meter-gauge-29lyNVxIS6983.98.224.4952.37yellow4.9Celsius151458444054137
6992USUSAUnited States30sensor-pad-30FC7VCyFp0i4666.225.253.4741.85green-87.65Celsius141458444054137
5979ESESPSpain31meter-gauge-31bcRD8TqXry4880.251.64.140.4green-3.68Celsius111458444054138
81262USUSAUnited States32sensor-pad-329KFvY30128.83.9.14630.3yellow-97.74Celsius191458444054139
3835RURUSRussia33device-mac-33B94GfPzi30178.23.147.13455.75green37.62Celsius151458444054139
7940RURUSRussia34sensor-pad-34F1Jubre3B8980.255.179.7857.62green39.86Celsius271458444054140
71131CNCHNChina35therm-stick-35Lg804z35124.164.2.18635yellow105Celsius211458444054141
11305CYCYPCyprus36sensor-pad-36VQv8fnEg47213.7.14.135yellow33Celsius241458444054141
5881JPJPNJapan37meter-gauge-3773dlrR76211.128.96.23035.69green139.69Celsius101458444054142
51345FRFRAFrance38sensor-pad-38hWAVaS90193.51.189.6148.86yellow2.35Celsius161458444054143
2908CNCHNChina39device-mac-39iklYVtvBT84218.7.15.145.75green126.65Celsius171458444054144
21224FRFRAFrance40sensor-pad-40NjeMqS27188.7.9.149.17yellow6.19Celsius341458444054145
61200ITITAItaly41meter-gauge-41oj7N18qG8s77212.103.205.25042.83yellow12.83Celsius201458444054146
71313ESESPSpain42sensor-pad-42I0jjE4062.82.3.6641.39yellow2.16Celsius201458444054147
2913ESESPSpain43meter-gauge-43RYonsvaW3980.81.64.14240.4green-3.68Celsius121458444054148
0917DEDEUGermany44sensor-pad-448DeWGL6362.128.16.7449.46green11.1Celsius271458444054149
4985ITITAItaly45device-mac-45fN2COZF4cM74213.140.20.23042.83green12.83Celsius261458444054149
8857USUSAUnited States46sensor-pad-46MiQ33UDaaa6365.5.48.25336.17green-86.78Celsius121458444054150
31454UAUKRUkraine47meter-gauge-47WsF9s890195.178.145.20950.45red30.52Celsius291458444054151
9986FRFRAFrance48sensor-pad-48jt4eL9790.37.208.143.88green4.9Celsius311458444054151
4967USUSAUnited States49meter-gauge-49YesGXwt70170.37.224.142.28green-71.44Celsius251458444054152
61217DEDEUGermany50sensor-pad-50g2ukc6962.153.177.7351yellow9Celsius291458444054152
5921GBGBRUnited Kingdom51device-mac-51iy02vXU64146.97.40.11351.5green-0.13Celsius131458444054153
21032USUSAUnited States52sensor-pad-52eFObBC3472.49.128.138yellow-97Celsius101458444054153
81446SESWESweden53meter-gauge-534fDwDPf6880.89.168.3459.33red18.07Celsius201458444054154
81436USUSAUnited States54sensor-pad-5410CWPrNb673204.15.64.24932.89red-117.13Celsius341458444054155
7997CNCHNChina55therm-stick-55kEAHLqWn029124.160.81.2230.29green120.16Celsius181458444054156
51038THTHAThailand56sensor-pad-564BJDqo67203.130.148.4113.75yellow100.47Celsius301458444054157
61503USUSAUnited States57device-mac-57tRB0uMlvSb4064.246.146.1342.66red-73.75Celsius221458444054158
41105MXMEXMexico58sensor-pad-58HgZVw096200.76.250.432.53yellow-117.02Celsius141458444054159
81242GBGBRUnited Kingdom59meter-gauge-59yhdg6rHM2686.189.54.12951.5yellow-0.13Celsius331458444054159
61206DEDEUGermany60sensor-pad-60z8B04PJh80134.61.64.151.45yellow7.02Celsius231458444054160
71160FRFRAFrance61meter-gauge-61NehO8Msi9386.198.202.148.86yellow2.35Celsius161458444054160
31082USUSAUnited States62sensor-pad-62fH8oKr8aiT31151.198.215.138yellow-97Celsius271458444054161
41090USUSAUnited States63device-mac-63GL4xSaZbj9166.198.198.144.56yellow-105.67Celsius311458444054162
31524USUSAUnited States64sensor-pad-64djcIn5538.99.198.18638red-97Celsius121458444054162
3872CLCHLChile65therm-stick-65V4WstIg2864.76.146.154-33.45green-70.67Celsius231458444054163
81482CZCZECzech Republic66sensor-pad-663dzoWk2U326213.210.151.149.98red14.73Celsius101458444054163
6947ITITAItaly67meter-gauge-67E75oX4678.110.188.645.47green9.2Celsius231458444054164
51250USUSAUnited States68sensor-pad-682gym3oKv81216.83.68.10342.4yellow-113.78Celsius121458444054164
4825HRHRVCroatia69device-mac-69kjyS4IgN25194.146.109.1245.17green15.5Celsius101458444054165
81399USUSAUnited States70sensor-pad-707OD4ui6U54137.164.46.14333.82yellow-118.04Celsius261458444054165
8934BYBLRBelarus71meter-gauge-712JgErD0zVw43195.222.71.153.9green27.57Celsius171458444054166
3949FIFINFinland72sensor-pad-72c0to96480.222.48.163.2green23.02Celsius251458444054166
4943USUSAUnited States73meter-gauge-73MhL2W92209.251.80.5842.73green-87.87Celsius261458444054167
51323CNCHNChina74sensor-pad-74E5pQZOTx25121.33.241.19323.12yellow113.25Celsius101458444054168
61328ATAUTAustria75device-mac-75OLmCeTdSwc96143.161.246.6548.2yellow16.37Celsius121458444054168
91212USUSAUnited States76sensor-pad-76h137fwB674208.103.221.138.87yellow-87.67Celsius201458444054169
11454ININDIndia77meter-gauge-77IKW3YAB5582218.248.255.3012.98red77.58Celsius171458444054169
61493KRKORRepublic of Korea78sensor-pad-78PkeI6U7td835222.234.0.137.57red126.98Celsius251458444054170
31291CNCHNChina79meter-gauge-79pb40uAO48219.158.12.6235yellow105Celsius231458444054170
01233CACANCanada80sensor-pad-80TY4dWSMH57159.128.0.18150.01yellow-97.22Celsius321458444054171
41072PHPHLPhilippines81device-mac-81nsKomrRe90222.127.71.114.55yellow121.04Celsius311458444054172
31076FRFRAFrance82sensor-pad-82HJm6yP76213.162.50.3348.86yellow2.35Celsius321458444054172
91221DEDEUGermany83meter-gauge-83lLWufdrzWE9662.214.32.22251yellow9Celsius311458444054173
1941JPJPNJapan84sensor-pad-84jla9J5O31122.133.0.2635.69green139.69Celsius271458444054174
11028USUSAUnited States85therm-stick-85NcuaO88205.213.119.4243.06yellow-89.41Celsius171458444054174
91167IDIDNIndonesia86sensor-pad-86NCIl72NBUJ26202.55.169.189-6.17yellow106.83Celsius231458444054175
11212USUSAUnited States87device-mac-87EJxth2l38204.16.48.13033.42yellow-86.68Celsius251458444054175
5894CHCHESwitzerland88sensor-pad-88nm5Thggzs2985.218.127.9347green8Celsius321458444054176
91423DEDEUGermany89meter-gauge-89fFsrU29QEU97217.160.140.23551red9Celsius191458444054176
81244DEDEUGermany90sensor-pad-90SuYQ8Ff044213.214.12.6651yellow9Celsius131458444054177
3928SESWESweden91meter-gauge-913q78Z1wkK9762.20.110.160.13green13Celsius281458444054177
01021KRKORRepublic of Korea92sensor-pad-92vxuq7e52211.195.95.6137.57yellow126.98Celsius281458444054178
4918RURUSRussia93device-mac-93FXnVpo42195.19.194.7761.67green50.81Celsius171458444054178
81290USUSAUnited States94sensor-pad-94HL9jChD6624.32.26.139.33yellow-120.25Celsius281458444054179
51553ECECUEcuador95therm-stick-95LXCTWLh46200.25.144.102-2.17red-79.9Celsius261458444054179
6938LTLTURepublic of Lithuania96sensor-pad-96Fzitfx11wj9684.32.38.156green24Celsius101458444054180
81593JPJPNJapan97meter-gauge-97K6lrYGS660124.32.46.135.69red139.75Celsius331458444054180
0952USUSAUnited States98sensor-pad-98mJQAfJpfW5012.116.241.9438green-97Celsius221458444054181
21131USUSAUnited States99device-mac-99Xh5YsPAzOU27146.82.204.3642.4yellow-71.26Celsius221458444054181
51186BEBELBelgium100sensor-pad-1009LusgoDso48217.113.83.24750.85yellow4.35Celsius181458444054182

Showing the first 1000 rows.

Iterating, transforming, and filtering Dataset

Iterate over the first 10 entries with the foreach() method and print them

ds.take(10).foreach(println(_))
DeviceIoTData(8,868,US,USA,United States,1,meter-gauge-1xbYRYcj,51,68.161.225.1,38.0,green,-97.0,Celsius,34,1458444054093) DeviceIoTData(7,1473,NO,NOR,Norway,2,sensor-pad-2n2Pea,70,213.161.254.1,62.47,red,6.15,Celsius,11,1458444054119) DeviceIoTData(2,1556,IT,ITA,Italy,3,device-mac-36TWSKiT,44,88.36.5.1,42.83,red,12.83,Celsius,19,1458444054120) DeviceIoTData(6,1080,US,USA,United States,4,sensor-pad-4mzWkz,32,66.39.173.154,44.06,yellow,-121.32,Celsius,28,1458444054121) DeviceIoTData(4,931,PH,PHL,Philippines,5,therm-stick-5gimpUrBB,62,203.82.41.9,14.58,green,120.97,Celsius,25,1458444054122) DeviceIoTData(3,1210,US,USA,United States,6,sensor-pad-6al7RTAobR,51,204.116.105.67,35.93,yellow,-85.46,Celsius,27,1458444054122) DeviceIoTData(3,1129,CN,CHN,China,7,meter-gauge-7GeDoanM,26,220.173.179.1,22.82,yellow,108.32,Celsius,18,1458444054123) DeviceIoTData(0,1536,JP,JPN,Japan,8,sensor-pad-8xUD6pzsQI,35,210.173.177.1,35.69,red,139.69,Celsius,27,1458444054123) DeviceIoTData(3,807,JP,JPN,Japan,9,device-mac-9GcjZ2pw,85,118.23.68.227,35.69,green,139.69,Celsius,13,1458444054124) DeviceIoTData(7,1470,US,USA,United States,10,sensor-pad-10BsywSYUF,56,208.109.163.218,33.61,red,-111.89,Celsius,26,1458444054125)

For all relational expressions, Apache Spark formulates an optimized logical and physical plan for execution, and optimizes the generated code. For our DeviceIoTData, it will use its standard encoders to optimize its binary internal representation, hence decrease the size of generated code, minimize the bytes transferred over the networks between nodes, and execute faster.

First filter the device dataset on temp and humidity attributes with a predicate.

// Issue select, map, filter, foreach operations on the datasets, just as you would for DataFrames
// it returns back a Dataset
val dsTempDS = ds.filter(d => {d.temp > 30 && d.humidity > 70})
display(dsTempDS)

01466USUSAUnited States17meter-gauge-17zb8Fghhl98161.188.212.25439.95red-75.16Celsius311458444054129
9986FRFRAFrance48sensor-pad-48jt4eL9790.37.208.143.88green4.9Celsius311458444054151
81436USUSAUnited States54sensor-pad-5410CWPrNb673204.15.64.24932.89red-117.13Celsius341458444054155
41090USUSAUnited States63device-mac-63GL4xSaZbj9166.198.198.144.56yellow-105.67Celsius311458444054162
41072PHPHLPhilippines81device-mac-81nsKomrRe90222.127.71.114.55yellow121.04Celsius311458444054172
31076FRFRAFrance82sensor-pad-82HJm6yP76213.162.50.3348.86yellow2.35Celsius321458444054172
91221DEDEUGermany83meter-gauge-83lLWufdrzWE9662.214.32.22251yellow9Celsius311458444054173
21182USUSAUnited States108sensor-pad-108NG6gl2jPi82208.35.184.25434.2yellow-118.82Celsius341458444054187
6852USUSAUnited States109meter-gauge-109PooBS8024.29.148.7338green-97Celsius321458444054188
41188DKDNKDenmark144sensor-pad-144T0J4k87212.242.41.5055.68yellow12.57Celsius311458444054211
61201EGEGYEgypt153device-mac-1535rtr4Bw4hD96163.121.192.14630.08yellow31.29Celsius321458444054215
61521GBGBRUnited Kingdom157meter-gauge-157XyFxAnJ8783.146.16.22651.5red-0.13Celsius321458444054217
51215RURUSRussia177device-mac-177LUZKzT7M9062.69.7.155.75yellow37.62Celsius341458444054226
31522KRKORRepublic of Korea186sensor-pad-186cRwjgGbRya88203.246.170.13437.57red126.98Celsius331458444054230
81399RURUSRussia215therm-stick-215rDovnHVn7685.26.238.5355.75yellow37.62Celsius331458444054243
51176CZCZECzech Republic262sensor-pad-262UFel3QHY875217.64.12.1450.08yellow14.42Celsius341458444054268
0931CRCRICosta Rica268sensor-pad-2685AydUae91200.91.105.1710green-84Celsius321458444054270
31200ITITAItaly286sensor-pad-286iRwLlOpR1v8894.47.149.2642.83yellow12.83Celsius341458444054276
41406USUSAUnited States303device-mac-303NtnEHKBNq91216.6.91.1339.74red-75.67Celsius331458444054281
11576CNCHNChina307meter-gauge-307sUHtMY891210.22.154.4031.05red121.4Celsius311458444054283
51170KRKORRepublic of Korea310sensor-pad-310SXQ1w8C82222.107.215.137.57yellow126.98Celsius321458444054284
81297IDIDNIndonesia353meter-gauge-353HMTZmHX93202.78.195.1-6.17yellow106.83Celsius311458444054298
11234USUSAUnited States357device-mac-357L3JNT9666.26.129.11835.22yellow-80.79Celsius341458444054299
41116FRFRAFrance374sensor-pad-374tmp90bW77188.7.34.148.86yellow2.35Celsius341458444054305
7810USUSAUnited States375device-mac-375V9BU5MG78206.128.120.23037.96green-121.26Celsius341458444054305
91043HUHUNHungary380sensor-pad-3803cakh77213.163.52.10647.49yellow19.05Celsius321458444054307
51248USUSAUnited States410sensor-pad-410mm3FeH7668.1.0.13338yellow-97Celsius331458444054316
91546KRKORRepublic of Korea474sensor-pad-474nftAl1at91211.247.149.137.57red126.98Celsius341458444054336
31556CNCHNChina487meter-gauge-4877NPlK95221.5.254.7429.56red106.55Celsius321458444054340
61461CNCHNChina492sensor-pad-492MFWg6x94220.164.69.125.04red102.72Celsius341458444054341
2816ATAUTAustria504sensor-pad-504KdiBKWIN7887.243.151.19347.27green11.4Celsius321458444054344
31517BRBRABrazil520sensor-pad-520Ua4UYBdOa73200.141.252.38-23.55red-46.64Celsius341458444054348
91318AZAZEAzerbaijan533meter-gauge-533HCPvYD84217.14.96.3440.5yellow47.5Celsius341458444054351
91572GBGBRUnited Kingdom547meter-gauge-547pBg3KO8082.138.249.5451.5red-0.13Celsius311458444054355
71350USUSAUnited States573device-mac-573qRwuac9865.77.177.10638yellow-97Celsius321458444054361
71521CACANCanada577meter-gauge-5771uqqa380216.8.137.6642.28red-82.98Celsius331458444054362
4875RURUSRussia605therm-stick-605PPmM891194.190.198.19455.75green37.62Celsius341458444054369
51373CNCHNChina624sensor-pad-624ulkCbj8COG7261.160.71.132.06yellow118.78Celsius311458444054374
91302JPJPNJapan668sensor-pad-668sfSlR88219.113.86.22635.69yellow139.69Celsius321458444054384
3959DEDEUGermany672sensor-pad-672NKhrFOJC7762.154.10.23351green9Celsius311458444054385
4852IDIDNIndonesia682sensor-pad-682iHc9Lg84202.146.128.254-6.17green106.83Celsius331458444054387
31524FRFRAFrance684sensor-pad-684NUMHKy95213.36.80.148.86red2.35Celsius311458444054388
11445RURUSRussia685therm-stick-685KsBPtDFGg71212.188.28.18555.75red37.62Celsius321458444054388
21149RURUSRussia703meter-gauge-703cFaf0td9QJ7562.213.32.17851.54yellow46.01Celsius331458444054392
4857USUSAUnited States715therm-stick-715xiU9vgwXx71216.180.237.1333.73green-84.43Celsius331458444054394
7907USUSAUnited States731meter-gauge-73158eHTQc080205.144.210.134.8green-84.91Celsius331458444054398
11037USUSAUnited States785therm-stick-785pGVAsag98134.139.27.133.78yellow-118.12Celsius341458444054408
61283DEDEUGermany787meter-gauge-787szq1TVrRxu96194.139.39.151yellow9Celsius321458444054408
41571MXMEXMexico792sensor-pad-792kFx0i84148.240.254.4525.67red-100.32Celsius311458444054409
31285USUSAUnited States808sensor-pad-808OcbAvEv2c71206.82.139.3039.74yellow-75.67Celsius311458444054412
91379JPJPNJapan816sensor-pad-816LIJkQ72221.112.14.16135.69yellow139.69Celsius311458444054413
71063AUAUSAustralia830sensor-pad-830vNGUV480121.200.226.19-37.81yellow144.96Celsius321458444054416
81520CNCHNChina854sensor-pad-854SPBFeq72211.136.2.17435red105Celsius311458444054420
11173USUSAUnited States873device-mac-873TkYao7795209.81.10.137.39yellow-122.08Celsius331458444054423
5995CNCHNChina878sensor-pad-878dgegckMv97221.218.160.139.93green116.39Celsius341458444054424
41048USUSAUnited States880sensor-pad-880mhCUcRX886141.218.144.142.27yellow-85.62Celsius311458444054425
81399USUSAUnited States897device-mac-8974FYJ593138.123.138.139.3yellow-75.57Celsius311458444054427
31189CLCHLChile899meter-gauge-8999HhQOMB71200.72.3.142-33.45yellow-70.67Celsius341458444054428
0877KRKORRepublic of Korea915device-mac-915u1MkX93125.134.15.2137.57green126.98Celsius321458444054430
01509AUAUSAustralia939device-mac-939ng1nz8kkH77203.94.135.1-33.84red151.18Celsius331458444054434
0861ARARGArgentina943meter-gauge-943BT5wQ05777200.59.128.19-34.6green-58.38Celsius331458444054435
51496USUSAUnited States968sensor-pad-968muLvQOE8268.172.21.138red-97Celsius341458444054439
01307CACANCanada975device-mac-975Zcm9meJ93198.168.252.9945.45yellow-73.67Celsius331458444054440
81465USUSAUnited States999device-mac-999iFA05vUvT8624.220.1.22643.64red-94.48Celsius341458444054443
51469CNCHNChina1011device-mac-1011Gr2MbbD95221.10.3.7830.67red104.07Celsius331458444054445
61140USUSAUnited States1032sensor-pad-1032WY7Trz8a91128.36.88.141.31yellow-72.93Celsius331458444054449
51324USUSAUnited States1041device-mac-1041WGplXofrK85216.52.93.17838yellow-97Celsius341458444054450
81006USUSAUnited States1060sensor-pad-1060wMhKFq9071.102.66.133.55yellow-115.97Celsius321458444054453
41340UAUKRUkraine1068sensor-pad-10680EU8mpVK89193.239.238.150.45yellow30.52Celsius311458444054454
61449PTPRTPortugal1082sensor-pad-1082IdyLA99213.30.95.13338.71red-9.14Celsius311458444054456
11047USUSAUnited States1112sensor-pad-1112hV4DEmjOjG89206.251.180.829.94yellow-90.07Celsius321458444054461
21369FRFRAFrance1161device-mac-1161o4sMSX89213.228.12.23348.86yellow2.35Celsius321458444054468
6959USUSAUnited States1208sensor-pad-1208l7k8Omo9624.249.36.16238green-97Celsius341458444054475
91075USUSA1217meter-gauge-1217heLo9zG2z795213.248.81.8247yellow8Celsius331458444054477
31387USUSAUnited States1233device-mac-1233NUgNva7DUo7269.58.194.3841.29yellow-96.17Celsius331458444054479
01075DEDEUGermany1241meter-gauge-12414MAYDjD3Lo97193.16.245.151yellow9Celsius341458444054480
5913USUSAUnited States1259meter-gauge-1259y34mBQwNA81209.177.243.130.38green-91.06Celsius321458444054484
21130AUAUSAustralia1278sensor-pad-1278k1OL1IJaKk73203.94.160.1-37.88yellow145.13Celsius321458444054488
6820USUSAUnited States1283meter-gauge-1283AM3yoi89206.81.193.2138green-97Celsius311458444054490
61547HKHKGHong Kong1306sensor-pad-130645On9xd8059.148.197.122.28red114.15Celsius341458444054495
51581SESWESweden1308sensor-pad-1308lHiXEB95194.23.0.159.33red18.07Celsius331458444054496
61272RURUSRussia1314sensor-pad-1314zgkWn1hCY181217.10.37.2155.75yellow37.62Celsius321458444054497
01416CNCHNChina1327meter-gauge-1327ZYIFgSq97218.106.127.21430.58red114.27Celsius341458444054500
71294USUSAUnited States1329device-mac-1329F33uek8966.206.8.347.64yellow-117.1Celsius341458444054500
41485USUSAUnited States1341device-mac-134162cJBci89865.51.155.19738red-97Celsius331458444054502
31178USUSAUnited States1389device-mac-1389yhoLvVjds82129.236.255.1041.02yellow-73.91Celsius321458444054512
7940USUSAUnited States1397meter-gauge-13973RlJbAWz88472.78.244.140.08green-75.29Celsius331458444054513
01586CNCHNChina1410sensor-pad-1410awngNXi91202.97.34.5035red105Celsius311458444054515
51023IDIDNIndonesia1411meter-gauge-1411aOLIk6aLM76111.67.67.69-6.71yellow108.56Celsius341458444054515
0819RURUSRussia1421meter-gauge-1421UdywAu9295.128.224.13055.75green37.62Celsius311458444054517
5890KRKORRepublic of Korea1431device-mac-14316IlJr8361.110.192.14937.57green126.98Celsius321458444054518
0877DEDEUGermany1445therm-stick-14453TR6Wf7Z8992.79.231.13851green9Celsius341458444054520
21162RURUSRussia1452sensor-pad-1452TkZ73cLC3v8382.196.95.23455.75yellow37.62Celsius341458444054521
71140KRKORRepublic of Korea1454sensor-pad-1454iQWzA77124.61.162.137.57yellow126.98Celsius341458444054521
81240DEDEUGermany1457meter-gauge-1457LBEi8DzS9Z97195.60.208.12751yellow9Celsius331458444054522
11011KGKGZKyrgyzstan1474sensor-pad-1474KilQz8085.115.220.141yellow75Celsius331458444054524
61188IDIDNIndonesia1481meter-gauge-1481q7ehfQ78202.152.7.81-6.17yellow106.83Celsius331458444054525
21525HKHKGHong Kong1517meter-gauge-1517xSSaN88Z77116.0.82.11022.28red114.15Celsius311458444054531
01518USUSA1521device-mac-1521KUBAw873130.117.2.10647red8Celsius331458444054531
21497TWTWNTaiwan1530sensor-pad-1530X7iAneyUG172220.128.1.18223.5red121Celsius321458444054538

Showing the first 1000 rows.

Use Dataset APIs for filtering: take(10) returns an Array[DeviceIoTData]; using a foreach() method on the Array collection, print each item.

//filter out dataset rows that meet the temperature and humidity predicate
val dsFilter = ds.filter(d => {d.temp > 30 && d.humidity > 70}).take(10).foreach(println(_))
DeviceIoTData(0,1466,US,USA,United States,17,meter-gauge-17zb8Fghhl,98,161.188.212.254,39.95,red,-75.16,Celsius,31,1458444054129) DeviceIoTData(9,986,FR,FRA,France,48,sensor-pad-48jt4eL,97,90.37.208.1,43.88,green,4.9,Celsius,31,1458444054151) DeviceIoTData(8,1436,US,USA,United States,54,sensor-pad-5410CWPrNb6,73,204.15.64.249,32.89,red,-117.13,Celsius,34,1458444054155) DeviceIoTData(4,1090,US,USA,United States,63,device-mac-63GL4xSaZbj,91,66.198.198.1,44.56,yellow,-105.67,Celsius,31,1458444054162) DeviceIoTData(4,1072,PH,PHL,Philippines,81,device-mac-81nsKomrRe,90,222.127.71.1,14.55,yellow,121.04,Celsius,31,1458444054172) DeviceIoTData(3,1076,FR,FRA,France,82,sensor-pad-82HJm6yP,76,213.162.50.33,48.86,yellow,2.35,Celsius,32,1458444054172) DeviceIoTData(9,1221,DE,DEU,Germany,83,meter-gauge-83lLWufdrzWE,96,62.214.32.222,51.0,yellow,9.0,Celsius,31,1458444054173) DeviceIoTData(2,1182,US,USA,United States,108,sensor-pad-108NG6gl2jPi,82,208.35.184.254,34.2,yellow,-118.82,Celsius,34,1458444054187) DeviceIoTData(6,852,US,USA,United States,109,meter-gauge-109PooBS,80,24.29.148.73,38.0,green,-97.0,Celsius,32,1458444054188) DeviceIoTData(4,1188,DK,DNK,Denmark,144,sensor-pad-144T0J4k,87,212.242.41.50,55.68,yellow,12.57,Celsius,31,1458444054211) dsFilter: Unit = ()

Filter out dataset using the high-level and readable method where(). filter() and where() are equivalent.

val dsTemp = ds.where($"temp" > 25).map(d => (d.temp, d.device_name, d.device_id, d.cca3))
dsTemp: org.apache.spark.sql.Dataset[(Long, String, Long, String)] = [_1: bigint, _2: string ... 2 more fields]
display(dsTemp)
34meter-gauge-1xbYRYcj1USA
28sensor-pad-4mzWkz4USA
27sensor-pad-6al7RTAobR6USA
27sensor-pad-8xUD6pzsQI8JPN
26sensor-pad-10BsywSYUF10USA
31meter-gauge-17zb8Fghhl17USA
31sensor-pad-18XULN9Xv18CHN
29meter-gauge-19eg1BpfCO19USA
30device-mac-21sjz5h21AUT
28sensor-pad-24PytzD00Cp24CAN
27therm-stick-25kK6VyzIFB25USA
27sensor-pad-34F1Jubre3B34RUS
34sensor-pad-40NjeMqS40FRA
27sensor-pad-448DeWGL44DEU
26device-mac-45fN2COZF4cM45ITA
29meter-gauge-47WsF9s847UKR
31sensor-pad-48jt4eL48FRA
29sensor-pad-50g2ukc50DEU
34sensor-pad-5410CWPrNb654USA
30sensor-pad-564BJDqo56THA
33meter-gauge-59yhdg6rHM59GBR
27sensor-pad-62fH8oKr8aiT62USA
31device-mac-63GL4xSaZbj63USA
26sensor-pad-707OD4ui6U70USA
26meter-gauge-73MhL2W73USA
32sensor-pad-80TY4dWSMH80CAN
31device-mac-81nsKomrRe81PHL
32sensor-pad-82HJm6yP82FRA
31meter-gauge-83lLWufdrzWE83DEU
27sensor-pad-84jla9J5O84JPN
32sensor-pad-88nm5Thggzs88CHE
28meter-gauge-913q78Z1wkK91SWE
28sensor-pad-92vxuq7e92KOR
28sensor-pad-94HL9jChD94USA
26therm-stick-95LXCTWLh95ECU
33meter-gauge-97K6lrYGS697JPN
29sensor-pad-104Hk2i8104USA
27device-mac-1058nN4WSY9XD105USA
34sensor-pad-108NG6gl2jPi108USA
32meter-gauge-109PooBS109USA
34sensor-pad-110djsARyn110HUN
33sensor-pad-114kolA2Y114JPN
27therm-stick-115CQYYpj115CZE
29device-mac-117mccYyRo117CHN
28sensor-pad-118LhAs6l9HDI118CAN
27sensor-pad-122eSbhvhLPCv122IRL
29device-mac-123zvY7uWFB123USA
26sensor-pad-124b8sCFB124USA
31sensor-pad-1269atdM0jAk126KOR
30sensor-pad-128FCJAk99Zr128GBR
29sensor-pad-130QcD7KHEM130USA
26sensor-pad-1325DdN3NiTBj132EST
27meter-gauge-133ReblmhHY133CZE
26sensor-pad-1362BAqBEt136CHN
27meter-gauge-137Nwxa4DPkwt137TWN
32sensor-pad-142ZlCMJM7142JPN
31sensor-pad-144T0J4k144DNK
27therm-stick-145zkVxKWgx145GBR
28sensor-pad-146YKkPApKAmz146USA
32device-mac-1535rtr4Bw4hD153EGY
32meter-gauge-157XyFxAnJ157GBR
30sensor-pad-158SPYkPKXf158CHN
27meter-gauge-161E8l3Pm161NLD
26sensor-pad-162AFoXZ4wxyj162USA
30sensor-pad-164kRggV164UKR
30sensor-pad-168jsmHIvZoQ168USA
32sensor-pad-1703fywiW170POL
26device-mac-171U5hrN9171GBR
27therm-stick-175pShgFa175CAN
34device-mac-177LUZKzT7M177RUS
33sensor-pad-186cRwjgGbRya186KOR
31sensor-pad-188iU1bREB0188USA
34sensor-pad-192lSdba2iz4192USA
26meter-gauge-193PftCEsRxme193SWE
26sensor-pad-194RmwXLdwEb194USA
27sensor-pad-198i0dgLr198USA
33sensor-pad-200P0imILHSKs200USA
27sensor-pad-208Nr7dPZ208JPN
26sensor-pad-210QLYyxpL210DEU
33sensor-pad-212s5XMzVT212USA
30device-mac-213cQj4kpQNZz213CHN
33therm-stick-215rDovnHVn215RUS
27meter-gauge-221Uo1eW221CHN
32meter-gauge-2231O94o223JPN
30sensor-pad-22615z3yLcov8226BLR
30device-mac-231ZLoopSPtvJ231USA
30meter-gauge-233cgR5RGuUM233UKR
26sensor-pad-240KyHP1kA8q240CAN
32device-mac-243LAI7tApLGv243USA
26sensor-pad-244p6oYcsl79k244MCO
29therm-stick-245hmz8pCLuO245USA
26meter-gauge-247jjoSxwYsW247MDA
30sensor-pad-248354euLBj248GRC
27device-mac-249GzIMESiIOc249CHN
30sensor-pad-250Wd7RtPJ3250CHN
28sensor-pad-252L39t2j252KOR
26meter-gauge-259vEpthIc259DEU
34sensor-pad-262UFel3QHY8262CZE
27therm-stick-265Hz76aIOZ265BEL
26sensor-pad-266wxdoPJgmVJ266USA

Showing the first 1000 rows.

Both where() and map() return a Dataset

Use the filter() method that is equivalent as the where() method used above.

display(ds.filter(d=> {d.temp > 25} ).map(d => (d.temp, d.device_name, d.device_id, d.cca3)))
34meter-gauge-1xbYRYcj1USA
28sensor-pad-4mzWkz4USA
27sensor-pad-6al7RTAobR6USA
27sensor-pad-8xUD6pzsQI8JPN
26sensor-pad-10BsywSYUF10USA
31meter-gauge-17zb8Fghhl17USA
31sensor-pad-18XULN9Xv18CHN
29meter-gauge-19eg1BpfCO19USA
30device-mac-21sjz5h21AUT
28sensor-pad-24PytzD00Cp24CAN
27therm-stick-25kK6VyzIFB25USA
27sensor-pad-34F1Jubre3B34RUS
34sensor-pad-40NjeMqS40FRA
27sensor-pad-448DeWGL44DEU
26device-mac-45fN2COZF4cM45ITA
29meter-gauge-47WsF9s847UKR
31sensor-pad-48jt4eL48FRA
29sensor-pad-50g2ukc50DEU
34sensor-pad-5410CWPrNb654USA
30sensor-pad-564BJDqo56THA
33meter-gauge-59yhdg6rHM59GBR
27sensor-pad-62fH8oKr8aiT62USA
31device-mac-63GL4xSaZbj63USA
26sensor-pad-707OD4ui6U70USA
26meter-gauge-73MhL2W73USA
32sensor-pad-80TY4dWSMH80CAN
31device-mac-81nsKomrRe81PHL
32sensor-pad-82HJm6yP82FRA
31meter-gauge-83lLWufdrzWE83DEU
27sensor-pad-84jla9J5O84JPN
32sensor-pad-88nm5Thggzs88CHE
28meter-gauge-913q78Z1wkK91SWE
28sensor-pad-92vxuq7e92KOR
28sensor-pad-94HL9jChD94USA
26therm-stick-95LXCTWLh95ECU
33meter-gauge-97K6lrYGS697JPN
29sensor-pad-104Hk2i8104USA
27device-mac-1058nN4WSY9XD105USA
34sensor-pad-108NG6gl2jPi108USA
32meter-gauge-109PooBS109USA
34sensor-pad-110djsARyn110HUN
33sensor-pad-114kolA2Y114JPN
27therm-stick-115CQYYpj115CZE
29device-mac-117mccYyRo117CHN
28sensor-pad-118LhAs6l9HDI118CAN
27sensor-pad-122eSbhvhLPCv122IRL
29device-mac-123zvY7uWFB123USA
26sensor-pad-124b8sCFB124USA
31sensor-pad-1269atdM0jAk126KOR
30sensor-pad-128FCJAk99Zr128GBR
29sensor-pad-130QcD7KHEM130USA
26sensor-pad-1325DdN3NiTBj132EST
27meter-gauge-133ReblmhHY133CZE
26sensor-pad-1362BAqBEt136CHN
27meter-gauge-137Nwxa4DPkwt137TWN
32sensor-pad-142ZlCMJM7142JPN
31sensor-pad-144T0J4k144DNK
27therm-stick-145zkVxKWgx145GBR
28sensor-pad-146YKkPApKAmz146USA
32device-mac-1535rtr4Bw4hD153EGY
32meter-gauge-157XyFxAnJ157GBR
30sensor-pad-158SPYkPKXf158CHN
27meter-gauge-161E8l3Pm161NLD
26sensor-pad-162AFoXZ4wxyj162USA
30sensor-pad-164kRggV164UKR
30sensor-pad-168jsmHIvZoQ168USA
32sensor-pad-1703fywiW170POL
26device-mac-171U5hrN9171GBR
27therm-stick-175pShgFa175CAN
34device-mac-177LUZKzT7M177RUS
33sensor-pad-186cRwjgGbRya186KOR
31sensor-pad-188iU1bREB0188USA
34sensor-pad-192lSdba2iz4192USA
26meter-gauge-193PftCEsRxme193SWE
26sensor-pad-194RmwXLdwEb194USA
27sensor-pad-198i0dgLr198USA
33sensor-pad-200P0imILHSKs200USA
27sensor-pad-208Nr7dPZ208JPN
26sensor-pad-210QLYyxpL210DEU
33sensor-pad-212s5XMzVT212USA
30device-mac-213cQj4kpQNZz213CHN
33therm-stick-215rDovnHVn215RUS
27meter-gauge-221Uo1eW221CHN
32meter-gauge-2231O94o223JPN
30sensor-pad-22615z3yLcov8226BLR
30device-mac-231ZLoopSPtvJ231USA
30meter-gauge-233cgR5RGuUM233UKR
26sensor-pad-240KyHP1kA8q240CAN
32device-mac-243LAI7tApLGv243USA
26sensor-pad-244p6oYcsl79k244MCO
29therm-stick-245hmz8pCLuO245USA
26meter-gauge-247jjoSxwYsW247MDA
30sensor-pad-248354euLBj248GRC
27device-mac-249GzIMESiIOc249CHN
30sensor-pad-250Wd7RtPJ3250CHN
28sensor-pad-252L39t2j252KOR
26meter-gauge-259vEpthIc259DEU
34sensor-pad-262UFel3QHY8262CZE
27therm-stick-265Hz76aIOZ265BEL
26sensor-pad-266wxdoPJgmVJ266USA

Showing the first 1000 rows.

Select individual fields using the Dataset method select() where battery_level is greater than 6, sort in ascending order on C02_level. This high-level domain specific language API reads like a SQL query.

display(ds.select($"battery_level", $"c02_level", $"device_name").where($"battery_level" > 6).sort($"c02_level"))
7800device-mac-70947hZuYNtok
7800meter-gauge-89809GpNdEp3aMM
8800meter-gauge-60509Mzj9YApS
9800meter-gauge-179323dfUitIH4u8
8800sensor-pad-5102ZPSPa4Olg
8800meter-gauge-109859eB82jiGE
8800meter-gauge-45413ajpEUtoJjz
9800device-mac-630270gi4y2Z
8800sensor-pad-151562COzl8oo
8800sensor-pad-17905617CdtV7
8800sensor-pad-70948sIVKTWBBk
7800sensor-pad-134798HMJ61LKz
9800meter-gauge-20267DRcaY7
8800sensor-pad-127602zlQ0PYSYpi
9800sensor-pad-23360Y20XgCUzYV
7800sensor-pad-101310Zyq9uy
8800meter-gauge-70190voazFbC
9800device-mac-154917UU1XP7GTj
7800device-mac-73431WI8NSr39aV
8800meter-gauge-98761Rxrdzc5
9800sensor-pad-142282fDTmdvJ
7800sensor-pad-44720kIJ7vx
9800meter-gauge-66787FRXBRixtY9
8800sensor-pad-1093844rfvY
8800sensor-pad-48748vmE7MkXqu
9800sensor-pad-1382463uU6m
8800meter-gauge-148337o50gjrXGEi
8800sensor-pad-75450LgmIGmUA
7800meter-gauge-1252195nuQ0GSi
8800meter-gauge-512894c2QTl
9800meter-gauge-84163ZMQeavt
8800meter-gauge-96443msxkD
7800device-mac-171549aDMEfCiPH
7800sensor-pad-194806J7sArv
8800sensor-pad-62723IqVmwy
7800sensor-pad-75642rYwnMd8Ml
7800device-mac-155337F0m78fHJKl
7800sensor-pad-670080O5FD
9800sensor-pad-128292V6kgzx1j0B
7800sensor-pad-77450aIAJeK
7800sensor-pad-218641sokniqN
9800sensor-pad-15664OXhVT
7800sensor-pad-21942raYXCWl
9800sensor-pad-114720sL1eGm1
7800device-mac-141741tnSSlTg
7800device-mac-156087C4ZyQ1
8800sensor-pad-123994KqDeeH
8800meter-gauge-37261gwzOn
9800therm-stick-700857UzhWGxGK
7800sensor-pad-146902NqACUHQISa
8800sensor-pad-7552ngzRwn5
8800device-mac-218672RVpKvk
9800sensor-pad-107764hc3P2
9800sensor-pad-1116964vFUoUwg
7800meter-gauge-4673NX46gvLZ
8800sensor-pad-131258au5Zw7o
8800device-mac-53529CWnXme
7800sensor-pad-1864985T2DlE87SJ
8800sensor-pad-185138dUJqFY0
9800device-mac-25995tsywBWjb
8800meter-gauge-1396391UpX0M1
9800sensor-pad-62238bIYdDCgZw
8800sensor-pad-83310txFDw
7800sensor-pad-144602MYaTPv
9800device-mac-36441uFJaQeBMv7
7800sensor-pad-33956lIjAk60a
8800sensor-pad-180930t1RHuUQds6
9800meter-gauge-99539mTrCQUvNdX
8801sensor-pad-1685702fPE5fZBgU
8801meter-gauge-186007K91UrX
7801meter-gauge-133943D5oTWv8l
7801meter-gauge-175177WKR0YC
8801meter-gauge-33787rF4dJkr
8801sensor-pad-69934HJCmV
7801sensor-pad-168080u6S5rg
8801sensor-pad-149710w9blB
7801device-mac-1651833rSYHVn
8801meter-gauge-134623Ojg9fme
8801sensor-pad-52740eE3SjeC
8801meter-gauge-102931pBzcH4HfX
7801meter-gauge-72773kv1NGIyR
9801sensor-pad-147554cTo9Sem
9801meter-gauge-91117SZPrGum
9801sensor-pad-143886WB1f9PbDW0
7801sensor-pad-79650soeOBR6D
7801therm-stick-72875aPMfc
8801meter-gauge-94289oGCzKRb
7801device-mac-118827qQAvO7Xkea
9801sensor-pad-126662CuPodL
9801meter-gauge-139321oHer1wN
8801meter-gauge-28747p16dVJ
8801sensor-pad-1164109QiXld0
7801meter-gauge-60823ChtOAnLWx
8801sensor-pad-77652vLgErtw6b4
7801meter-gauge-15883702jALaZ9xL
7801device-mac-81201L049L
7801sensor-pad-514503iRydcFGUF
9801meter-gauge-175651FyBOxON
9801therm-stick-21515QfK6Cvf
9801meter-gauge-67177Znc7R9bLL8

Showing the first 1000 rows.

display(ds.filter(d => d.temp > 25).map(d => (d.temp, d.device_name, d.device_id, d.cca3)))
34meter-gauge-1xbYRYcj1USA
28sensor-pad-4mzWkz4USA
27sensor-pad-6al7RTAobR6USA
27sensor-pad-8xUD6pzsQI8JPN
26sensor-pad-10BsywSYUF10USA
31meter-gauge-17zb8Fghhl17USA
31sensor-pad-18XULN9Xv18CHN
29meter-gauge-19eg1BpfCO19USA
30device-mac-21sjz5h21AUT
28sensor-pad-24PytzD00Cp24CAN
27therm-stick-25kK6VyzIFB25USA
27sensor-pad-34F1Jubre3B34RUS
34sensor-pad-40NjeMqS40FRA
27sensor-pad-448DeWGL44DEU
26device-mac-45fN2COZF4cM45ITA
29meter-gauge-47WsF9s847UKR
31sensor-pad-48jt4eL48FRA
29sensor-pad-50g2ukc50DEU
34sensor-pad-5410CWPrNb654USA
30sensor-pad-564BJDqo56THA
33meter-gauge-59yhdg6rHM59GBR
27sensor-pad-62fH8oKr8aiT62USA
31device-mac-63GL4xSaZbj63USA
26sensor-pad-707OD4ui6U70USA
26meter-gauge-73MhL2W73USA
32sensor-pad-80TY4dWSMH80CAN
31device-mac-81nsKomrRe81PHL
32sensor-pad-82HJm6yP82FRA
31meter-gauge-83lLWufdrzWE83DEU
27sensor-pad-84jla9J5O84JPN
32sensor-pad-88nm5Thggzs88CHE
28meter-gauge-913q78Z1wkK91SWE
28sensor-pad-92vxuq7e92KOR
28sensor-pad-94HL9jChD94USA
26therm-stick-95LXCTWLh95ECU
33meter-gauge-97K6lrYGS697JPN
29sensor-pad-104Hk2i8104USA
27device-mac-1058nN4WSY9XD105USA
34sensor-pad-108NG6gl2jPi108USA
32meter-gauge-109PooBS109USA
34sensor-pad-110djsARyn110HUN
33sensor-pad-114kolA2Y114JPN
27therm-stick-115CQYYpj115CZE
29device-mac-117mccYyRo117CHN
28sensor-pad-118LhAs6l9HDI118CAN
27sensor-pad-122eSbhvhLPCv122IRL
29device-mac-123zvY7uWFB123USA
26sensor-pad-124b8sCFB124USA
31sensor-pad-1269atdM0jAk126KOR
30sensor-pad-128FCJAk99Zr128GBR
29sensor-pad-130QcD7KHEM130USA
26sensor-pad-1325DdN3NiTBj132EST
27meter-gauge-133ReblmhHY133CZE
26sensor-pad-1362BAqBEt136CHN
27meter-gauge-137Nwxa4DPkwt137TWN
32sensor-pad-142ZlCMJM7142JPN
31sensor-pad-144T0J4k144DNK
27therm-stick-145zkVxKWgx145GBR
28sensor-pad-146YKkPApKAmz146USA
32device-mac-1535rtr4Bw4hD153EGY
32meter-gauge-157XyFxAnJ157GBR
30sensor-pad-158SPYkPKXf158CHN
27meter-gauge-161E8l3Pm161NLD
26sensor-pad-162AFoXZ4wxyj162USA
30sensor-pad-164kRggV164UKR
30sensor-pad-168jsmHIvZoQ168USA
32sensor-pad-1703fywiW170POL
26device-mac-171U5hrN9171GBR
27therm-stick-175pShgFa175CAN
34device-mac-177LUZKzT7M177RUS
33sensor-pad-186cRwjgGbRya186KOR
31sensor-pad-188iU1bREB0188USA
34sensor-pad-192lSdba2iz4192USA
26meter-gauge-193PftCEsRxme193SWE
26sensor-pad-194RmwXLdwEb194USA
27sensor-pad-198i0dgLr198USA
33sensor-pad-200P0imILHSKs200USA
27sensor-pad-208Nr7dPZ208JPN
26sensor-pad-210QLYyxpL210DEU
33sensor-pad-212s5XMzVT212USA
30device-mac-213cQj4kpQNZz213CHN
33therm-stick-215rDovnHVn215RUS
27meter-gauge-221Uo1eW221CHN
32meter-gauge-2231O94o223JPN
30sensor-pad-22615z3yLcov8226BLR
30device-mac-231ZLoopSPtvJ231USA
30meter-gauge-233cgR5RGuUM233UKR
26sensor-pad-240KyHP1kA8q240CAN
32device-mac-243LAI7tApLGv243USA
26sensor-pad-244p6oYcsl79k244MCO
29therm-stick-245hmz8pCLuO245USA
26meter-gauge-247jjoSxwYsW247MDA
30sensor-pad-248354euLBj248GRC
27device-mac-249GzIMESiIOc249CHN
30sensor-pad-250Wd7RtPJ3250CHN
28sensor-pad-252L39t2j252KOR
26meter-gauge-259vEpthIc259DEU
34sensor-pad-262UFel3QHY8262CZE
27therm-stick-265Hz76aIOZ265BEL
26sensor-pad-266wxdoPJgmVJ266USA

Showing the first 1000 rows.

Apply higher-level Dataset API methods such as groupBy() and avg(). In order words, take all temperatures readings > 25, along with their corresponding devices' humidity, groupBy ccca3 country code, and compute averages. Plot the resulting Dataset.

display(ds.map(d => (d.temp, d.humidity, d.cca3)).groupBy($"_3").avg())
PSE20.8464.4
HTI25.33333333333333264.58333333333333
POL21.98396501457725862.33163265306123
LVA21.89944134078212263.11173184357542
BRB23.21052631578947358.36842105263158
JAM22.11363636363636363.86363636363637
ZMB23.71428571428571561.142857142857146
BRA21.95812655086848561.96867245657568
ARM21.5882352941176563.23529411764706
MOZ19.5909090909090958.77272727272727
JOR21.06521739130434863.84782608695652
CUB25.86666666666666749.53333333333333
FRA22.11573986804901261.82054665409991
ABW20.7564.75
TCA1738
BRN21.89473684210526465.42105263157895
COD21.37560
FSM2468.33333333333333
URY21.57264957264957462.376068376068375
BOL20.4907407407407460.888888888888886
LBY22.062558.8125
GIB20.55555555555555761.111111111111114
VAT1135
ATG20.93333333333333458.6
ITA21.9406518010291661.90257289879931
UKR21.937154696132663.34254143646409
GHA21.13333333333333360.766666666666666
CMR22.13793103448275865.06896551724138
VIR20.39215686274509762.03921568627451
GTM21.82539682539682662.3968253968254
SEN2363.32
IOT2665
LCA2761.833333333333336
GUY17.845.8
HRV22.85492227979274660.854922279792746
VCT17.45454545454545357.18181818181818
QAT22.36206896551724262.53448275862069
BHS24.2777777777777868.61111111111111
GBR22.08587727412889462.1395312981807
PRY21.8437560.84375
GMB17.42857142857142759.42857142857143
ARE22.21138211382113865.39837398373983
TON1625
FRO21.21428571428571562.57142857142857
CRI22.4568965517241461.724137931034484
TJK2048
BMU22.94339622641509364.64150943396227
NPL22.69696969696969556.666666666666664
UGA20.9696969696969755.90909090909091
VUT27.364
AZE21.4693877551020458.55102040816327
AUS21.90894517473549361.8348829753126
MLI19.63636363636363762.90909090909091
FLK1357
MLT21.4126984126984160.41269841269841
KNA21.56521739130434864.08695652173913
MEX22.13591433278418662.02471169686985
PNG23.2563.6875
BGD21.9934640522875862.790849673202615
AFG24.0526315789473766.6842105263158
DMA24.7307692307692370.46153846153847
YEM20.4210526315789561.36842105263158
BLR22.31707317073170763.333333333333336
MNG22.92307692307692357.48717948717949
SVK22.72131147540983862.42295081967213
SYC20.2545.5
COG2259.333333333333336
HUN21.86787564766839561.95725388601036
REU20.2222222222222256.55555555555556
TKM26.66666666666666869
NZL21.9868708971553661.36323851203501
MAF2348
THA21.8711721224920863.343189017951424
NOR22.077336919973161.118359112306656
IRQ26.42857142857142762.42857142857143
VEN22.38947368421052561.45789473684211
TLS24.33333333333333259
FIN21.95677233429394861.64121037463977
SAU22.04411764705882266.40441176470588
BWA24.573.75
BFA19.12567
DOM20.16666666666666859.291666666666664
ALB20.0937567.21875
TGO20.57142857142857364.28571428571429
BHR21.52727272727272756.25454545454546
NIC22.446808510638367.04255319148936
SUR22.163.9
GGY21.37579.625
BIH21.1792452830188762.632075471698116
KWT21.8560.391666666666666
MWI26.66666666666666859.55555555555556
FJI25.0909090909090956.45454545454545
AND20.2575
MRT2158.42857142857143
PER23.2155172413793160.36206896551724
NLD21.88625401929260462.282154340836016
PAK22.2773437562.44921875
LUX21.05681818181818362
CYM22.92307692307692361.61538461538461
TUR21.87958883994126361.36123348017621

Visualizing datasets

Data without visualization without a narrative arc, to infer insights or to see a trend, is useless. By saving a Dataset as a temporary table, you can issue complex SQL queries against it and visualize the results, using notebook's myriad plotting options.

dsTempDS.createOrReplaceTempView("iot_device_data")
%sql 
select * from iot_device_data
01466USUSAUnited States17meter-gauge-17zb8Fghhl98161.188.212.25439.95red-75.16Celsius311458444054129
9986FRFRAFrance48sensor-pad-48jt4eL9790.37.208.143.88green4.9Celsius311458444054151
81436USUSAUnited States54sensor-pad-5410CWPrNb673204.15.64.24932.89red-117.13Celsius341458444054155
41090USUSAUnited States63device-mac-63GL4xSaZbj9166.198.198.144.56yellow-105.67Celsius311458444054162
41072PHPHLPhilippines81device-mac-81nsKomrRe90222.127.71.114.55yellow121.04Celsius311458444054172
31076FRFRAFrance82sensor-pad-82HJm6yP76213.162.50.3348.86yellow2.35Celsius321458444054172
91221DEDEUGermany83meter-gauge-83lLWufdrzWE9662.214.32.22251yellow9Celsius311458444054173
21182USUSAUnited States108sensor-pad-108NG6gl2jPi82208.35.184.25434.2yellow-118.82Celsius341458444054187
6852USUSAUnited States109meter-gauge-109PooBS8024.29.148.7338green-97Celsius321458444054188
41188DKDNKDenmark144sensor-pad-144T0J4k87212.242.41.5055.68yellow12.57Celsius311458444054211
61201EGEGYEgypt153device-mac-1535rtr4Bw4hD96163.121.192.14630.08yellow31.29Celsius321458444054215
61521GBGBRUnited Kingdom157meter-gauge-157XyFxAnJ8783.146.16.22651.5red-0.13Celsius321458444054217
51215RURUSRussia177device-mac-177LUZKzT7M9062.69.7.155.75yellow37.62Celsius341458444054226
31522KRKORRepublic of Korea186sensor-pad-186cRwjgGbRya88203.246.170.13437.57red126.98Celsius331458444054230
81399RURUSRussia215therm-stick-215rDovnHVn7685.26.238.5355.75yellow37.62Celsius331458444054243
51176CZCZECzech Republic262sensor-pad-262UFel3QHY875217.64.12.1450.08yellow14.42Celsius341458444054268
0931CRCRICosta Rica268sensor-pad-2685AydUae91200.91.105.1710green-84Celsius321458444054270
31200ITITAItaly286sensor-pad-286iRwLlOpR1v8894.47.149.2642.83yellow12.83Celsius341458444054276
41406USUSAUnited States303device-mac-303NtnEHKBNq91216.6.91.1339.74red-75.67Celsius331458444054281
11576CNCHNChina307meter-gauge-307sUHtMY891210.22.154.4031.05red121.4Celsius311458444054283
51170KRKORRepublic of Korea310sensor-pad-310SXQ1w8C82222.107.215.137.57yellow126.98Celsius321458444054284
81297IDIDNIndonesia353meter-gauge-353HMTZmHX93202.78.195.1-6.17yellow106.83Celsius311458444054298
11234USUSAUnited States357device-mac-357L3JNT9666.26.129.11835.22yellow-80.79Celsius341458444054299
41116FRFRAFrance374sensor-pad-374tmp90bW77188.7.34.148.86yellow2.35Celsius341458444054305
7810USUSAUnited States375device-mac-375V9BU5MG78206.128.120.23037.96green-121.26Celsius341458444054305
91043HUHUNHungary380sensor-pad-3803cakh77213.163.52.10647.49yellow19.05Celsius321458444054307
51248USUSAUnited States410sensor-pad-410mm3FeH7668.1.0.13338yellow-97Celsius331458444054316
91546KRKORRepublic of Korea474sensor-pad-474nftAl1at91211.247.149.137.57red126.98Celsius341458444054336
31556CNCHNChina487meter-gauge-4877NPlK95221.5.254.7429.56red106.55Celsius321458444054340
61461CNCHNChina492sensor-pad-492MFWg6x94220.164.69.125.04red102.72Celsius341458444054341
2816ATAUTAustria504sensor-pad-504KdiBKWIN7887.243.151.19347.27green11.4Celsius321458444054344
31517BRBRABrazil520sensor-pad-520Ua4UYBdOa73200.141.252.38-23.55red-46.64Celsius341458444054348
91318AZAZEAzerbaijan533meter-gauge-533HCPvYD84217.14.96.3440.5yellow47.5Celsius341458444054351
91572GBGBRUnited Kingdom547meter-gauge-547pBg3KO8082.138.249.5451.5red-0.13Celsius311458444054355
71350USUSAUnited States573device-mac-573qRwuac9865.77.177.10638yellow-97Celsius321458444054361
71521CACANCanada577meter-gauge-5771uqqa380216.8.137.6642.28red-82.98Celsius331458444054362
4875RURUSRussia605therm-stick-605PPmM891194.190.198.19455.75green37.62Celsius341458444054369
51373CNCHNChina624sensor-pad-624ulkCbj8COG7261.160.71.132.06yellow118.78Celsius311458444054374
91302JPJPNJapan668sensor-pad-668sfSlR88219.113.86.22635.69yellow139.69Celsius321458444054384
3959DEDEUGermany672sensor-pad-672NKhrFOJC7762.154.10.23351green9Celsius311458444054385
4852IDIDNIndonesia682sensor-pad-682iHc9Lg84202.146.128.254-6.17green106.83Celsius331458444054387
31524FRFRAFrance684sensor-pad-684NUMHKy95213.36.80.148.86red2.35Celsius311458444054388
11445RURUSRussia685therm-stick-685KsBPtDFGg71212.188.28.18555.75red37.62Celsius321458444054388
21149RURUSRussia703meter-gauge-703cFaf0td9QJ7562.213.32.17851.54yellow46.01Celsius331458444054392
4857USUSAUnited States715therm-stick-715xiU9vgwXx71216.180.237.1333.73green-84.43Celsius331458444054394
7907USUSAUnited States731meter-gauge-73158eHTQc080205.144.210.134.8green-84.91Celsius331458444054398
11037USUSAUnited States785therm-stick-785pGVAsag98134.139.27.133.78yellow-118.12Celsius341458444054408
61283DEDEUGermany787meter-gauge-787szq1TVrRxu96194.139.39.151yellow9Celsius321458444054408
41571MXMEXMexico792sensor-pad-792kFx0i84148.240.254.4525.67red-100.32Celsius311458444054409
31285USUSAUnited States808sensor-pad-808OcbAvEv2c71206.82.139.3039.74yellow-75.67Celsius311458444054412
91379JPJPNJapan816sensor-pad-816LIJkQ72221.112.14.16135.69yellow139.69Celsius311458444054413
71063AUAUSAustralia830sensor-pad-830vNGUV480121.200.226.19-37.81yellow144.96Celsius321458444054416
81520CNCHNChina854sensor-pad-854SPBFeq72211.136.2.17435red105Celsius311458444054420
11173USUSAUnited States873device-mac-873TkYao7795209.81.10.137.39yellow-122.08Celsius331458444054423
5995CNCHNChina878sensor-pad-878dgegckMv97221.218.160.139.93green116.39Celsius341458444054424
41048USUSAUnited States880sensor-pad-880mhCUcRX886141.218.144.142.27yellow-85.62Celsius311458444054425
81399USUSAUnited States897device-mac-8974FYJ593138.123.138.139.3yellow-75.57Celsius311458444054427
31189CLCHLChile899meter-gauge-8999HhQOMB71200.72.3.142-33.45yellow-70.67Celsius341458444054428
0877KRKORRepublic of Korea915device-mac-915u1MkX93125.134.15.2137.57green126.98Celsius321458444054430
01509AUAUSAustralia939device-mac-939ng1nz8kkH77203.94.135.1-33.84red151.18Celsius331458444054434
0861ARARGArgentina943meter-gauge-943BT5wQ05777200.59.128.19-34.6green-58.38Celsius331458444054435
51496USUSAUnited States968sensor-pad-968muLvQOE8268.172.21.138red-97Celsius341458444054439
01307CACANCanada975device-mac-975Zcm9meJ93198.168.252.9945.45yellow-73.67Celsius331458444054440
81465USUSAUnited States999device-mac-999iFA05vUvT8624.220.1.22643.64red-94.48Celsius341458444054443
51469CNCHNChina1011device-mac-1011Gr2MbbD95221.10.3.7830.67red104.07Celsius331458444054445
61140USUSAUnited States1032sensor-pad-1032WY7Trz8a91128.36.88.141.31yellow-72.93Celsius331458444054449
51324USUSAUnited States1041device-mac-1041WGplXofrK85216.52.93.17838yellow-97Celsius341458444054450
81006USUSAUnited States1060sensor-pad-1060wMhKFq9071.102.66.133.55yellow-115.97Celsius321458444054453
41340UAUKRUkraine1068sensor-pad-10680EU8mpVK89193.239.238.150.45yellow30.52Celsius311458444054454
61449PTPRTPortugal1082sensor-pad-1082IdyLA99213.30.95.13338.71red-9.14Celsius311458444054456
11047USUSAUnited States1112sensor-pad-1112hV4DEmjOjG89206.251.180.829.94yellow-90.07Celsius321458444054461
21369FRFRAFrance1161device-mac-1161o4sMSX89213.228.12.23348.86yellow2.35Celsius321458444054468
6959USUSAUnited States1208sensor-pad-1208l7k8Omo9624.249.36.16238green-97Celsius341458444054475
91075USUSA1217meter-gauge-1217heLo9zG2z795213.248.81.8247yellow8Celsius331458444054477
31387USUSAUnited States1233device-mac-1233NUgNva7DUo7269.58.194.3841.29yellow-96.17Celsius331458444054479
01075DEDEUGermany1241meter-gauge-12414MAYDjD3Lo97193.16.245.151yellow9Celsius341458444054480
5913USUSAUnited States1259meter-gauge-1259y34mBQwNA81209.177.243.130.38green-91.06Celsius321458444054484
21130AUAUSAustralia1278sensor-pad-1278k1OL1IJaKk73203.94.160.1-37.88yellow145.13Celsius321458444054488
6820USUSAUnited States1283meter-gauge-1283AM3yoi89206.81.193.2138green-97Celsius311458444054490
61547HKHKGHong Kong1306sensor-pad-130645On9xd8059.148.197.122.28red114.15Celsius341458444054495
51581SESWESweden1308sensor-pad-1308lHiXEB95194.23.0.159.33red18.07Celsius331458444054496
61272RURUSRussia1314sensor-pad-1314zgkWn1hCY181217.10.37.2155.75yellow37.62Celsius321458444054497
01416CNCHNChina1327meter-gauge-1327ZYIFgSq97218.106.127.21430.58red114.27Celsius341458444054500
71294USUSAUnited States1329device-mac-1329F33uek8966.206.8.347.64yellow-117.1Celsius341458444054500
41485USUSAUnited States1341device-mac-134162cJBci89865.51.155.19738red-97Celsius331458444054502
31178USUSAUnited States1389device-mac-1389yhoLvVjds82129.236.255.1041.02yellow-73.91Celsius321458444054512
7940USUSAUnited States1397meter-gauge-13973RlJbAWz88472.78.244.140.08green-75.29Celsius331458444054513
01586CNCHNChina1410sensor-pad-1410awngNXi91202.97.34.5035red105Celsius311458444054515
51023IDIDNIndonesia1411meter-gauge-1411aOLIk6aLM76111.67.67.69-6.71yellow108.56Celsius341458444054515
0819RURUSRussia1421meter-gauge-1421UdywAu9295.128.224.13055.75green37.62Celsius311458444054517
5890KRKORRepublic of Korea1431device-mac-14316IlJr8361.110.192.14937.57green126.98Celsius321458444054518
0877DEDEUGermany1445therm-stick-14453TR6Wf7Z8992.79.231.13851green9Celsius341458444054520
21162RURUSRussia1452sensor-pad-1452TkZ73cLC3v8382.196.95.23455.75yellow37.62Celsius341458444054521
71140KRKORRepublic of Korea1454sensor-pad-1454iQWzA77124.61.162.137.57yellow126.98Celsius341458444054521
81240DEDEUGermany1457meter-gauge-1457LBEi8DzS9Z97195.60.208.12751yellow9Celsius331458444054522
11011KGKGZKyrgyzstan1474sensor-pad-1474KilQz8085.115.220.141yellow75Celsius331458444054524
61188IDIDNIndonesia1481meter-gauge-1481q7ehfQ78202.152.7.81-6.17yellow106.83Celsius331458444054525
21525HKHKGHong Kong1517meter-gauge-1517xSSaN88Z77116.0.82.11022.28red114.15Celsius311458444054531
01518USUSA1521device-mac-1521KUBAw873130.117.2.10647red8Celsius331458444054531
21497TWTWNTaiwan1530sensor-pad-1530X7iAneyUG172220.128.1.18223.5red121Celsius321458444054538

Showing the first 1000 rows.

Count all devices for a particular country and map them.

%sql select cca3, count(device_id) as number, avg(humidity), avg(temp) from iot_device_data group by cca3 order by number desc limit 20
+More than two numerical columns. Only the values of the first one will be shown.N/A0-2020-4040-6060-80