javadatabaseinfluxdbtdengineschemaless

something wrong when writing influx data into TDengine


I am trying to port my application which writing InfluxDb data to TDengine via its schemaless interface. I think it should be very easy but actually not.

Java code is below:

public void batchInsert() {
JSONObject json = new JSONObject();
json.put("f0", "tt");
json.put("f1", "aa");
json.put("f2", "tt");
json.put("f3", "aa");
json.put("f4", "tt");
json.put("f5", "aa");
json.put("f6", "tt");
json.put("f7", "aa");

BatchPoints batchPoints = BatchPoints.database(database)
    .precision(TimeUnit.MILLISECONDS)
    .build();
for (int i = 0; i < 1; i++) {
  Point point = Point.measurement(measurement)
      .time(System.currentTimeMillis() - 3600000, TimeUnit.MILLISECONDS)
      .addField("current", 10.2)
      .addField("voltage", 219 + i)
      .addField("phase", 0.32 + i)
      .addField("strAtri", "123456781234567812345678")
      .addField("json$j", json.toJSONString())
      .tag("__deviceId__", "sensor" + i)
      .build();
  try {
    Thread.sleep(10);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
  batchPoints.point(point);
}
try {
  JSONObject responseBody;

  String sql = "CREATE DATABASE IF NOT EXISTS " + database
      + " REPLICA 1 QUORUM 1 DAYS 1 KEEP 365 UPDATE 2;";
  HttpHeaders header = new HttpHeaders();
  header.setContentType(MediaType.APPLICATION_JSON);
  header.set("Authorization", getBasicAuth("root", "taosdata"));
  HttpEntity<String> httpEntity = new HttpEntity<>(sql, header);
  responseBody = restTemplate.postForObject(tdengineUrl, httpEntity, JSONObject.class);
  System.out.println("database=" + responseBody.get("status"));

  System.out.println(batchPoints.lineProtocol());
  ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(tdengineInfluxdbUrl, batchPoints.lineProtocol(), JSONObject.class);
  if (responseEntity.getStatusCodeValue() == 204) {
    System.out.println(responseEntity.getStatusCodeValue());
  }
} catch (Exception e) {
  e.printStackTrace();
}

I got result on the console:

sensor,deviceId=sensor0 current=10.2,json$j="{"f6":"tt","f7":"aa","f0":"tt","f1":"aa","f2":"tt","f3":"aa","f4":"tt","f5":"aa"}",phase=0.32,strAtri="123456781234567812345678",voltage=219i 1647569782596

org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error: [{"code":"internal error","message":"[0x21f] Invalid column length"}] at org.springframework.web.client.HttpServerErrorException.create(HttpServerErrorException.java:100) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:186) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:125) at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:782) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:740) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674) at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:449) at com.rootcloud.controller.InfluxController.batchInsert(InfluxController.java:112) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)

I don't know why and appreciated if someone can point me how to solve it.

thanks,


Solution

  • for json text, an escape character should be used before double quote and comma, for example "ste,t1=35 json_test=L"{"carNumber":"","image":"","outParkingSpace":1638927251000,"parkingSpaceStatus":0}" 1645329601000"