sparkStream在shell的启用
使用spark监听scoket的9999端口这个相当于client
import org.apache.spark._
import org.apache.spark.streaming._
import org.apache.spark.streaming.StreamingContext._
val ssc = new StreamingContext(sc, Seconds(12))//设置下处理的监听的时间
val lines = ssc.socketTextStream("sicong.localdomain", 9999)//使用hostname来查看本机名
val words = lines.flatMap(_.split(" "))
import org.apache.spark.streaming.StreamingContext._
val pairs = words.map(word => (word, 1))
val wordCounts = pairs.reduceByKey(_ + _)
ssc.start()
ssc.awaitTermination()
发送端
h h h h hh h hh h h h h uuu u u u u uu u u uu u
处理的结果
(Content-Type:,1)
(hh,2)
(h,9)
(,4)
(uu,2)
(text/plain,1)
(u,7)
(uuu,1)
php scoketServer
set_time_limit(0);
$socket = stream_socket_server("tcp://sicong.localdomain:9999", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)<br />\n";
} else {
$in = fopen('php://stdin', 'r');//启用控制端接受数据
while ($conn = stream_socket_accept($socket)) {
while ( true) {
$messages = explode(',', fgets($in));
foreach (array_keys($messages) as $k) {
//$messages[$k] = trim($messages[$k]);
}
fwrite($conn, "Content-Type: text/plain\n\n");
var_dump($messages);
//将控制端的数据写出
fwrite($conn, $messages[0]);
}
}
fclose($conn);
fclose($socket);
}