Comments (4)
package JavaRecieverTest
import com.google.common.io.Closeables;
import org.apache.spark.SparkConf;
import org.apache.spark.storage.StorageLevel;
import org.apache.spark.streaming.Duration;
import org.apache.spark.streaming.api.java.JavaReceiverInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import org.apache.spark.streaming.receiver.Receiver;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
/**
-
Implement Receiver for any customer resources
*/
public class TestJavaReceiver extends Receiver{
public static void main(String[] args){
if(args.length < 2){
System.err.println("Usage: TestJavaReceiver ");
System.exit(1);
}
}//create context
SparkConf sparkConf = new SparkConf().setAppName("TestJavaReceiver");
JavaStreamingContext jsc = new JavaStreamingContext(sparkConf, new Duration(1000));//1 second//Create a receiver inputstream based on provided IP port
//lines is the input data from any resources
JavaReceiverInputDStream lines = jsc.receiverStream(
new TestJavaReceiver(args[0], Integer.parseInt(args[1]))
); //A Java-friendly interface to ReceiverInputDStream, the abstract class for defining any input stream that receives data over the network./**After receiving data, we could do a lot of thing here.
- Such as map them;
- and process them.
*/
//define the input originals
String host = null;
int port = -1;public TestJavaReceiver(String host, int port){
super(StorageLevel.MEMORY_AND_DISK_SER());//replicate data into 2 cluster seems useless
this.host = host;
this.port = port;
}/**
- Start threads when receiving data over a connection
*/
public void onStart(){
new Thread(){
public void run(){
receive();
}
}.start();
}
//The thread will stop itself in receive() function
// public void onStop() {}/**
- Create a socket connection and receive data streams
/
private void receive(){
Socket socket = null;
BufferedReader reader = null;
String userInput = null;
try{
try{
//connect to data sources
socket = new Socket(host, port);
//read data sources as UTF-8 charset
reader = new BufferReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
//Until stopped or connection broken down
while(!isStopped() && (userInput = reader.readLine()) != null){
store(userInput);//Receiver's built-in function
}
}finally{
Closeables.close(reader, / swallowIOException / true);
Closeables.close(socket, / swallowIOException */ true);
}
//restart to try again
restart("try to connect sources again");
}catch(ConnectException cex){
//restart connection
restart("Could not connect data source", cex);
}catch(Throwable t){
//restart connection
restart("Error receiving data",t);
}
}
public JavaReceiverInputDStream getData(){
return this.lines;
}
}
from openxdr.
Hi, after finishing my thesis. I wrote one example here. The receiver retrieve data sources from user's defined ip and port. It originates from Spark document:http://spark.apache.org/docs/latest/streaming-custom-receivers.html. But I made some changes.
from openxdr.
Yes, the abstract implementation should depend on spark custom receivers, You can extend the basic 'Receiver' to support various data sources OpenADS support.
from openxdr.
@xiaoleihuang Please add this code under 'feature/receiver' branch and send a pull-request to merge to develop, then merge it with 'feature/receiver-pcap' and develop your pcap code
from openxdr.
Related Issues (12)
- List down all possible data sources to consider for OpenDS HOT 7
- What is the data-structure to be considered for identified data points? HOT 1
- Perform a comprehensive literature study of network anomaly detection models in large scale enviornments HOT 4
- Evaluate Succinct to use Spark as a document store HOT 5
- Design mock dashboard for OpenADS HOT 2
- Implement custom receiver model to extract syslog data HOT 1
- Repo should follow GitFlow workflow
- Create the Display Window
- Initializing the remote command execution app
- Adding log collector
- Refactor http endpoints to websocket connection
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from openxdr.