Data streams are often processed in a distributed manner using multiple machines or multiple processes. For example, a data stream may be produced by a sensor attached to a remote machine or multiple clustering algorithms run in parallel using several R processes. Another application is to connect to other software components in a stream mining pipeline.
First, we show how socket connections together with the package stream
can be used to connect multiple processes or machines.
Then we give examples of how package streamConnect
makes connecting stream mining components more convenient by providing an interface to connect stream processing using sockets or web services. While sockets are only used to connect data steam generating processes, web services are more versatile and can also be used to create data stream clustering processes as a service.
The last section of this paper shows how to deploy the server/web service.
For the examples below, we will use a random available port.
httpuv::randomPort()
port <- port
## [1] 9275
The functions write_stream()
and the class DSD_ReadStream
provided in package stream
can be used for communicate via connections (files, sockets, URLs, etc.). In the first example, we manually set up the connection. The example is useful to understand how sockets work especially for users interested in implementing their own components using other programming languages or connecting with other data stream software.
A more convenient way to do this using package streamConnect
is described later in this paper.
The server served data from a data stream. We use library callr
to create a separate R process that serves a data stream creating 10 points every second using a socket connection, but you can also put the code in function r_bg()
in a file called server.R
and run (potentially on a different machine) it with R CMD BATCH server.R
from the command line.
library(stream)
## Loading required package: magrittr
library(callr)
r_bg(function(port) {
rp1 <-library(stream)
DSD_Gaussians(k = 3, d = 3)
stream <- 10
blocksize <-
socketConnection(port = port, server = TRUE)
con <-
while (TRUE) {
write_stream(stream, con, n = blocksize, close = FALSE)
Sys.sleep(1)
}
close(con)
}, args = list(port = port))
rp1
## PROCESS 'R', running, pid 61787.
Sys.sleep(1) # wait for the socket to establish
The client consumes the data stream. We open the connection which starts the data generating process and then poll all available data (n = -1
) several times. The first request should yield 10 points, the second none and the third request should yield 20 points (2 seconds).
library(stream)
socketConnection(port = port, open = 'r')
con <- con
## A connection with
## description "->localhost:9275"
## class "sockconn"
## mode "r"
## text "text"
## opened "opened"
## can read "yes"
## can write "yes"
Sys.sleep(1) # wait for the socket to establish
DSD_ReadStream(con)
dsd <-
get_points(dsd, n= -1)
## V1 V2 V3
## 1 0.9047997 0.52186379 0.8044719
## 2 0.9050244 0.41133225 0.8632416
## 3 0.4329269 -0.02751129 0.2450066
## 4 0.4248826 0.03066694 0.2567322
## 5 0.5164283 -0.01551262 0.3242230
## 6 0.8728699 0.34272960 0.9187377
## 7 0.9732045 0.45537605 0.5398918
## 8 0.4370118 0.02743836 0.3350599
## 9 0.4059312 0.04516246 0.2762548
## 10 0.9195888 0.47762344 0.5571384
## 11 0.9549011 0.51472257 0.7915742
## 12 1.0505180 0.40592178 0.5436453
## 13 0.9524608 0.40431083 0.5542186
## 14 0.4690362 -0.02688353 0.2884523
## 15 0.9052682 0.47235297 0.5417151
## 16 0.4875795 0.06994513 0.2723085
## 17 0.9820347 0.46710826 0.4692987
## 18 0.8723740 0.47481997 0.8529258
## 19 0.8783201 0.46657375 0.5114071
## 20 0.9839075 0.44890884 0.4434689
get_points(dsd, n= -1)
## [1] V1 V2 V3
## <0 rows> (or 0-length row.names)
Sys.sleep(2)
get_points(dsd, n= -1)
## V1 V2 V3
## 1 0.8826423 0.428505269 0.8469474
## 2 0.5097568 -0.059866288 0.3016689
## 3 0.9036892 0.488034415 0.5593271
## 4 0.8516482 0.443432710 0.8168350
## 5 0.4468856 0.058070923 0.2736163
## 6 0.4285829 -0.009003735 0.3212047
## 7 0.5338878 0.007341443 0.2301360
## 8 0.9326579 0.456050486 0.5972693
## 9 0.9407112 0.464927505 0.5263519
## 10 1.0352548 0.408376150 0.6474200
close(con)
Here we stop the callr
process. Note that the socket connection is still active and will serve the data in the connection buffer as long as the reading process keeps the connection open.
$kill() rp1
## [1] TRUE
streamConnect
provides publish DSD_via_Socket()
and the class DSD_ReadSocket
to create a socked based connection. DSD_via_Socket()
creates a socket broadcasting data using a socket and DSD_ReadSocket
creates a DSD
object reading from that socket.
We create a DSD process sending data to the port.
library(streamConnect)
DSD_Gaussians(k = 3, d = 3) %>% publish_DSD_via_Socket(port = port)
rp1 <- rp1
## PROCESS 'R', running, pid 61845.
Sys.sleep(1) # wait for the socket to become available
Next, we create a DSD that connects to the socket.
library(streamConnect)
DSD_ReadSocket(port = port, col.names = c("x", "y", "z", ".class"))
dsd <- dsd
## Data Stream from Connection (d = 3, k = NA)
## Class: DSD_ReadStream, DSD_R, DSD
## connection: ->localhost:9275 (opened)
get_points(dsd, n = 10)
## x y z .class
## 1 0.7567607 0.8319033 0.05513106 3
## 2 0.9360300 0.1039441 0.40642467 2
## 3 0.8364580 0.2234238 0.43907727 2
## 4 0.7796039 0.8566099 0.08602838 3
## 5 0.9389026 0.1361315 0.46070560 2
## 6 0.2492437 0.3462209 0.74175057 1
## 7 0.2794318 0.4106671 0.76771604 1
## 8 0.9500622 0.1724914 0.40171971 2
## 9 0.8426019 0.1895990 0.46195236 2
## 10 0.7438122 0.8188341 0.04061998 3
plot(dsd)
close_stream(dsd)
Closing the stream on the client also closes the connection which may already kill the serving process.
if (rp1$is_alive()) rp1$kill()
Web services are more versatile, they can be used to deploy data stream sources using publish_DSD_via_WebService()
/DSD_ReadWebservice
or data stream tasks using publish_DSC_via_WebService()
/DSC_WebService
. Here we only show how to deploy a clusterer, but a DSD can be published in a similar manner. Larger workflows can be created using DST_Runner
from stream
.
streamConnect
uses the package plumber
to manage web services. The data is transmitted in serialized form. The default serialization format it csv
(comma separated values). Other formats are json
and rds
(see plumber::serializer_csv
).
Creating a clustering web service process listening for data on the port.
library(streamConnect)
publish_DSC_via_WebService("DSC_DBSTREAM(r = .05)", port = port)
rp1 <- rp1
## PROCESS 'R', running, pid 61921.
Connect to the web service with a local DSC interface.
library(streamConnect)
DSC_WebService(paste0("http://localhost", ":", port), quiet = FALSE) dsc <-
## Error in curl::curl_fetch_memory(url, handle = handle): Failed to connect to localhost port 9275 after 0 ms: Connection refused
## Request failed [ERROR]. Retrying in 1.7 seconds...
dsc
## Web Service Data Stream Clusterer: DBSTREAM
## Served from: http://localhost:9275
## Class: DSC_WebService, DSC_R, DSC
## Number of micro-clusters: 0
## Number of macro-clusters: 0
Cluster some data.
DSD_Gaussians(k = 3, d = 2, noise = 0.05)
dsd <-
update(dsc, dsd, 500)
dsc
## Web Service Data Stream Clusterer: DBSTREAM
## Served from: http://localhost:9275
## Class: DSC_WebService, DSC_R, DSC
## Number of micro-clusters: 18
## Number of macro-clusters: 3
get_centers(dsc)
## # A tibble: 18 × 2
## X1 X2
## <dbl> <dbl>
## 1 0.495 0.203
## 2 0.135 0.670
## 3 0.0940 0.643
## 4 0.0699 0.599
## 5 0.548 0.227
## 6 0.806 0.463
## 7 0.531 0.171
## 8 0.467 0.153
## 9 0.0266 0.582
## 10 0.767 0.435
## 11 0.819 0.412
## 12 0.748 0.393
## 13 0.789 0.371
## 14 0.840 0.494
## 15 0.236 0.771
## 16 0.605 0.270
## 17 0.167 0.715
## 18 0.886 0.494
get_weights(dsc)
## [1] 59.784471 57.632239 72.168699 54.786610 59.068051 73.338511 66.109499
## [8] 25.806733 25.471136 82.406985 60.287946 39.230393 33.319079 19.059312
## [15] 3.318702 4.355685 11.803871 6.558004
plot(dsc)
Kill the web service process.
$kill() rp1
## [1] TRUE
Web services and the socket-based server can be easily deployed to any server or cloud system including containers. Make sure R and the package streamConnect
and all dependencies are installed. Create a short R script to start the server/service and deploy it.
library(streamConnect)
8001
port =
publish_DSC_via_WebService("DSC_DBSTREAM(r = .05)", port = port,
background = FALSE)
Web services can also be deployed using a plumber task file. The following call returns the name of the task file.
publish_DSC_via_WebService("DSC_DBSTREAM(r = .05)", serve = FALSE)
Open the file in R studio to deploy it or read the plumber Hosting vignette.