-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDataGenerator.scala
66 lines (59 loc) · 2.16 KB
/
DataGenerator.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.io.{ByteArrayOutputStream, IOException}
import java.net.ServerSocket
import java.nio.ByteBuffer
import scala.io.Source
import org.apache.spark.{SparkConf, Logging}
import org.apache.spark.serializer.KryoSerializer
import org.apache.spark.util.IntParam
/**
* A helper program that sends blocks of Kryo-serialized text strings out on a socket at a
* specified rate. Used to feed data into RawInputDStream.
*
* Files of the required line length can be generated by this two lines in the Spark-shell
*
* val text = (1 to 1000).map(x => { (1 to 15).map(_ => scala.util.Random.alphanumeric.take(6).mkString("")).mkString(" ") }).mkString("\n")
* org.apache.commons.io.FileUtils.writeStringToFile(new java.io.File("test.txt"), text)
*
*/
object DataGenerator {
def main(args: Array[String]) {
if (args.length != 3) {
System.err.println("Usage: RawTextSender <port> <file> <bytesPerSec>")
System.exit(1)
}
// Parse the arguments using a pattern match
val (port, file, bytesPerSec) = (args(0).toInt, args(1), args(2).toInt)
val blockSize = bytesPerSec / 10
// Repeat the input data multiple times to fill in a buffer
val lines = Source.fromFile(file).getLines().toArray
val bufferStream = new ByteArrayOutputStream(blockSize + 1000)
val ser = new KryoSerializer(new SparkConf()).newInstance()
val serStream = ser.serializeStream(bufferStream)
var i = 0
while (bufferStream.size < blockSize) {
serStream.writeObject(lines(i))
i = (i + 1) % lines.length
}
val array = bufferStream.toByteArray
val countBuf = ByteBuffer.wrap(new Array[Byte](4))
countBuf.putInt(array.length)
countBuf.flip()
val serverSocket = new ServerSocket(port)
println("Listening on port " + port)
while (true) {
val socket = serverSocket.accept()
println("Got a new connection")
val out = new RateLimitedOutputStream(socket.getOutputStream, bytesPerSec)
try {
while (true) {
out.write(countBuf.array)
out.write(array)
}
} catch {
case e: IOException =>
println("Client disconnected")
socket.close()
}
}
}
}