All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clickhouse.client.api.internal.TableSchemaParser Maven / Gradle / Ivy

There is a newer version: 0.7.0
Show newest version
package com.clickhouse.client.api.internal;

import com.clickhouse.client.ClickHouseResponse;
import com.clickhouse.client.api.metadata.TableSchema;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Properties;

public class TableSchemaParser {

    public TableSchemaParser() {
    }

    public TableSchema createFromBinaryResponse(ClickHouseResponse response, String tableName, String databaseName) {
        TableSchema schema = new TableSchema();
        schema.setTableName(tableName);
        schema.setDatabaseName(databaseName);
        Properties p = new Properties();
        response.records().forEach(record -> {
            String values = record.getValue(0).asString().replaceAll("\t", "\n");
            try {
                p.clear();
                p.load(new StringReader(values));
                schema.addColumn(p.getProperty("name"), p.getProperty("type"), p.getProperty("default_type"));
            } catch (IOException e) {
                throw new RuntimeException("Failed to parse table schema", e);
            }
        });
        return schema;
    }

    public TableSchema readTSKV(InputStream content, String table, String database) {
        TableSchema schema = new TableSchema();
        schema.setTableName(table);
        schema.setDatabaseName(database);
        Properties p = new Properties();
        try (BufferedReader r = new BufferedReader(new InputStreamReader(content))) {
            String line;
            while ((line = r.readLine()) != null) {
                p.clear();
                int lineLength = line.length();
                if (!line.trim().isEmpty()) {
                    p.load(new StringReader(line.replaceAll("\t", "\n")));
                    schema.addColumn(p.getProperty("name"), p.getProperty("type"), p.getProperty("default_type"));
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("Failed to parse table schema", e);
        }
        return schema;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy