1

In java I get this error inserting into cassandra, I also tried timestamp data type,

So is issue date type, or java code ? Can you suggest code change

Reading a csv file.

Error:

Exception in thread "main" com.datastax.driver.core.exceptions.InvalidQueryException: Invalid STRING constant (2012/11/11) for "fl_date" of type timeuuid

Structure: fl_date timeuuid,

Code:

    System.out.println("Processing CSV file ...");
    List<Flight> flightList = ProcessFlightsCSV.processFlights("flights_from_pg.csv");

    for (Flight flight : flightList) {

        System.out.println(flight);

        Insert query = QueryBuilder.insertInto("flights")
                .value("id", flight.getId())
                .value("year", flight.getYear())
                .value("fl_date", flight.getFlDate())
                .value("airline_id", flight.getAirlineId())
                .value("carrier", flight.getCarrier())
                .value("fl_num", flight.getFlNum())
                .value("origin_airport_id", flight.getOriginAirportId())
                .value("origin", flight.getOrigin())
                .value("origin_city_name", flight.getOriginCityName())
                .value("origin_state_abr", flight.getOriginStateAbr())
                .value("dest", flight.getDest())
                .value("day_of_month", flight.getDayOfMonth())
                .value("dest_city_name", flight.getDestCityName())
                .value("dest_state_abr", flight.getDestStateAbr())
                .value("dep_time", flight.getDepTime())
                .value("arr_time", flight.getArrTime())
                .value("distance", flight.getDistance())
                ;

        session.execute(query.toString());
    }
}
}
aironman
  • 837
  • 5
  • 26
  • 55
LJF
  • 11
  • 2

2 Answers2

1

There is no direct method exists in Cassandra that would convert your string-date-time into the TimeUUID. So you need to make timeuuid from date-time value. Here is an simple example:

    long fl_date_in_mili = // some code to get millisecond time from flight.getFlDate(); DO it yourself, As we don't know what kind of datatype it is.
    ...
    ...
    UUID fl_date_time_uuid = UUIDs.endOf(fl_date_in_mili);
    // or
    UUID fl_date_time_uuid = UUIDs.startOf(fl_date_in_mili);

    ...
    ...
    ...
    Insert query = QueryBuilder.insertInto("flights")
            .value("id", flight.getId())
            ......
            .value("fl_date", flDateTimeUUID)
            .value("airline_id", flight.getAirlineId())
            .....

Here we use: com.datastax.driver.core.utils.UUIDs; which could be found in datastax cassandra driver.

MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37
0

You need to convert the date from string YYYY/MM/DD format to long format in ms and pass it on. You can convert that using java date utility functions. (How to convert a string Date to long millseconds).

CoolMandy
  • 11
  • 2