@@ -41,11 +41,22 @@ export async function createTable(fields: string[], tableName: string) {
41
41
return true ;
42
42
}
43
43
44
+ /**
45
+ * Create a table within clickhouse from the inferred schema from the json file
46
+ * if table already exists within the clickhouse function will return false
47
+ *
48
+ * @param s3Path URL location of the json within Minio
49
+ * @param s3Config Access key and Secrete key credentials to access Minio
50
+ * @param tableName The name of the table to be created within Minio
51
+ * @param groupByColumnName The column the created table will be ORDERED By within clickhouse
52
+ * @returns
53
+ */
54
+
44
55
export async function createTableFromJson (
45
56
s3Path : string ,
46
57
s3Config : { accessKey : string ; secretKey : string } ,
47
58
tableName : string ,
48
- key : string
59
+ groupByColumnName : string
49
60
) {
50
61
const client = createClient ( {
51
62
url,
@@ -54,19 +65,18 @@ export async function createTableFromJson(
54
65
55
66
const normalizedTableName = tableName . replace ( / - / g, '_' ) ;
56
67
57
- //check if the table exists
58
68
try {
59
69
const existsResult = await client . query ( {
60
70
query : `desc ${ normalizedTableName } ` ,
61
71
} ) ;
62
- logger . info ( `Table ${ normalizedTableName } already exists` ) ;
72
+ logger . debug ( `Table ${ normalizedTableName } already exists` ) ;
63
73
await client . close ( ) ;
64
74
return false ;
65
75
} catch ( error ) {
66
- logger . error ( `Table ${ normalizedTableName } does not exist` ) ;
76
+ logger . debug ( `Table ${ normalizedTableName } does not exist` ) ;
67
77
}
68
78
69
- const query = generateDDLFromJson ( s3Path , s3Config , normalizedTableName , key ) ;
79
+ const query = generateDDLFromJson ( s3Path , s3Config , normalizedTableName , groupByColumnName ) ;
70
80
await client . query ( { query } ) ;
71
81
await client . close ( ) ;
72
82
}
@@ -79,12 +89,12 @@ export function generateDDLFromJson(
79
89
s3Path : string ,
80
90
s3Config : { accessKey : string ; secretKey : string } ,
81
91
tableName : string ,
82
- key : string
92
+ groupByColumnName : string
83
93
) {
84
94
const query = `
85
95
CREATE TABLE IF NOT EXISTS \`default\`.${ tableName }
86
96
ENGINE = MergeTree
87
- ORDER BY ${ key } EMPTY
97
+ ORDER BY ${ groupByColumnName } EMPTY
88
98
AS SELECT *
89
99
FROM s3('${ s3Path } ', '${ s3Config . accessKey } ', '${ s3Config . secretKey } ', JSONEachRow)
90
100
SETTINGS schema_inference_make_columns_nullable = 0
0 commit comments