2
大家好我正在嘗試將我從google analytics api查詢的數據導出爲使用JAVA的csv文件格式。我對java非常陌生,並且曾經使用過supercsv和其他一些csv轉換程序。不過,我正在查看代碼,並感覺只需將數據簡單地輸出爲csv格式即可。如果有人有任何建議,它會很棒!將google analytics api中的數據導出爲CSV文件格式使用JAVA
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:/
好吧,我可以這樣做,它將它連接到FileWriter很難? – zomdar
看到這個: [鏈接](http://stackoverflow.com/questions/1994255/how-to-write-console-output-to-a-txt-file) – user1089599
是的,該鏈接是更好的,它關閉了作家。 –