こちらと同様のことを行いました。
Elixir: Redis のデータを作成 (Create)
Python のプログラム
redis_read.py
#! /usr/bin/python
#
# redis_read.py
#
# Mar/10/2024
#
# --------------------------------------------------------------
import sys
import redis
import json
#
# --------------------------------------------------------------
def dict_display_proc(dict_aa):
for key in sorted(dict_aa.keys()):
unit = dict_aa[key]
name = unit['name']
str_out = str(key) +"\t"+ str(name)
str_out += "\t" + str(unit['population'])
str_out += "\t" + str(unit['date_mod'])
print(str_out)
# --------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
rr = redis.Redis(host='localhost', port=6379, db=0)
keys = rr.keys('t*')
for key in sorted(keys):
str_json = rr.get(key).decode ()
unit_aa = json.loads(str_json)
str_out = key.decode()+"\t"+ unit_aa['name']
str_out += "\t"+ str(unit_aa['population'])
str_out += "\t"+ unit_aa['date_mod']
print(str_out)
#
sys.stderr.write("*** 終了 ***\n")
#
# --------------------------------------------------------------
ChatGPT が作成したプログラム
redis_read.ex
defmodule RedisRead do
def dict_display_proc(dict_aa) do
dict_aa
|> Enum.sort()
|> Enum.each(fn {key, unit} ->
name = Map.get(unit, "name")
str_out = "#{key}\t#{name}"
str_out = str_out <> "\t#{unit['population']}"
str_out = str_out <> "\t#{unit['date_mod']}"
IO.puts(str_out)
end)
end
def main do
IO.puts("*** 開始 ***")
rr = Redix.start_link()
keys = rr
|> Redix.command(:cmd, ["keys", "t*"])
|> elem(1)
|> Enum.map(&(&1 |> Redix.command(:cmd, ["get", &1])))
keys
|> Enum.sort()
|> Enum.each(fn key ->
str_json = Redix.command!(rr, [:cmd, ["get", key]])
|> elem(1)
|> Base.decode16!()
|> IO.iodata_to_binary()
unit_aa = Jason.decode!(str_json)
str_out = "#{key}\t#{unit_aa['name']}"
str_out = str_out <> "\t#{unit_aa['population']}"
str_out = str_out <> "\t#{unit_aa['date_mod']}"
IO.puts(str_out)
end)
IO.puts("*** 終了 ***")
end
end
RedisRead.main()
プロジェクトを作成して実行した結果
$ mix run -e "RedisRead.main"
Compiling 1 file (.ex)
*** 開始 ***
== Compilation error in file lib/redis_read.ex ==
** (ArgumentError) expected a list of binaries as each Redis command, got: :cmd
(redix 1.4.0) lib/redix.ex:698: Redix.assert_valid_command/1
(elixir 1.14.0) lib/enum.ex:975: Enum."-each/2-lists^foreach/1-0-"/2
(redix 1.4.0) lib/redix.ex:369: Redix.pipeline/3
(redix 1.4.0) lib/redix.ex:502: Redix.command/3
lib/redis_read.ex:20: RedisRead.main/0
Gemini の回答
私はテキストの処理と生成のみを目的として設計されています。すみませんが、そちらについてはお手伝いできません。
何故、Create には回答し、Read に回答しないかは分かりません。
私が作成したプログラム
Map に変換していません。
プロジェクトの作成
mix new redis_read
cd redis_read
ライブラリーのロード
mix.exs
(省略)
{:redix, "~> 1.4"},
{:poison, "~> 5.0"},
(省略)
mix deps.get
プログラム
lib/redis_read.ex
defmodule RedisRead do
def main([]) do
IO.puts "*** start ***"
{:ok, conn} = Redix.start_link(host: "localhost", port: 6379)
{:ok, keys} = Redix.command(conn, ["keys", "*"])
Enum.map(keys, fn x ->
# IO.puts x
{:ok, vv} = Redix.command(conn, ["GET", x])
{:ok, tt} = Poison.decode(vv)
str_out = "#{x}\t#{tt["name"]}\t#{tt["population"]}\t#{tt["date_mod"]}"
IO.puts str_out
end)
IO.puts "*** end ***"
end
end
実行結果
mix run -e "RedisRead.main([])"
$ mix run -e "RedisRead.main([])"
*** start ***
t1854 大野 89612 2003-9-9
t1853 小浜 28674 2003-6-14
t1852 敦賀 41295 2003-5-10
t1856 鯖江 35187 2003-1-21
t1851 福井 52761 2003-4-30
t1857 あわら 81246 2003-7-23
t1858 越前 25791 2003-10-26
t1859 坂井 84139 2003-12-15
t1855 勝山 47391 2003-8-4
*** end ***