Carl-Zhou-CN commented on code in PR #6975:
URL: https://github.com/apache/seatunnel/pull/6975#discussion_r1637657380


##########
seatunnel-connectors-v2/connector-clickhouse/src/main/java/org/apache/seatunnel/connectors/seatunnel/clickhouse/source/ClickhouseSourceFactory.java:
##########
@@ -37,11 +61,60 @@ public String factoryIdentifier() {
         return "Clickhouse";
     }
 
+    @Override
+    public <T, SplitT extends SourceSplit, StateT extends Serializable>
+            TableSource<T, SplitT, StateT> 
createSource(TableSourceFactoryContext context) {
+        ReadonlyConfig readonlyConfig = context.getOptions();
+        List<ClickHouseNode> nodes = 
ClickhouseUtil.createNodes(readonlyConfig);
+
+        String sql = readonlyConfig.get(SQL);
+        ClickHouseNode currentServer = 
nodes.get(ThreadLocalRandom.current().nextInt(nodes.size()));
+        try (ClickHouseClient client = 
ClickHouseClient.newInstance(currentServer.getProtocol());
+                ClickHouseResponse response =
+                        client.connect(currentServer)
+                                
.format(ClickHouseFormat.RowBinaryWithNamesAndTypes)
+                                .query(modifySQLToLimit1(sql))
+                                .executeAndWait()) {
+            TableSchema.Builder builder = TableSchema.builder();
+            int columnSize = response.getColumns().size();
+            for (int i = 0; i < columnSize; i++) {
+                String columnName = 
response.getColumns().get(i).getColumnName();
+                SeaTunnelDataType<?> seaTunnelDataType =
+                        TypeConvertUtil.convert(response.getColumns().get(i));
+                builder.column(
+                        PhysicalColumn.of(
+                                columnName, seaTunnelDataType, Long.MAX_VALUE, 
true, null, null));
+            }
+            CatalogTable catalogTable =
+                    CatalogTable.of(
+                            TableIdentifier.of(
+                                    factoryIdentifier(), 
readonlyConfig.get(DATABASE), "default"),
+                            builder.build(),
+                            Collections.emptyMap(),
+                            Collections.emptyList(),
+                            "",
+                            factoryIdentifier());
+            return () ->
+                    (SeaTunnelSource<T, SplitT, StateT>)
+                            new ClickhouseSource(nodes, catalogTable, sql);
+        } catch (ClickHouseException e) {
+            throw new ClickhouseConnectorException(
+                    SeaTunnelAPIErrorCode.CONFIG_VALIDATION_FAILED,
+                    String.format(
+                            "PluginName: %s, PluginType: %s, Message: %s",
+                            factoryIdentifier(), PluginType.SOURCE, 
e.getMessage()));
+        }
+    }
+

Review Comment:
       @Override
       public <T, SplitT extends SourceSplit, StateT extends Serializable>
               TableSource<T, SplitT, StateT> 
createSource(TableSourceFactoryContext context)
                       throws PrepareFailException {
           ReadonlyConfig config = context.getOptions();
   
           List<ClickHouseNode> servers =
                   ClickhouseUtil.createNodes(
                           config.get(HOST),
                           config.get(DATABASE),
                           config.get(SERVER_TIME_ZONE),
                           config.get(USERNAME),
                           config.get(PASSWORD));
   
           String sql = config.get(SQL);
           ClickHouseNode currentServer =
                   
servers.get(ThreadLocalRandom.current().nextInt(servers.size()));
   
           CatalogTable catalogTable;
           try (ClickHouseClient client = 
ClickHouseClient.newInstance(currentServer.getProtocol());
                   ClickHouseResponse response =
                           client.connect(currentServer)
                                   
.format(ClickHouseFormat.RowBinaryWithNamesAndTypes)
                                   .query(modifySQLToLimit1(sql))
                                   .executeAndWait()) {
   
               TableSchema.Builder builder = TableSchema.builder();
               List<ClickHouseColumn> columns = response.getColumns();
               columns.stream()
                       .forEach(
                               column -> {
                                   PhysicalColumn physicalColumn =
                                           PhysicalColumn.of(
                                                   column.getColumnName(),
                                                   
TypeConvertUtil.convert(column),
                                                   
Long.valueOf(column.getEstimatedLength()),
                                                   column.getScale(),
                                                   column.isNullable(),
                                                   null,
                                                   null);
                                   builder.column(physicalColumn);
                               });
               String catalogName = "clickhouse_catalog";
               catalogTable =
                       CatalogTable.of(
                               TableIdentifier.of(catalogName, 
config.get(DATABASE), "default", "default"),
                               builder.build(),
                               new HashMap<>(),
                               new ArrayList<>(),
                               "",
                               catalogName);
           } catch (ClickHouseException e) {
               throw new ClickhouseConnectorException(
                       SeaTunnelAPIErrorCode.CONFIG_VALIDATION_FAILED,
                       String.format(
                               "PluginName: %s, PluginType: %s, Message: %s",
                               factoryIdentifier(), PluginType.SOURCE, 
e.getMessage()));
           }
   A reference



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to