在Java中,我在插入cassandra时遇到此错误,
我也尝试过时间戳数据类型,
那么是发布日期类型还是Java代码?你能建议代码更改吗
读取一个csv文件。
错误:
Exception in thread "main" com.datastax.driver.core.exceptions.InvalidQueryException: Invalid STRING constant (2012/11/11) for "fl_date" of type timeuuid
结构体:
fl_date timeuuid,
码:
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());
}
}
}
最佳答案
Cassandra中不存在将您的字符串日期时间转换为TimeUUID的直接方法。因此,您需要根据日期时间值制作timeuuid。这是一个简单的例子:
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())
.....
在这里,我们使用:com.datastax.driver.core.utils.UUIDs;可以在datastax cassandra驱动程序中找到。