【问题标题】:Exporting data in google analytics api to CSV file format using JAVA使用 JAVA 将谷歌分析 api 中的数据导出为 CSV 文件格式
【发布时间】:2014-07-01 15:59:57
【问题描述】:

大家好,我正在尝试使用 JAVA 将我从谷歌分析 api 查询的数据导出为 csv 文件格式。我对 java 很陌生,并且已经研究过使用 supercsv 和其他一些 csv 转换程序之类的东西。但是,我正在查看代码,感觉您可以简单地将数据输出为 csv 格式。如果有人有建议,那就太棒了!

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();
  }

这是我的查询

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();
    }
  }
}

这是我认为我需要修改才能将其输出到 csv 的部分。 谢谢大家


好的,所以我已经将它更改为使用缓冲写入器进行 CSV 转换,到目前为止我已经有了它......

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();
    }
}

对于作者的第一部分...

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();

    }

  }
}

给我错误说它不读它。有人想给我一些指点吗? 得到说 pw 无法解决的错误:/

【问题讨论】:

    标签: java csv google-analytics-api export-to-csv


    【解决方案1】:

    在你循环遍历列的地方只需添加一个逗号。

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

    您可能需要对其进行修改,因为此代码在每个元素(包括最后一个元素)的末尾添加了一个逗号。您需要确定列是否是行的最后一个元素,如果不是,则仅添加逗号。

    如果需要,您可以将其连接到 FileWriter(通过 BufferedWriter)并将其输出到 .csv 文件。将此代码放在方法的开头

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

    然后打印每一行你的循环看起来像这样

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

    再次,您需要确定列是否是行的最后一个元素。

    【讨论】:

    • 看这个:link
    • 是的,链接更好,它关闭了作者。
    • cool 只是让它完成整个逗号操作...除了编写代码使其不会将其添加到最后一列但无论如何...我将在哪里添加 FileWriter 代码,它会在 println 的东西之后还是之前,还是根本不重要
    • @user3634077 BufferedWriter = ... 代码位于函数的开头, bw.println(... 代替 System.out.println(column) 在 (String 列) :row 循环。
    • 好吧,很酷,刚刚检查了链接,但很奇怪。您知道是否有任何好的资源可以让缓冲写入器启动并运行?顺便感谢大家的帮助
    【解决方案2】:

    您可以使用此工具将数据从 Google Analytics 导出到 BigQuery,然后再导出到 csv - https://www.owox.com/products/bi/pipeline/google-analytics-to-google-bigquery-streaming/ 没有 Java。

    【讨论】:

    • 虽然我们喜欢指向外部资源的链接,但我们更喜欢仅将它们用于“备份”答案。我们不鼓励仅回答链接,因为它们在链接到期时无用。请更新您的答案以包括和解释。
    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多