RabbitMQ 和 F# - 如何 'read' MQ 消息并将内容拆分为变量或数组?

RabbitMQ and F# - How to 'read' MQ messages and split content into Variables or an Array?

希望你一切都好。我是 F# 和编程新手,我需要一些关于从 RabbitMQ 消息队列将数据读入 F# 的问题的指导。

我已经能够编写一个 F# 脚本来订阅消息队列并以与下面类似的格式接收数据。

Received onData;FeedQ1;7.488;7.48813;1427315602762;8.61;8.31

我现在需要学习如何将从消息队列接收到的 'string' 消息拆分为变量或数组,然后可以将其存储 and/or 在我的 F# 脚本中使用。如您所见,上面的消息实际上包含 7 个字段,这些字段由 ; 个字符分隔。

我曾尝试搜索网络和 RabbitMQs 资源,但我发现的所有内容似乎都只是详细说明了从 F# 到 RabbiTMQ 的实际连接,而在处理从MQ 队列....

非常感谢任何帮助,谢谢!

实际的 F# fsx 脚本如下所示:

#r "RabbitMQ.Client.dll"

open System
open RabbitMQ.Client
open RabbitMQ.Client.Events
open System.Text

let factory = new ConnectionFactory(HostName = "localhost")
(
use connection = factory.CreateConnection()
use channel = connection.CreateModel()
use channel1 = connection.CreateModel()

channel.QueueDeclare("FeedQ1", false, false, false, null) |> ignore
channel1.QueueDeclare("FeedQ2", false, false, false, null) |> ignore

let consumer = new QueueingBasicConsumer(channel)
channel.BasicConsume("FeedQ1", true, consumer) |> ignore
channel.BasicConsume("FeedQ2", true, consumer) |> ignore


printfn " [*] Waiting for messages. To exit press CTRL+C"

let rec loop () : unit =
    let ea = consumer.Queue.Dequeue();

    let body = ea.Body
    let message = Encoding.UTF8.GetString(body)
    printfn " [x] Received %s" message

    loop ()

loop ()
)

如果你告诉我你要做什么处理,帮助会更容易

let message = "Received onData;FeedQ1;7.488;7.48813;1427315602762;8.61;8.31"
let splited = message.Split(';')
splited |> printfn "Array: %A"
splited.[0] |> printfn "First element: %s"
splited.Length |> printfn "Array length: %d"

输出:

Array: [|"Received onData"; "FeedQ1"; "7.488"; "7.48813"; "1427315602762"; "8.61";
  "8.31"|]
First element: Received onData
Array length: 7

http://ideone.com/SEyYov

更多信息: https://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx?cs-save-lang=1&cs-lang=fsharp#code-snippet-1