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 final section of this paper shows how to deploy the server/web service.
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.
For we find an available port.
httpuv::randomPort()
port <- port
## [1] 43624
The server serves 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 194491.
The client consumes the data stream. We open the connection which starts the data generating process. Note that streamConnect
is not used here. For convenience, we only use the helper retry()
defined in streamConnect to make sure the server connections are established.
streamConnect::retry(socketConnection(port = port, open = 'r'))
con <- con
## A connection with
## description "->localhost:43624"
## class "sockconn"
## mode "r"
## text "text"
## opened "opened"
## can read "yes"
## can write "yes"
streamConnect::retry(DSD_ReadStream(con)) dsd <-
We 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).
get_points(dsd, n= -1)
## V1 V2 V3
## 1 0.1689110 0.8333214 0.07096888
## 2 0.4581580 0.2370112 0.64971780
## 3 0.5893644 0.9363182 0.97720440
## 4 0.4563825 0.2828736 0.61750396
## 5 0.3748784 0.2296728 0.66696569
## 6 0.5884456 0.9957330 1.00788978
## 7 0.1222034 0.8428789 0.11666528
## 8 0.1598287 0.8540922 0.06919708
## 9 0.6311839 0.9276826 0.98396917
## 10 0.4468843 0.2894998 0.60271367
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.6298902 0.8752374 0.96267006
## 2 0.1063677 0.8539092 0.14164125
## 3 0.5304545 0.9546835 1.04637631
## 4 0.1599019 0.8925179 0.07167097
## 5 0.4035567 0.3088145 0.57232773
## 6 0.6181698 0.9688666 0.98643527
## 7 0.6036740 0.9650260 0.98840184
## 8 0.5532671 0.9579103 1.03752260
## 9 0.3497405 0.2479782 0.65297676
## 10 0.1201102 0.8777870 0.10910942
## 11 0.1137567 0.8341284 0.14595475
## 12 0.6453987 0.8752888 0.97804710
## 13 0.6025805 0.8804477 1.01443668
## 14 0.4071709 0.2465887 0.61698004
## 15 0.6119926 0.9028206 1.00535371
## 16 0.1681226 0.8823828 0.08167311
## 17 0.4413103 0.3080875 0.62240146
## 18 0.1093375 0.8546032 0.11042619
## 19 0.6450648 0.9371922 0.99771222
## 20 0.6359681 0.8678153 0.97965674
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 a more convenient way to set up a connection using sockets. publish_DSD_via_Socket()
creates a socket broadcasting the data stream and DSD_ReadSocket
creates a DSD
object reading from that socket.
We will use an available port.
httpuv::randomPort()
port <- port
## [1] 31413
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 194565.
Next, we create a DSD that connects to the socket. DSD_ReadSocket()
already performs internally retries
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:31413 (opened)
get_points(dsd, n = 10)
## x y z .class
## 1 0.3038707 0.3485553 0.7469510 1
## 2 0.6703497 0.7725840 0.5625436 3
## 3 0.2790565 0.3537149 0.7526760 1
## 4 0.1135079 0.2819303 0.4905892 2
## 5 0.3560995 0.3122839 0.7436765 1
## 6 0.2551160 0.3504957 0.7785844 1
## 7 0.1231861 0.2560017 0.4603403 2
## 8 0.1600843 0.2628107 0.3888445 2
## 9 0.3079750 0.3618152 0.7407862 1
## 10 0.7235221 0.8470806 0.5481081 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
).
We will use an available port.
httpuv::randomPort()
port <- port
## [1] 25145
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 194636.
Connect to the web service with a local DSC interface.
library(streamConnect)
DSC_WebService(paste0("http://localhost", ":", port),
dsc <-verbose = TRUE, config = httr::verbose(info = TRUE))
## Connecting to DSC Web service at http://localhost:25145
## Success
dsc
## Web Service Data Stream Clusterer: DBSTREAM
## Served from: http://localhost:25145
## Class: DSC_WebService, DSC_R, DSC
## Number of micro-clusters: 0
## Number of macro-clusters: 0
Note that the verbose output can help with debugging connection issues.
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:25145
## Class: DSC_WebService, DSC_R, DSC
## Number of micro-clusters: 24
## Number of macro-clusters: 2
get_centers(dsc)
## # A tibble: 24 × 2
## X1 X2
## <dbl> <dbl>
## 1 0.753 0.117
## 2 0.717 0.552
## 3 0.706 0.155
## 4 0.887 0.688
## 5 0.919 0.644
## 6 0.680 0.587
## 7 0.907 0.763
## 8 0.672 0.526
## 9 0.638 0.225
## 10 0.771 0.733
## # ℹ 14 more rows
get_weights(dsc)
## [1] 62.761348 31.849930 96.817280 60.296632 25.812328 48.492974 8.319457
## [8] 43.457054 13.293918 9.720636 55.455490 12.445026 28.923028 31.099905
## [15] 43.381493 7.989769 45.589219 16.270779 6.892395 13.375400 7.324363
## [22] 13.187773 10.184400 3.907151
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 does not create a server, but 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.