Parquet 中除了有表示时间的 Timestamp 类型, 还有一种类型也可以被表示为 时间 —— int96

这是因为在某些大数据系统 (如 Hive, Impala) 中, 使用特殊的 int96 类型来表示时间, 具体时间可以精确到 ns (纳秒)

int96 为12个字节, 前8字节表示时间戳对应当天已过去的纳秒数,后4字节表示时间戳当天距离儒略历起始日已过去的天数。 并且前8个字节和 后4个字节 都是 小端序

具体查看可以通过 parquet-tools 工具查看对应的 schema

附上一段代码, 解析相应的时间列

package com.test.util;
 
import java.util.concurrent.TimeUnit;
 
import org.apache.parquet.io.api.Binary;
 
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
 
public class ParquetTimestampUtils {
	
		/**
		 * julian date的偏移量,2440588相当于1970/1/1
		 */
		private static final int JULIAN_EPOCH_OFFSET_DAYS = 2440588;
    private static final long MILLIS_IN_DAY = TimeUnit.DAYS.toMillis(1);
    private static final long NANOS_PER_MILLISECOND = TimeUnit.MILLISECONDS.toNanos(1);
 
    /**
     * Returns GMT timestamp from binary encoded parquet timestamp (12 bytes - julian date + time of day nanos).
     *
     * @param timestampBinary INT96 parquet timestamp
     * @return timestamp in millis, GMT timezone
     */
    public static long getTimestampMillis(Binary timestampBinary)
    {
        if (timestampBinary.length() != 12) {
        	return 0;
//            throw new PrestoException(HIVE_BAD_DATA, "Parquet timestamp must be 12 bytes, actual " + timestampBinary.length());
        }
        byte[] bytes = timestampBinary.getBytes();
 
        // little endian encoding - need to invert byte order
        long timeOfDayNanos = Longs.fromBytes(bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]);
        int julianDay = Ints.fromBytes(bytes[11], bytes[10], bytes[9], bytes[8]);
 
        return julianDayToMillis(julianDay) + (timeOfDayNanos / NANOS_PER_MILLISECOND);
    }
 
    private static long julianDayToMillis(int julianDay)
    {
        return (julianDay - JULIAN_EPOCH_OFFSET_DAYS) * MILLIS_IN_DAY;
    }
}