简体   繁体   English

使用Riemann在clojure中转换浮点字符串

[英]Convert a string in a float with Riemann in clojure

I am processing streams in Riemann and all the fields are strings. 我在黎曼处理流,所有字段都是字符串。 However I'd like to perform some numeric comparisons on some fields. 但是我想在某些字段上进行一些数字比较。 Therefore I tried to convert them in float. 因此,我试图在浮动中转换它们。

Let's say my stream is like that : 假设我的流是这样的:

#riemann.codec.Event{:host "myHost", :service nil, :state nil, :description "my description", :pred_score "0.156"}

I've tried to modify the riemann.conf file with something like this : 我试图用这样的东西修改riemann.conf文件:

(streams
 (with :new_field (read-string :pred_score))
 prn)

However I got some error and I feel like this is not the correct way to do this. 但是我遇到了一些错误,我觉得这不是正确的方法。 I have recently read some stuffs making me think that I should use smap or adjust but I'm not sure. 我最近读过一些东西让我认为我应该使用smapadjust但我不确定。

I am absolutely not familiar with clojure by the way. 顺便说一句,我绝对不熟悉clojure。 (In fact I discovered it with riemann). (事实上​​我是用riemann发现的)。

Have you got any idea on how to tackle the problem ? 您对如何解决这个问题有任何想法吗?

Thanks in advance, 提前致谢,

Robin. 罗宾。

I don't know about the riemann part, but in plain clojure you could use either Java interop or the Tupelo library . 我不知道riemann部分,但在普通的clojure中你可以使用Java interop或Tupelo库 Using Java interop, you'd do: 使用Java互操作,您可以:

> (def x {:host "myHost", :service nil, :state nil, :description "my description", :pred_score "0.156"} )
> (Double/parseDouble (:pred_score x))
0.156

The 'tupelo.parse' namespace wraps this inside a normal clojure function, and adds the ability to specify a default value to return in the event of a malformed value (instead of throwing an exception): 'tupelo.parse'命名空间将其包装在普通的clojure函数中,并添加了指定在格式错误的情况下返回的默认值(而不是抛出异常)的能力:

> (require '[tupelo.parse :as parse])
> (parse/parse-double (:pred_score x))
0.156
> (parse/parse-double "123.4x6" :default 0)
0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM