Home > Uncategorized > Importing a CSV file into Amazon SimpleDB

Importing a CSV file into Amazon SimpleDB

Here’s some code to import a CSV file into a Amazon SimpleDB database

            Console.WriteLine("Connecting to Amazon Simple DB");
    
            NameValueCollection appConfig = ConfigurationManager.AppSettings;

            AmazonSimpleDB sdb = AWSClientFactory.CreateAmazonSimpleDBClient(
                appConfig["AWSAccessKey"],
                appConfig["AWSSecretKey"]
                );

            // Setup Flights DataStore
            String domainName = "CSV";
            CreateDomainRequest createDomain = (new CreateDomainRequest()).WithDomainName(domainName);
            sdb.CreateDomain(createDomain);

            Console.WriteLine("Created flights DB on Amazon");

            string strCSVFile = @"C:MyFile.csv";
            FileStream fsCSV = new FileStream(strCSVFile, FileMode.Open, FileAccess.Read);
            StreamReader srCSV = new StreamReader(fsCSV);
            string strHeaderLine = srCSV.ReadLine();
            string[] strHeaders = Regex.Split(strHeaderLine, ",");
            // Remove Inverted Commas
            for (int i = 0; i < strHeaders.Length; i++)
            {
                strHeaders[i] = strHeaders[i].Replace(""", "");
            }

            Console.WriteLine("Read column headers");

            string strDataLine = srCSV.ReadLine();
            while(!string.IsNullOrEmpty(strDataLine))
            {
                string[] strData = Regex.Split(strDataLine, ",");
                if (strData.Length < strHeaders.Length) continue;
                strDataLine = srCSV.ReadLine();
                PutAttributesRequest putAttributesAction = new PutAttributesRequest().WithDomainName(domainName).WithItemName(Guid.NewGuid().ToString());
                List<ReplaceableAttribute> attributes = putAttributesAction.Attribute;               
                for (int i = 0; i < strHeaders.Length-1; i++)
                {
                    strData[i] = strData[i].Replace(""", "");
                    attributes.Add(new ReplaceableAttribute().WithName(strHeaders[i]).WithValue(strData[i]));              
                }
                sdb.PutAttributes(putAttributesAction);
                Console.WriteLine(strDataLine);
            }

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: