Search code examples
javaazure-data-explorerkusto-java-sdk

Equivalent Data Type in Java for Datetime in Kusto


I am using Datetime in Kusto. What should be the data type in Java to ingest to Kusto as datetime?

Example: I am setting StartTime:datetime, EndTime: datetime as column type. I have tried Date as type in java but Kusto is not accepting this type:

import java.util.Date;
    public Date startTime;
    public Date endTime;

What should be the type instead?


Solution

  • tl;dr

    Likely OffsetDateTime, or else Instant.

    Kusto datetime

    In Kusto, the datetime data type represents a moment as seen in UTC, that is, a date and time-of-day with an offset from UTC of zero hours-minutes-seconds. The datetime type resolves to 100-nanosecond units.

    java.time.Instant

    In Java, the Instant class represents a moment as seen in UTC. This type has a finer resolution, down to nanoseconds.

    So, the Instant class in Java is the natural counterpart to datetime in Kusto.

    java.time.OffsetDateTime

    However, your Java driver for Kusto may use another type, if it abides by the JDBC specification. JDBC is oriented to the SQL standard. Per that standard, the Java class OffsetDateTime is mapped to columns of a type akin to the SQL standard type TIMESTAMP WITH TIME ZONE.

    An OffsetDateTime is similar to Instant in that in represents a date and time-of-day as seen in a particular offset from UTC. In OffsetDateTime, the offset can be any number of hours-minutes-seconds whereas in Instant the offset is always zero.

    Also, Instant is the basic building-block class within the java.time framework. In contrast, OffsetDateTime is more flexible including more options for parsing & generating text representations.

    I am not a Kusto user, and their documentation is lacking, so I do not know which Java class your driver will map to a Kusto datetime. But I would guess mostly likely an OffsetDateTime, or otherwise an Instant.

    Conversion

    You can easily convert between the two.

    Instant instant = odt.toInstant() ;
    

    … and:

    OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
    

    Avoid legacy date-time classes

    Be aware that java.util.Date shown in your Question is one of the terribly flawed date-time classes from the earliest versions of Java. These legacy classes were years ago supplanted entirely by the modern java.time classes defined in JSR 310 for Java 8+.