[英]Clojure getting data from db, transform and print into console
I have the following task. 我有以下任务。
I need to create a console application which takes one param which is the number of data to generate. 我需要创建一个使用一个参数的控制台应用程序,该参数是要生成的数据数量。 The data is person address and name.
数据是人的地址和姓名。 I create a table
adress
with state, city, zip-code
fields. 我创建一个包含
state, city, zip-code
字段的表格adress
。 I also create a table with first
and last name
columns. 我还创建一个表
first
和last name
列。 I use HugSQL to deal with PostgreSQL. 我使用HugSQL处理PostgreSQL。 So I want to dynamically mix addresses, first and last name and print such the result into the console, the number of generated values depends on the argument passed to the application.
因此,我想动态混合地址,名字和姓氏,并将结果打印到控制台中,生成的值的数量取决于传递给应用程序的参数。 This is my code:
这是我的代码:
(ns project.core
(:require
[project.db.get :as get]))
(defn parse-int [s]
(Integer. (re-find #"\d+" s )))
(def usa-data (get/usa))
(defn usa-adress-getter []
(let [data (into {} (shuffle usa-data))
city (get data :city)
state (get data :state)
zip (get data :zip_code)]
(str state " " city " " zip)))
(defn repeater [times]
(dotimes [i times]
(println (usa-adress-getter))))
(defn -main [value]
(repeater (parse-int value)))
Here I just check the result of usa-adress-getter
function. 在这里,我只是检查
usa-adress-getter
函数的结果。 But the time of the evaluation of function is too big, i have limit which is 1 million values in 1 minutes. 但是功能评估的时间太大,我在1分钟内有1百万个值的限制。 How to increase the speed of the evaluation?
如何提高评估速度? Function
(get/usa)
retrieve all data from adress
table. 函数
(get/usa)
从adress
表中检索所有数据。
It's difficult to say from this piece of code where the performance bottlenecks are, but here are some tips: 从这段代码很难说出性能瓶颈在哪里,但是这里有一些技巧:
usa-address-getter
fn: (defn ^String usa-adress-getter [] ...)
usa-address-getter
fn上进行设置: (defn ^String usa-adress-getter [] ...)
concat
). concat
类的SQL函数)。 That way, you won't need to get the values from the hash and build the string yourself. Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.