我在json中有一个带有城市名称的结果集,我想得到每个人的lat-long。以下功能在一定程度上起作用:
(:require [http.async.client :as client])
(defn get-geo-fact [row]
(let [
n (string/replace (:cityname row) " " "+")
url (str "http://maps.googleapis.com/maps/api/geocode/json?address="
n "&sensor=false")
resp (client/GET url) ]
(client/await resp)
(make-geo-fact row (json/read-json (client/string resp))) ))
最后一次调用make-geo-fact只返回城市坐标的RDF渲染。我遇到的问题是运行(对此函数的约40次调用)返回lat-longs的一些(3-5个lat-long对)null结果。返回null值的城市因运行而异 - 有时San Jose会获得坐标,有时则不会。
我最初使用 slurp 来抓取网址的主体并获得类似偶尔的空值。我认为我没有正确等待响应,但切换到 http.async.client 似乎没有做到这一点。有什么想法吗?
修改:
这是make-geo-fact功能,它带有“这个团队位于这个城市”的事实和谷歌地图的响应,返回两个传递纬度和经度的三元组矢量:
(defn make-geo-fact [row response]
(let [ g (:location (:geometry (first (:results response))))
lat (str "<" (:team row) ">"
" <http://www.nhl.com/latitude> \"" (:lat g)
"\"^^<http://www.w3.org/2001/XMLSchema#decimal> ." )
lon (str "<" (:team row) ">"
" <http://www.nhl.com/longitude> \"" (:lng g)
"\"^^<http://www.w3.org/2001/XMLSchema#decimal> ." ) ]
[lat lon] ))
这就是我打电话来解决所有问题的功能:
(defn make-geo-facts []
(let [ a (bounce team-city (build "files/team-city.nt"))
f "files/geo-facts.nt" ]
(spit f (string/join "\n" (flatten (map get-geo-fact (:rows a)))))
f ))
bounce 函数针对RDF模型发出SPARQL select查询,该模型使用 build 函数进行实例化。
编辑2
这是一个不需要make-geo-fact的重要因素:
(defn get-geo-fact [row]
(let [ n (string/replace (:cityname row) " " "+")
url (str "http://maps.googleapis.com/maps/api/geocode/json?address=" n "&sensor=false")
resp (client/GET url) ]
(-> (client/GET url)
client/await
client/string
json/read-json
:results
first
:geometry
:location )))
(defn make-geo-facts []
(let [ a (bounce tc-query (build "files/team-city.nt"))
f "files/geo-facts.nt"
*client* (client/create-client)]
(try (spit f (string/join "\n" (flatten (map get-geo-fact (:rows a))))))
(finally (client/close *client*)) ))
答案 0 :(得分:1)
正如您所说,改变客户端实现并没有什么不同。 我仔细检查并为http.async.client的开发版本创建test。 总是得到身体的回应。
请提供make-geo-fact implementation。
答案 1 :(得分:1)
原来我的代码需要一点睡眠。我将(线程/睡眠1000)添加到我的核心函数中,现在我没有得到null结果:
(defn get-geo-fact [row]
(let [ n (string/replace (:cityname row) " " "+")
url (str "http://maps.googleapis.com/maps/api/geocode/json?address=" n "&sensor=false")
resp (client/GET url) ]
(Thread/sleep 1000)
(-> (client/GET url)
client/await
client/string
json/read-json
(make-geo-fact ,,, row ) )))