1. 程式人生 > >解決Spark Arrays.toString(Dataset.collect())報錯

解決Spark Arrays.toString(Dataset.collect())報錯

以前用java.util.Arrays.toString(Dataset.collect())的時候是可以輸出的,不知為何,今天編譯的時候報了錯誤:

[INFO] -------------------------------------------------------------
[ERROR] /var/lib/jenkins/workspace/streamer-dev/streamer/src/main/java/cn/superid/streamer/compute/RegularQuery.java:[134,60] no suitable method found for toString(java.lang.Object)
    method java.util.Arrays.toString(long[]) is not applicable
      (argument mismatch; java.lang.Object cannot be converted to long[])
    method java.util.Arrays.toString(int[]) is not applicable
      (argument mismatch; java.lang.Object cannot be converted to int[])
    method java.util.Arrays.toString(short[]) is not applicable
      (argument mismatch; java.lang.Object cannot be converted to short[])
    method java.util.Arrays.toString(char[]) is not applicable
      (argument mismatch; java.lang.Object cannot be converted to char[])
    method java.util.Arrays.toString(byte[]) is not applicable
      (argument mismatch; java.lang.Object cannot be converted to byte[])
    method java.util.Arrays.toString(boolean[]) is not applicable
      (argument mismatch; java.lang.Object cannot be converted to boolean[])
    method java.util.Arrays.toString(float[]) is not applicable
      (argument mismatch; java.lang.Object cannot be converted to float[])
    method java.util.Arrays.toString(double[]) is not applicable
      (argument mismatch; java.lang.Object cannot be converted to double[])
    method java.util.Arrays.toString(java.lang.Object[]) is not applicable
      (argument mismatch; java.lang.Object cannot be converted to java.lang.Object[])
[ERROR] /var/lib/jenkins/workspace/streamer-dev/streamer/src/main/java/cn/superid/streamer/compute/RegularQuery.java:[145,54] no suitable method found for toString(java.lang.Object)

相關的java程式碼如下:

Dataset<PageView> inTimeRange = getInTimeRange(pageSet, last, unit, offset);
Timestamp epoch = Timestamp.valueOf(unit.update(last, offset + 1));
System.out.println("inTimeRange.collect()="+ Arrays.toString(inTimeRange.collect()));

無奈之下,加了型別強制轉換(強制轉換成Object[]):

Dataset<
PageView>
inTimeRange = getInTimeRange(pageSet, last, unit, offset); Timestamp epoch = Timestamp.valueOf(unit.update(last, offset + 1)); System.out.println("inTimeRange.collect()="+ Arrays.toString((Object[]) inTimeRange.collect()));

然後編譯執行都沒有問題了。