Skip to content

Instantly share code, notes, and snippets.

@aliostad
Created November 26, 2014 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aliostad/580ee18c966ce922a029 to your computer and use it in GitHub Desktop.
Save aliostad/580ee18c966ce922a029 to your computer and use it in GitHub Desktop.
Table Storage Dumper
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
namespace TableStorageDumper
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 2)
{
ConsoleWrite(ConsoleColor.White, "TableStorageDumper <connectionstring> <tablename>");
return;
}
var account = CloudStorageAccount.Parse(args[0]);
var client = account.CreateCloudTableClient();
var table = client.GetTableReference(args[1]);
var query = new TableQuery<DynamicTableEntity>();
string[] headers = null;
var writer = new StreamWriter(args[1] + "_" + Environment.TickCount + ".tsv");
writer.AutoFlush = true;
int n = 0;
var starTimeOffset = DateTimeOffset.Now;
foreach (var entity in table.ExecuteQuery(query))
{
if (headers==null)
{
headers=new string[entity.Properties.Count + 3];
headers[0] = "PK";
headers[1] = "RK";
headers[2] = "Timestamp";
int i = 3;
foreach (var property in entity.Properties.Keys)
{
headers[i++] = property;
}
writer.WriteLine(string.Join("\t", headers));
}
writer.Write(entity.PartitionKey);
writer.Write('\t');
writer.Write(entity.RowKey);
writer.Write('\t');
writer.Write(entity.Timestamp.ToString());
writer.Write('\t');
for (int i = 3; i < headers.Length; i++)
{
writer.Write(entity.Properties[headers[i]].PropertyAsObject);
if(i<headers.Length-1)
writer.Write('\t');
}
writer.WriteLine();
if (++n%1000 == 0)
{
ConsoleWrite(ConsoleColor.Yellow, "\r" + n);
ConsoleWrite(ConsoleColor.DarkCyan, "\t" + DateTimeOffset.Now.Subtract(starTimeOffset).ToString());
}
}
writer.Close();
}
private static void ConsoleWrite(ConsoleColor color, string value, params object[] args)
{
var foregroundColor = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.Write(value, args);
Console.ForegroundColor = foregroundColor;
}
private static void ConsoleWriteLine(ConsoleColor color, string value, params object[] args)
{
var foregroundColor = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.WriteLine(value, args);
Console.ForegroundColor = foregroundColor;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment