【发布时间】: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