简体   繁体   English

Clojure从数据库获取数据,转换并打印到控制台

[英]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. 我还创建一个表firstlast 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: 从这段代码很难说出性能瓶颈在哪里,但是这里有一些技巧:

  • Use type hints for hotspots. 对热点使用类型提示。 Sometimes the Clojure compiler can't figure out the types, and then type hints can speed things up a lot. 有时Clojure编译器无法找出类型,然后使用类型提示可以加快处理速度。 In this case, you could set it on the usa-address-getter fn: (defn ^String usa-adress-getter [] ...) 在这种情况下,可以在usa-address-getter fn上进行设置: (defn ^String usa-adress-getter [] ...)
  • You could consider modifying your query so that it returns the concatenated string from the database (using an SQL function like concat ). 您可以考虑修改查询,以使其从数据库返回连接的字符串(使用诸如concat类的SQL函数)。 That way, you won't need to get the values from the hash and build the string yourself. 这样,您将不需要从哈希中获取值并自己构建字符串。
  • Printing stuff may be slow, so you could eliminate that, like @leetwinsky says. 打印内容可能很慢,因此您可以消除这种情况,例如@leetwinsky说。
  • You have to measure in detail the performance of your code, otherwise you won't be able to tell if a modification has gained you some speed. 您必须详细评估代码的性能,否则您将无法判断修改是否使您获得了一定的速度。 For instance, you could put a timer that prints the number msecs for every 1000 records processed. 例如,您可以放置​​一个计时器,该计时器为每处理1000条记录打印毫秒数。 An of course, only do one change at a time. 当然,一次只能进行一次更改。

Hope this helps. 希望这可以帮助。

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

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