2

Hi all I am in the process of trying to export data that i queried from google analytics api to a csv file format using JAVA. I am very new to java and have looked at using things like supercsv and some other csv converting programs. However I am looking at the code and feel like you can just simply output the data to a csv format. If anyone has suggestions it would be awesome!

private static GaData executeDataQuery(Analytics analytics, String profileId) throws IOException {

    return analytics.data().ga().get("ga:" + profileId, // Table Id. ga: + profile id.
        "today", // Start date.
        "today", // End date.
        "ga:pageviews, ga:visits, ga:uniquePageviews") // Metrics.
        .setDimensions("")
        .setSort("-ga:visits")
        .setFilters("ga:medium==organic")
        .setMaxResults(25)
        .execute();
  }

That is my query

private static void printGaData(GaData results) {
    System.out.println(
        "printing results for profile: " + results.getProfileInfo().getProfileName());

    if (results.getRows() == null || results.getRows().isEmpty()) {
      System.out.println("No results Found.");
    } else {

      // Print column headers.
      for (ColumnHeaders header : results.getColumnHeaders()) {
        System.out.printf("%30s", header.getName());
      }
      System.out.println();

      // Print actual data.
      for (List<String> row : results.getRows()) {
        for (String column : row) {
          System.out.printf("%30s", column);
        }
        System.out.println();
      }

      System.out.println();
    }
  }
}

This is the part that i think i need to modify in order to output it to csv. Thanks all


ok so i have changed it to use buffered writer for the CSV conversion so far i have it so..

public static void main(String[] args) throws IOException{

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    BufferedWriter out = new BufferedWriter(new FileWriter("c://data.csv"));
    try {
        String inputLine = null;
        do {
            inputLine=in.readLine();
            out.write(inputLine);
            out.newLine();
        } while (!inputLine.equalsIgnoreCase("eof"));
        System.out.print("Write Successful");
    } catch(IOException e1) {
        System.out.println("Error during reading/writing");
    } finally {
        out.close();
        in.close();
    }
}

for the first part of the writer...

private static void printGaData(GaData results) {

    System.out.println(
        "printing results for profile: " + results.getProfileInfo().getProfileName());

    if (results.getRows() == null || results.getRows().isEmpty()) {
      System.out.println("No results Found.");
    } else {

      // Print column headers.
      for (ColumnHeaders header : results.getColumnHeaders()) {
        pwt.print(header.getName() + ", ");
      }
      pw.println();

      // Print actual data.
      for (List<String> row : results.getRows()) {
        for (String column : row) {
          pw.print(column + ", ");
        }
        pw.println();
      }

      System.out.println();

    }

  }
}

giving me errors saying that it doesnt read it. Anyone wanna give me some pointers? getting errors that says pw cannot be resolved :/

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
zomdar
  • 263
  • 2
  • 6
  • 22

2 Answers2

0

Well where you loop through the columns just add a comma.

for(String column : row) {
  System.out.println(column + ", ");
}

You might need to modify it as this code adds a comma at the end of each element, including the last one. You would need to find out if column is the last element of row, and only add a comma if it ISN'T.

You could connect it to a FileWriter (through BufferedWriter) and output it to a .csv file if you wanted. Put this code at the beginning of your method

try {
  PrintWriter pw = new PrintWriter(BufferedWriter(new FileWriter("data.csv")));
}
catch(Exception e) {
  e.printStackTrace();
}

and then to print each line your loop would look like this

for (List<String> row : results.getRows()) {
  for (String column : row) {
    pw.print(row + ",");
  }
  pw.println();
}

again you would need to find out if column is the last element of row.

tevemadar
  • 12,389
  • 3
  • 21
  • 49
Luke Moll
  • 428
  • 6
  • 18
  • See this: [link](http://stackoverflow.com/questions/1994255/how-to-write-console-output-to-a-txt-file) – user1089599 May 13 '14 at 20:36
  • yeah that link is better, it closes the writer. – Luke Moll May 13 '14 at 20:43
  • cool just got it to do the whole comma thing...other than writing code to make it so that it doesnt add it to the last column but anyway...where would i add the FileWriter code, would it go after or before the println stuff or does it not matter at all – zomdar May 13 '14 at 21:04
  • @user3634077 the BufferedWriter = ... code goes at the beginning of your function and the bw.println(... goes instead of the System.out.println(column) inside your (String column:row loop. – Luke Moll May 13 '14 at 21:16
  • ok cool got that just checked the link out and it was weird to follow. Do you know if there is any good resources for getting the bufferedwriter to get up and running? thanks for all the help by the way – zomdar May 13 '14 at 21:23
0

You can export data from Google Analytics to BigQuery and then to csv using this tool - https://www.owox.com/products/bi/pipeline/google-analytics-to-google-bigquery-streaming/ Without Java.

Serge
  • 47
  • 1
  • 7
  • Whilst we love links to external sources, we prefer to use them only to "backup" an answer. We discourage link only answered because they are useless when the link expires. Please update your answer to include and explanation. – I.T Delinquent Jun 14 '19 at 09:53