0

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?

superninja
  • 3,114
  • 7
  • 30
  • 63
  • Can you clarify what do you mean by "Kusto is not accepting the type"? What type of action are you trying to do and what error do you get? – user3459685 Aug 28 '23 at 09:38

2 Answers2

3

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+.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

If you mean when you run a query:

KustoOperationResult result = queryClient.execute("Data", "AllDataTypes");
KustoResultSetTable table = result.getPrimaryResults();

The way to retrieve a column is to iterate over the rows, and grab it by its type.
For date, you can either have it as `java.sql.Date' or 'java.time.LocalDateTime', depends on which method you call:

while (table.hasNext) {
   table.next(); // advances to the next row
   java.sql.Date d = table.getDate("your-date-column"); 
   java.time.LocalDateTime d2 = table.getKustoDateTime("your-date-column");
}
user3459685
  • 87
  • 2
  • 5