Load data from CSV in Grails
I have a CSV file with a list of "banned usernames" --it's about 10,000 names. Basically I want to check against this list upon user registration. I'd like to load this list in my database but I'd rather not create a domain object for each name and BootStrap them. However, I'd also like to stay within the Grails framework as far as managing my data. For example, if I have dbCreate set to create-drop, I don't want to lose my username list --that is, I don't want to just create a separate table by hand. How开发者_开发技巧 should I go about doing this? Does Grails provide anything out of the box to handle this type of situation? Any advice would be great
So, what exactly is your problem with creating a domain class for those banned usernames and storing them in the database? The only issue I can see is that you need to load them as seed data every time you start your app in development (in production you wouldn't use create-drop, would you? ;-) ). I haven't tested this, but my guess would be that with 10,000 very simple objects this shouldn't be an annoying performance blocker.. In production, you will most probably use "update" as hibernate strategy, so you just need to load your data once into the production DB. So, my preferred way would be to create a domain class and load the data upon app start (in development only).
If your banned username data is read-only (ie. the list is not extended during runtime), you could also consider to load it into memory (probably a HashSet
for constant time contains()
). 10,000 strings shouldn't be a problem with respect to memory consumption and the need to load them upon each app start isn't a problem either, because usually you don't restart your production environment too often.
Yes, you can load your file into your database and still use it in your grails app. At that point you can use Groovy Sql to do your custom queries. I think it's a good practice to keep the queries together inside of DAO style classes but that is up to you. If you go that route you can also still use dependency injection of the DAO classes into your services by using @Component and @Resource annotations
i think you would want this data to be in the database. that way you could query the registering user against the 'blacklisted' names.
精彩评论