0

I am downloading csv file from AWS S3 bucket so that I can use it in D3.

No problem downloading the csv file. In code below the console.log(data.Body.toString()) prints out the csv file contents in expected format

However, I am not sure how to pass the downloaded csv file content to D3 in code below.

The file contents are already in nice csv file format.

Question: how to replace merged.csv below with the mergedcsv object?

<script type="text/javascript">

var bucket = new AWS.S3();

bucket.getObject({
    Bucket: 's3-google-analytics', 
    Key: 'merged.csv'
}, 
    function awsDataFile(error, data) {
        if (error) {
            return console.log(error);
        }
        mergedcsv = data.Body.toString();
        console.log(data.Body.toString());
    }
);

// how to replace the "merged.csv" below with the mergedcsv object above?

d3.csv("merged.csv", function(data) {
    var parseDate = d3.time.format("%Y%m%d").parse;
    var counter = 0;
    data.forEach(function(d) {
        etc
    });
});

</script>

updated to add sample of mergedcsv content eg output of console.log(mergedcsv); or console.log(data.Body.toString()); :

yyyymm,date,hostname,source,PPCC,in30days,sessionDuration,users,sessions,newUsers,twitterSessions,bounceRate,sessionsPerUser
201203,20120330,journal,google,PPCC,>30days,26.25,4,4,4,0,75.0,1.0
201203,20120331,journal,(direct),PPCC,>30days,0.0,3,3,3,0,100.0,1.0
201203,20120331,journal,bing,PPCC,>30days,0.0,1,1,1,0,100.0,1.0
1
  • Can you give an example of the structure of mergedcsv? Commented Jul 11, 2019 at 23:24

1 Answer 1

5

After reading a the docs on d3.csv, it loads a CSV file from a URL, but in your example, you've already loaded the data, you just need to parse it. So d3.csv isn't going to be of any use.

It looks like the D3 function for parsing is d3.csv.parse(data) which will return an array of parsed data. (Docs are here)

So you could do something like…

var bucket = new AWS.S3();
var mergedcsv;

bucket.getObject({
    Bucket: 's3-google-analytics', 
    Key: 'merged.csv'
}, 
    function awsDataFile(error, data) {
        if (error) {
            return console.log(error);
        }
        mergedcsv = d3.csv.parse(data.Body.toString()); 

        // now mergedcsv will be an array, so you can do whatever you were going to do with it before, i.e...
        var parseDate = d3.time.format("%Y%m%d").parse;
        var counter = 0;
        data.forEach(function(d) {
           etc
        });
    }
);

Sign up to request clarification or add additional context in comments.

6 Comments

Good catch on the scope problem, but I don't think that putting the object mergedcsv into d3.csv (which is expecting a URL in string form there) will work
Declared var mergedcsv; at top, then put console.log(mergedcsv); after d3.csv(mergedcsv, function(data) { However nothing came out on console and it said undefined for that variable. But console.log(mergedcsv); after mergedcsv = data.Body.toString(); does output to console. So didn't appear to put in scope?
Ha! I can't believe I didn't catch that! mergedcsv will only be populated after the callback fires at a future time. That call to d3.csv happens right afterward, so it will still be undefined. Also, reading d3.csv's docs states that it loads in from a URL so that wouldn't work anyway. I'll upload an answer that should work for you one sec…
Updated with a better answer. I saw docs that listed the csv parsing function as d3.csv.parse() and docs that listed it as d3.csvParse(). Try the second if the first isn't in your version.
Your previous answer was wrong, as you know, but this one is correct. FYI, OP is using D3 v3 (because of d3.time.format("%Y%m%d").parse;), so the correct choice is d3.csv.parse() indeed. Also, I'd suggest you to link the original API: github.com/d3/d3-3.x-api-reference/blob/master/CSV.md#parse
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.