打印 Mnesia 表的最佳方式
我尝试了这个代码片段:
print_next(Current) ->
case mnesia:dirty_next(muppet, Current) of
'$end_of_table' ->
io:format("~n", []),
ok;
Next ->
[Muppet] = mnesia:dirty_read({muppet, Next}),
io:format("~p~n", [Muppet]),
print_next(Next),
ok
end.
print() ->
case mnesia:dirty_first(muppet) of
'$end_of_table' ->
ok;
First ->
[Muppet] = mnesia:dirty_read({muppet, First}),
io:format("~p~n", [Muppet]),
print_next(First),
ok
end.
但它太长了。我也可以使用 dirty_all_keys
然后遍历键列表,但我想知道是否有更好的方法来打印 Mnesia 表内容。
I tried this code snippet:
print_next(Current) ->
case mnesia:dirty_next(muppet, Current) of
'$end_of_table' ->
io:format("~n", []),
ok;
Next ->
[Muppet] = mnesia:dirty_read({muppet, Next}),
io:format("~p~n", [Muppet]),
print_next(Next),
ok
end.
print() ->
case mnesia:dirty_first(muppet) of
'$end_of_table' ->
ok;
First ->
[Muppet] = mnesia:dirty_read({muppet, First}),
io:format("~p~n", [Muppet]),
print_next(First),
ok
end.
But it is so long. Also I can use dirty_all_keys
and then iterate through key list, but I want to know if there is a better way to print out Mnesia table contents.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您只是想要一种快速而肮脏的方式在 shell 中打印 Mnesia 表的内容,并且您的表不是
disc_only_copies
类型,那么您可以利用 Mnesia 存储其表的事实ETS 表中的数据并运行:或者,如果您认为 shell 截断数据太多:
当然,不建议用于“真实”代码。
If you just want a quick and dirty way to print the contents of a Mnesia table in the shell, and if your table is not of type
disc_only_copies
, then you can take advantage of the fact that Mnesia stores its data in ETS tables and run:or, if you think the shell truncates the data too much:
Not recommended for "real" code, of course.
要简单快速地查看表格内容,您可以使用 mnesia 的 select 功能和 catch-all 匹配规范如下:
您也可以在事务上下文中运行它:
但是,如果您处于具有大数据的生产环境中,请小心。
For a simple and quick look at your table contents you can use select function of mnesia with catch-all Match Specification as follows:
and also you can run it inside a transaction context:
however be careful if you are in a production environment with a big data.
好吧,如果目的是查看表的内容,则有一个名为 tv,可以查看 ETS 和 mnesia 表。
如果您希望在终端上查看所有表格内容,请尝试如下操作:
然后,如果您的表格名为
muppet
,则可以按如下方式使用该函数:这样做的优点:
如果在事务内执行,就不会有嵌套事务的问题。与 get_next_key -> 的实现相比,它的工作量更少,因为它通过 mnesia 迭代器功能在一个 mnesia 事务中完成。 do_read_with_key ->;然后读取记录(这些操作很多)。这样,mnesia 就会自动告诉你它已经覆盖了你整个表中的所有记录。此外,如果表是分段的,您的功能将仅显示第一个分段中的记录。这将迭代属于该表的所有片段。
在此迭代 mnesia 方法中,我对 Accumulator 变量不执行任何操作,该变量应与 Iterator 函数一起使用,这就是为什么您会看到第二个变量的下划线。
此迭代的详细信息可以在这里找到:
< /a>http://www.erlang.org/doc/man/mnesia.html#foldl-3
Well, if the intent is to see the contents of your table, there is the application called tv, which can view both ETS and mnesia tables.
If you wish to see all the table contents on your terminal, then try something like this:
Then if your table is called
muppet
, you use the function as follows:Advantages of this:
If its executed within a transaction , it will have no problems of nested transactions. It is less work because its done within one mnesia transaction through mnesia iterator functionality as compared to your implementation of get_next_key -> do_read_with_key -> then read the record (these are many operations). With this, mnesia will automatically tell that it has covered all the records in your entire table. Also, if the table is fragmented, your functionality will only display records in the first fragment. This will iterate through all the fragments the belong to that table.
In this iteration mnesia method, i do nothing with the Accumulator variable which should go along with the
Iterator
fun and thats why you see the underscore for the second variable.Details of this iteration can be found here:
http://www.erlang.org/doc/man/mnesia.html#foldl-3
正如 Muzaaya 所说,您可以使用 tv(表可视化工具)来查看 mnesia 和 ets 表。
或者,您可以使用以下代码获取 mnesia 表数据 - 在终端上打印或者如果您想将结果存储在文件中:
As Muzaaya told, you can you use tv (table visualizer tool) to view both mnesia and ets tables.
Alternatively, you can use the following code to get mnesia table data - Print on terminal or in case you want to store the result in a file :