假設(shè)有如下數(shù)據(jù) Records
rank name desc
1 apple fruit
2 cat animal
3 bike vehicle
如果你想把你的集合拆分成小的分組捶闸,chunk
就是你要找的函數(shù):
iex> Enum.chunk([1, 2, 3, 4, 5, 6], 2)
[[1, 2], [3, 4], [5, 6]]
使用~w()
這個(gè)魔術(shù)符可以將空格分割的字符串轉(zhuǎn)換為列表:
iex> ~w(1 apple fruit)
["1", "apple", "fruit"]
1. 數(shù)據(jù)預(yù)處理:輸入所有數(shù)據(jù)到data列表變量
iex> data = ~w(1 apple fruit
...> 2 cat animal
...> 3 bike vehicle)
["1", "apple", "fruit", "2", "cat", "animal", "3", "bike", "vehicle"]
2. 數(shù)據(jù)分組:使用chunk
函數(shù)進(jìn)行分組
iex> Enum.chunk(data, 3)
[["1", "apple", "fruit"], ["2", "cat", "animal"], ["3", "bike", "vehicle"]]
當(dāng)然兄纺,也可以寫成
data |> Enum.chunk(3)
結(jié)果是一樣的
3. 構(gòu)成結(jié)構(gòu)體
可以使用Enum.zip組合兩個(gè)列表
iex> Enum.zip([:rank, :name, :desc], ["1", "apple", "fruit"])
[rank: "1", name: "apple", desc: "fruit"]
如果在后面使用Enum.into就可以構(gòu)造map類型啦!
iex> Enum.zip([:rank, :name, :desc], ["1", "apple", "fruit"]) |> Enum.into(%{})
%{desc: "fruit", name: "apple", rank: "1"}
4. Map VS Struct
這里我們回顧一下Elixir Map和Struct的區(qū)別,map屬于基本結(jié)構(gòu)颓遏,形式為%{usernmae: "szy", passwd: "elixir"}
狸剃,注意到這里的username拼寫錯(cuò)誤,在使用時(shí):
iex> user.username
** (KeyError) key :username not found in: %{passwd: "elixir", usernmae: "szy"}
發(fā)現(xiàn)提示沒有找到這個(gè)key烹玉。這說明map只在runtime進(jìn)行key的驗(yàn)證驰怎,在插入的時(shí)候沒有進(jìn)行驗(yàn)證。如果希望在插入數(shù)據(jù)時(shí)就對key是否存在進(jìn)行驗(yàn)證二打,則需要使用struct县忌。
Struct本質(zhì)上也是一個(gè)Map,只不過多了一個(gè)Key继效,也就是__struct__
症杏。
iex> user = %User{username: "szy"}
%Elecity.User{__meta__: #Ecto.Schema.Metadata<:built>, description: nil,
email: nil, id: nil, inserted_at: nil, password: nil, password_hash: nil,
updated_at: nil,
user_role_id: nil, username: "szy"}
如果插入時(shí)沒有key會立即提示錯(cuò)誤
iex> user2 = %User{usernmae: "szy"}
** (CompileError) iex:3: unknown key :usernmae for struct Elecity.User
此外,插入時(shí)沒有聲明的部分會自動填充默認(rèn)內(nèi)容瑞信。
5. 綜合
現(xiàn)在我們就來構(gòu)造結(jié)構(gòu)體吧厉颤!同樣使用~w()a
,這里后面的a代表轉(zhuǎn)換的是atom類型凡简。
iex> key = ~w(rank name desc)a
[:rank, :name, :desc]
iex> data = ~w(1 apple fruit
...> 2 cat animal
...> 3 bike vehicle)
["1", "apple", "fruit", "2", "cat", "animal", "3", "bike", "vehicle"]
iex> data = Enum.chunk(data, 3)
[["1", "apple", "fruit"], ["2", "cat", "animal"], ["3", "bike", "vehicle"]]
iex(19)> for record <- data do
...(19)> Enum.zip(key, record)
...(19)> |> Enum.into(%Record{})
...(19)> |> Repo.insert!()
...(19)> end
當(dāng)然逼友,使用insert_all
可以大大簡化
MyApp.Repo.insert_all(Post, [[title: "hello", body: "world"], [title: "another", body: "post"]])
第一個(gè)data
iex(21)> for rec <- data do
...(21)> Enum.zip(key, rec)
...(21)> end
[[rank: "1", name: "apple", desc: "fruit"],
[rank: "2", name: "cat", desc: "animal"],
[rank: "3", name: "bike", desc: "vehicle"]]
總結(jié)一下绩郎,就是:
- ~w()a轉(zhuǎn)換key
- ~w()轉(zhuǎn)換數(shù)據(jù)
- Enum.chunk分組數(shù)據(jù)
- Enum.zip添加key
- Enum.into轉(zhuǎn)換Struct后逐個(gè)插入Ecto 1.x
- 批量 insert_all (Step4) Ecto 2.x
http://blog.plataformatec.com.br/2016/05/ectos-insert_all-and-schemaless-queries/
http://www.elixir-cn.com/posts/136