gpt4 book ai didi

java - 使用 Akka Actor 的文件操作

转载 作者:行者123 更新时间:2023-12-02 17:11:41 25 4
gpt4 key购买 nike

与普通的文件操作方法相比,使用 Akka Actor 有什么优势?。我试图计算分析日志文件所花费的时间。操作是找出登录次数超过50次的IP地址并显示出来。与 Akka Actor 模型相比,普通文件操作更快。为什么会这样?

使用普通的文件操作

public static void main(String[] args) {
// TODO Auto-generated method stub
//long startTime = System.currentTimeMillis();
File file = new File("log.txt");
Map<String, Long> ipMap = new HashMap<>();

try {

FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();

while(line!=null) {
int idx = line.indexOf('-');
String ipAddress = line.substring(0, idx).trim();
long count = ipMap.getOrDefault(ipAddress, 0L);
ipMap.put(ipAddress, ++count);
line = br.readLine();
}

System.out.println("================================");
System.out.println("||\tCount\t||\t\tIP");
System.out.println("================================");

fr.close();
br.close();
Map<String, Long> result = new HashMap<>();

// Sort by value and put it into the "result" map
ipMap.entrySet().stream()
.sorted(Map.Entry.<String, Long>comparingByValue().reversed())
.forEachOrdered(x -> result.put(x.getKey(), x.getValue()));

// Print only if count > 50
result.entrySet().stream().filter(entry -> entry.getValue() > 50).forEach(entry ->
System.out.println("||\t" + entry.getValue() + " \t||\t" + entry.getKey())
);

// long endTime = System.currentTimeMillis();
// System.out.println("Time: "+(endTime-startTime));

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

Using Actors:
1. The Main Class
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
// Create actorSystem
ActorSystem akkaSystem = ActorSystem.create("akkaSystem");

// Create first actor based on the specified class
ActorRef coordinator = akkaSystem.actorOf(Props.create(FileAnalysisActor.class));

// Create a message including the file path
FileAnalysisMessage msg = new FileAnalysisMessage("log.txt");

// Send a message to start processing the file. This is a synchronous call using 'ask' with a timeout.
Timeout timeout = new Timeout(6, TimeUnit.SECONDS);
Future<Object> future = Patterns.ask(coordinator, msg, timeout);

// Process the results
final ExecutionContext ec = akkaSystem.dispatcher();
future.onSuccess(new OnSuccess<Object>() {
@Override
public void onSuccess(Object message) throws Throwable {
if (message instanceof FileProcessedMessage) {
printResults((FileProcessedMessage) message);

// Stop the actor system
akkaSystem.shutdown();
}
}

private void printResults(FileProcessedMessage message) {
System.out.println("================================");
System.out.println("||\tCount\t||\t\tIP");
System.out.println("================================");

Map<String, Long> result = new LinkedHashMap<>();

// Sort by value and put it into the "result" map
message.getData().entrySet().stream()
.sorted(Map.Entry.<String, Long>comparingByValue().reversed())
.forEachOrdered(x -> result.put(x.getKey(), x.getValue()));

// Print only if count > 50
result.entrySet().stream().filter(entry -> entry.getValue() > 50).forEach(entry ->
System.out.println("||\t" + entry.getValue() + " \t||\t" + entry.getKey())
);
long endTime = System.currentTimeMillis();
System.out.println("Total time: "+(endTime - startTime));
}

}, ec);

}

2.文件分析器类

public class FileAnalysisActor extends UntypedActor {

private Map<String, Long> ipMap = new HashMap<>();
private long fileLineCount;
private long processedCount;
private ActorRef analyticsSender = null;

@Override
public void onReceive(Object message) throws Exception {
/*
This actor can receive two different messages, FileAnalysisMessage or LineProcessingResult, any
other type will be discarded using the unhandled method
*/
//System.out.println(Thread.currentThread().getName());
if (message instanceof FileAnalysisMessage) {

List<String> lines = FileUtils.readLines(new File(
((FileAnalysisMessage) message).getFileName()));

fileLineCount = lines.size();
processedCount = 0;

// stores a reference to the original sender to send back the results later on
analyticsSender = this.getSender();

for (String line : lines) {
// creates a new actor per each line of the log file
Props props = Props.create(LogLineProcessor.class);
ActorRef lineProcessorActor = this.getContext().actorOf(props);

// sends a message to the new actor with the line payload
lineProcessorActor.tell(new LogLineMessage(line), this.getSelf());
}

} else if (message instanceof LineProcessingResult) {

// a result message is received after a LogLineProcessor actor has finished processing a line
String ip = ((LineProcessingResult) message).getIpAddress();

// increment ip counter
Long count = ipMap.getOrDefault(ip, 0L);
ipMap.put(ip, ++count);

// if the file has been processed entirely, send a termination message to the main actor
processedCount++;
if (fileLineCount == processedCount) {
// send done message
analyticsSender.tell(new FileProcessedMessage(ipMap), ActorRef.noSender());
}

} else {
// Ignore message
this.unhandled(message);
}
}
}

3.Logline处理器类

public class LogLineProcessor extends UntypedActor {

@Override
public void onReceive(Object message) throws Exception {
if (message instanceof LogLineMessage) {
// What data each actor process?
//System.out.println("Line: " + ((LogLineMessage) message).getData());
// Uncomment this line to see the thread number and the actor name relationship
//System.out.println("Thread ["+Thread.currentThread().getId()+"] handling ["+ getSelf().toString()+"]");

// get the message payload, this will be just one line from the log file
String messageData = ((LogLineMessage) message).getData();

int idx = messageData.indexOf('-');
if (idx != -1) {
// get the ip address
String ipAddress = messageData.substring(0, idx).trim();

// tell the sender that we got a result using a new type of message
this.getSender().tell(new LineProcessingResult(ipAddress), this.getSelf());
}
} else {
// ignore any other message type
this.unhandled(message);
}
}
}

消息类

  1. 文件分析消息

    公共(public)类 FileAnalysisMessage {

    private String fileName;

    public FileAnalysisMessage(String file) {
    this.fileName = file;
    }

    public String getFileName() {
    return fileName;
    }

2.文件处理消息

public class FileProcessedMessage {

private Map<String, Long> data;

public FileProcessedMessage(Map<String, Long> data) {
this.data = data;
}

public Map<String, Long> getData() {
return data;
}
}
  1. 线处理结果

    公共(public)类 LineProcessingResult {

    private String ipAddress;

    public LineProcessingResult(String ipAddress) {
    this.ipAddress = ipAddress;
    }

    public String getIpAddress() {
    return ipAddress;
    }

4.日志消息

public class LogLineMessage {

private String data;

public LogLineMessage(String data) {
this.data = data;
}

public String getData() {
return data;
}
}

我正在为文件中的每一行创建一个 Actor 。

最佳答案

对于所有并发框架,部署的并发数量与每个并发单元所涉及的复杂性之间始终存在权衡。 Akka 也不异常(exception)。

在您的非 akka 方法中,每行的步骤序列相对简单:

  1. 从文件中读取一行
  2. 用'-'分隔行
  3. 将 ip 地址提交到 HashMap 中并增加计数

相比之下,您的 akka 方法每一行都复杂得多:

  1. 创建一个 Actor
  2. 创建一条 LogLineMessage 消息
  3. 将消息发送给 Actor
  4. 用'-'分隔行
  5. 创建一个LineProcessingResult 消息
  6. 将消息发送回协调参与者
  7. 将 ip 地址提交到 HashMap 中并增加计数

如果我们天真地假设上述每个步骤花费相同的时间,那么您将需要 2 个带有 akka 的线程才能以与没有 akka 的 1 个线程相同的速度运行。

让每个并发单元做更多的工作

不是每 1 行有 1 个 Actor,而是让每个 actor 将 N 行处理到它自己的子 HashMap 中(例如,每个 Actor 处理 1000 行):

public class LogLineMessage {

private String[] data;

public LogLineMessage(String[] data) {
this.data = data;
}

public String[] getData() {
return data;
}
}

那么 Actor 就不会发回像 IP 地址这样简单的东西了。相反,它将为其行子集发送计数哈希:

public class LineProcessingResult {

private HashMap<String, Long> ipAddressCount;

public LineProcessingResult(HashMap<String, Long> count) {
this.ipAddressCount = Count;
}

public HashMap<String, Long> getIpAddress() {
return ipAddressCount;
}
}

并且协调 Actor 可以负责组合所有不同的子计数:

//inside of FileAnalysisActor
else if (message instanceof LineProcessingResult) {
HashMap<String,Long> localCount = ((LineProcessingResult) message).getIpAddressCount();

localCount.foreach((ipAddress, count) -> {
ipMap.put(ipAddress, ipMap.getOrDefault(ipAddress, 0L) + count);
})

然后您可以改变 N 以查看您在特定系统中获得最佳性能的位置。

不要将整个文件读入内存

并发解决方案的另一个缺点是它首先将整个文件读入内存。这对 JVM 来说是不必要的和繁重的。

相反,一次读取文件 N 行。一旦你在内存中有了这些行,就会像前面提到的那样从 Actor 中产生。

FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);

String[] lineBuffer;
int bufferCount = 0;
int N = 1000;

String line = br.readLine();

while(line!=null) {
if(0 == bufferCount)
lineBuffer = new String[N];
else if(N == bufferCount) {
Props props = Props.create(LogLineProcessor.class);
ActorRef lineProcessorActor = this.getContext().actorOf(props);

lineProcessorActor.tell(new LogLineMessage(lineBuffer),
this.getSelf());

bufferCount = 0;
continue;
}

lineBuffer[bufferCount] = line;
br.readLine();
bufferCount++;
}

//handle the final buffer
if(bufferCount > 0) {
Props props = Props.create(LogLineProcessor.class);
ActorRef lineProcessorActor = this.getContext().actorOf(props);

lineProcessorActor.tell(new LogLineMessage(lineBuffer),
this.getSelf());
}

这将允许文件 IO、线处理和子图组合全部并行运行。

关于java - 使用 Akka Actor 的文件操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48902089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com