Skip to content

Instantly share code, notes, and snippets.

@cdahlqvist
Last active December 15, 2015 14:48
Show Gist options
  • Save cdahlqvist/5276622 to your computer and use it in GitHub Desktop.
Save cdahlqvist/5276622 to your computer and use it in GitHub Desktop.
Script to list all keys in a Riak bucket and spool to a file. To be run while attached to riak console.
%% -------------------------------------------------------------------
%%
%% keylister: Utility for listing all keys in a bucket
%%
%% Copyright (c) 2013 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
-module(keylister).
-author('Christian Dahlqvist <[email protected]>').
-export([list_keys_to_file/2]).
-define(TIMEOUT, 10000000).
%% @spec list_keys_to_file(binary(), string()) ->
%% ok | {error, term()}
list_keys_to_file(Bucket, File) when is_binary(Bucket) andalso is_list(File) ->
{ok, C} = riak:local_client(),
case file:open(File, [write]) of
{ok, IoDev} ->
C:stream_list_keys(Bucket, ?TIMEOUT),
write_keys_to_file(IoDev);
{error, Reason} ->
{error, Reason}
end.
write_keys_to_file(IoDev) ->
receive
{_, {keys,Keys}} ->
Output = [[K, <<"\n">>] || K <- Keys],
file:write(IoDev, Output),
write_keys_to_file(IoDev);
{_, From, {keys,Keys}} ->
riak_kv_keys_fsm:ack_keys(From),
Output = [[K, <<"\n">>] || K <- Keys],
file:write(IoDev, Output),
write_keys_to_file(IoDev);
{_, done} ->
file:close(IoDev);
M ->
{error, unexpected_message, M}
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment