打印 Mnesia 表的最佳方式

发布于 2024-12-10 03:22:17 字数 729 浏览 0 评论 0原文

我尝试了这个代码片段:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

王权女流氓 2024-12-17 03:22:17

如果您只是想要一种快速而肮脏的方式在 shell 中打印 Mnesia 表的内容,并且您的表不是 disc_only_copies 类型,那么您可以利用 Mnesia 存储其表的事实ETS 表中的数据并运行:

ets:tab2list(my_table).

或者,如果您认为 shell 截断数据太多:

rp(ets:tab2list(my_table)).

当然,不建议用于“真实”代码。

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:

ets:tab2list(my_table).

or, if you think the shell truncates the data too much:

rp(ets:tab2list(my_table)).

Not recommended for "real" code, of course.

送君千里 2024-12-17 03:22:17

要简单快速地查看表格内容,您可以使用 mnesiaselect 功能和 catch-all 匹配规范如下:

CatchAll = [{'_',[],['$_']}].
mnesia:dirty_select(TableName, CatchAll).

您也可以在事务上下文中运行它:

CatchAll = [{'_',[],['$_']}].
SelectFun = fun() -> mnesia:select(TableName, CatchAll) end.
mnesia:transaction(SelectFun).

但是,如果您处于具有大数据的生产环境中,请小心。

For a simple and quick look at your table contents you can use select function of mnesia with catch-all Match Specification as follows:

CatchAll = [{'_',[],['$_']}].
mnesia:dirty_select(TableName, CatchAll).

and also you can run it inside a transaction context:

CatchAll = [{'_',[],['$_']}].
SelectFun = fun() -> mnesia:select(TableName, CatchAll) end.
mnesia:transaction(SelectFun).

however be careful if you are in a production environment with a big data.

黄昏下泛黄的笔记 2024-12-17 03:22:17

好吧,如果目的是查看表的内容,则有一个名为 tv,可以查看 ETS 和 mnesia 表。

如果您希望在终端上查看所有表格内容,请尝试如下操作:

traverse_table_and_show(Table_name)->
    Iterator =  fun(Rec,_)->
                    io:format("~p~n",[Rec]),
                    []
                end,
    case mnesia:is_transaction() of
        true -> mnesia:foldl(Iterator,[],Table_name);
        false -> 
            Exec = fun({Fun,Tab}) -> mnesia:foldl(Fun, [],Tab) end,
            mnesia:activity(transaction,Exec,[{Iterator,Table_name}],mnesia_frag)
    end.

然后,如果您的表格名为 muppet,则可以按如下方式使用该函数:

traverse_table_and_show(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:

traverse_table_and_show(Table_name)->
    Iterator =  fun(Rec,_)->
                    io:format("~p~n",[Rec]),
                    []
                end,
    case mnesia:is_transaction() of
        true -> mnesia:foldl(Iterator,[],Table_name);
        false -> 
            Exec = fun({Fun,Tab}) -> mnesia:foldl(Fun, [],Tab) end,
            mnesia:activity(transaction,Exec,[{Iterator,Table_name}],mnesia_frag)
    end.

Then if your table is called muppet, you use the function as follows:

traverse_table_and_show(muppet).

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

酒儿 2024-12-17 03:22:17

正如 Muzaaya 所说,您可以使用 tv(表可视化工具)来查看 mnesia 和 ets 表。
或者,您可以使用以下代码获取 mnesia 表数据 - 在终端上打印或者如果您想将结果存储在文件中:

select_all() -> 
   mnesia:transaction( 
fun() ->
    P=qlc:e(qlc:q([E || E <- mnesia:table(tableName)])),      %query to select all data from table named 'tableName'                                                                                    
    io:format(" ~p ~n ", [P]), % Prints table data on terminal
    to_file("fileName.txt",P) % to_file method writes the data to file
end ).


to_file(File, L) ->
  mnesia:transaction( 
fun() ->
      {ok, S} = file:open(File, write),
      lists:foreach(fun(X) -> io:format(S, "~p.~n" ,[X]) end, L),
      file:close(S)
  end).

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 :

select_all() -> 
   mnesia:transaction( 
fun() ->
    P=qlc:e(qlc:q([E || E <- mnesia:table(tableName)])),      %query to select all data from table named 'tableName'                                                                                    
    io:format(" ~p ~n ", [P]), % Prints table data on terminal
    to_file("fileName.txt",P) % to_file method writes the data to file
end ).


to_file(File, L) ->
  mnesia:transaction( 
fun() ->
      {ok, S} = file:open(File, write),
      lists:foreach(fun(X) -> io:format(S, "~p.~n" ,[X]) end, L),
      file:close(S)
  end).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文