【问题标题】:I am getting this error on inserting data in ClickHouse在 ClickHouse 中插入数据时出现此错误
【发布时间】:2022-01-16 17:10:38
【问题描述】:

未知错误字段:代码:27。 DB::ParsingException:无法解析 输入:预期的 '"' 之前:
'40","gmtoffset":0,"open":109.6,"high":109.6,"low":109.6,"close":109.6,"volume":0,"previousClose":108.4,"change": 1.2,"change_p":1.107}\n': (读取键时间戳的值时):执行时 JSONEachRowRowInputFormat:(在第 1 行)。 (CANNOT_PARSE_INPUT_ASSERTION_FAILED) (版本 21.11.5.33 (官方 构建))]

建表查询很简单

 CREATE TABLE IF NOT EXISTS RealTime ( code String,  timestamp DateTime,  gmtoffset Int32,  open Float32,  high Float32,  low Float32,  close Float32,  volume Float32,  previousClose Int32,  change Float32,  change_p Float32) ENGINE = MergeTree 
    PARTITION BY code 
    ORDER BY (timestamp);

插入查询:

realTime = {
  "code": "2010.SR",
  "timestamp": 1639311540,
  "gmtoffset": 0,
  "open": 108.4,
  "high": 109.8,
  "low": 107.8,
  "close": 109.6,
  "volume": 1326663,
  "previousClose": 108.4,
  "change": 1.2,
  "change_p": 1.107
}



const writeableStream = clickhouse.query(
      `INSERT into RealTime`,
      { format: "JSONEachRow" },
      (err) => {
        if (err) {
          console.error(err);
        }
        console.log("Insert complete!");
      }
    );

    writeableStream.write([JSON.stringify(realtime)]);

    writeableStream.end();

【问题讨论】:

    标签: sql node.js clickhouse


    【解决方案1】:

    列'previousClose'的类型是'Int32',但realTime中的值是float类型。

    我用 clickhouse java jdbc 驱动程序试过你的问题。将'previousClose'的值改为108,就可以正常工作了。

    这是我的java代码:

    public class ClickHouseDemo {
    
        private static final String CLICKHOUSE_DRIVER_NAME = "ru.yandex.clickhouse.ClickHouseDriver";
    
        /**
         * jdbc url
         */
        private static final String CK_URL = "your-ck-url";
    
        /**
         * jdbc database
         */
        private static final String CK_DB = "your-ck-db";
    
        /**
         * database user
         */
        private static final String CK_USER = "your-ck-user";
    
        /**
         * data password
         */
        private static final String CK_PASS = "your-ck-pass";
    
        public static void main(String[] args) throws SQLException {
            //correct string
            String validStr = "{\"code\": \"2010.SR\",\"timestamp\": 1639311540,\"gmtoffset\": 0,\"open\": 108.4,\"high\": 109.8,\"low\": 107.8,\"close\": 109.6,\"volume\": 1326663,\"previousClose\": 108,\"change\": 1.2,\"change_p\": 1.107}";
    
            //incorrect string
            String inValidStr = "{\"code\": \"2010.SR\",\"timestamp\": 1639311540,\"gmtoffset\": 0,\"open\": 108.4,\"high\": 109.8,\"low\": 107.8,\"close\": 109.6,\"volume\": 1326663,\"previousClose\": 108.4,\"change\": 1.2,\"change_p\": 1.107}";
    
            ClickHouseDemo clickHouseDemo = new ClickHouseDemo();
            ClickHouseConnection connection = clickHouseDemo.getConnection(CK_URL, CK_DB, CK_USER, CK_PASS);
            ClickHouseStatement chst = connection.createStatement();
            chst.write().addDbParam(ClickHouseQueryParam.QUERY_ID, UUID.randomUUID().toString()).sql("INSERT INTO RealTime_d").data(new ByteArrayInputStream(validStr.getBytes(StandardCharsets.UTF_8)), ClickHouseFormat.JSONEachRow).send();
        }
    
        private ClickHouseConnection getConnection(String url, String dbName, String user, String password) throws SQLException {
            try {
                Class.forName(CLICKHOUSE_DRIVER_NAME);
            } catch (ClassNotFoundException e) {
                throw new SQLException(e);
            }
    
            String conStr = String.format("jdbc:clickhouse://%s/%s", url, dbName);
    
            Properties properties = new Properties();
            properties.setProperty("user", user);
            properties.setProperty("password", password);
            return (ClickHouseConnection) DriverManager.getConnection(conStr, properties);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-02
      • 2017-09-26
      • 1970-01-01
      • 2012-10-30
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      相关资源
      最近更新 更多