StepOne.jsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import React, { useState, useEffect } from 'react'
  2. import { Form, Select, Input, message } from 'antd'
  3. import { getTableNamesList, getAilabList, getLakeTable } from '../services'
  4. export default function StepOne({
  5. drawDataForm,
  6. dataSourceList,
  7. updateTableStruct,
  8. updateDrawAilabStruct,
  9. updateDrawLakeStruct,
  10. }) {
  11. const [datasourceType, setDatasourceType] = useState(null)
  12. const [datasources, setDatasources] = useState()
  13. const [tableList, setTableList] = useState([])
  14. const [dsType, setDsType] = useState(null)
  15. // 获取数据源列表
  16. useEffect(() => {
  17. if (dataSourceList.length !== 0) {
  18. setDatasources(
  19. dataSourceList.map(item => {
  20. return {
  21. label: item.datasource_name,
  22. value: item.key,
  23. }
  24. })
  25. )
  26. }
  27. }, [dataSourceList])
  28. // 初始化表单类型
  29. useEffect(() => {
  30. const ds_id = drawDataForm.getFieldValue('datasource_name')
  31. const table_name = drawDataForm.getFieldValue('datasource_table')
  32. setDsType(drawDataForm.getFieldValue('datasource_type'))
  33. if (ds_id) {
  34. const type = dataSourceList.find(item => item.key === ds_id).datasource
  35. setDatasourceType(type)
  36. selectDatasource(ds_id)
  37. drawDataForm.setFieldValue('datasource_table', table_name)
  38. }
  39. }, [])
  40. // 选择数据源
  41. const selectDatasource = async val => {
  42. drawDataForm.setFieldValue('datasource_table', null)
  43. const type = dataSourceList.find(item => item.key === val)?.datasource
  44. setDatasourceType(type)
  45. const { data } = await getTableNamesList(val)
  46. if (data.code === 200) {
  47. const list = data.data.map(item => ({
  48. label: item,
  49. value: item,
  50. }))
  51. setTableList(list)
  52. } else {
  53. message.error('数据表加载失败')
  54. }
  55. }
  56. const selectAilab = async () => {
  57. const { data } = await getAilabList()
  58. if (data.code === 200) {
  59. const list = data.data.map((item, index) => {
  60. return {
  61. key: index,
  62. label: item,
  63. value: item,
  64. }
  65. })
  66. setTableList(list)
  67. } else {
  68. message.error(data.msg)
  69. }
  70. }
  71. const selectLake = async () => {
  72. const { data } = await getLakeTable()
  73. if (data.code === 200) {
  74. const list = data.data.map((item, index) => {
  75. return {
  76. key: index,
  77. label: item.table_name,
  78. value: item.table_name,
  79. table_path: item.table_path,
  80. }
  81. })
  82. setTableList(list)
  83. } else {
  84. message.error(data.msg)
  85. }
  86. }
  87. // 选择表
  88. const selectTable = val => {
  89. const currentTable = drawDataForm.getFieldValue('datasource_table')
  90. switch (dsType) {
  91. case 'datasource':
  92. const ds_id = drawDataForm.getFieldValue('datasource_name')
  93. updateTableStruct(ds_id, val)
  94. break
  95. case 'datalake':
  96. updateDrawLakeStruct(currentTable)
  97. drawDataForm.setFieldValue(
  98. 'reader_path',
  99. tableList.find(item => (item.value = currentTable)?.table_path)
  100. )
  101. break
  102. default:
  103. updateDrawAilabStruct(currentTable)
  104. break
  105. }
  106. }
  107. const selectDsType = val => {
  108. setDsType(val)
  109. switch (val) {
  110. case 'datasource':
  111. selectDatasource(drawDataForm.getFieldValue('datasource_name'))
  112. break
  113. case 'datalake':
  114. setDatasourceType('hive')
  115. selectLake()
  116. break
  117. default:
  118. setDatasourceType('hive')
  119. selectAilab()
  120. break
  121. }
  122. }
  123. return (
  124. <>
  125. <p
  126. style={{
  127. fontWeight: 600,
  128. borderLeft: '3px solid #1890ff',
  129. paddingLeft: '12px',
  130. }}>
  131. 配置提取源
  132. </p>
  133. <Form
  134. name="drawForm"
  135. form={drawDataForm}
  136. labelCol={{
  137. span: 2,
  138. }}
  139. wrapperCol={{
  140. span: 6,
  141. }}
  142. initialValues={{
  143. query_sql: '',
  144. where_param: '',
  145. reader_split_pk: '',
  146. delimiter: '',
  147. }}>
  148. <Form.Item
  149. label="数据源分类"
  150. name="datasource_type"
  151. rules={[
  152. {
  153. required: true,
  154. message: '请选择数据源分类!',
  155. },
  156. ]}>
  157. <Select
  158. allowClear
  159. options={
  160. ['1', '2', '3'].includes(sessionStorage.getItem('role'))
  161. ? [
  162. { label: '自添加数据源', value: 'datasource' },
  163. { label: 'AIlab', value: 'ailab' },
  164. { label: '数据湖', value: 'datalake' },
  165. ]
  166. : [{ label: 'AIlab', value: 'ailab' }]
  167. }
  168. onSelect={selectDsType}
  169. placeholder="请选择数据源分类"
  170. />
  171. </Form.Item>
  172. {dsType === 'datasource' && (
  173. <Form.Item
  174. label="选择数据源"
  175. name="datasource_name"
  176. rules={[
  177. {
  178. required: true,
  179. message: '请选择数据源!',
  180. },
  181. ]}>
  182. <Select
  183. showSearch
  184. optionFilterProp="label"
  185. allowClear
  186. options={datasources}
  187. onSelect={selectDatasource}
  188. placeholder="请选择数据源"
  189. />
  190. </Form.Item>
  191. )}
  192. <Form.Item
  193. label="选择表"
  194. name="datasource_table"
  195. rules={[
  196. {
  197. required: true,
  198. message: '请选择表',
  199. },
  200. ]}>
  201. <Select
  202. showSearch
  203. optionFilterProp="label"
  204. options={tableList}
  205. allowClear
  206. onSelect={selectTable}
  207. placeholder="请选择数据表"
  208. />
  209. </Form.Item>
  210. {datasourceType === 'mysql' ? (
  211. <>
  212. <Form.Item
  213. label="query sql"
  214. name="query_sql"
  215. wrapperCol={{ span: 10 }}>
  216. <Input.TextArea
  217. rows={5}
  218. placeholder="sql查询,一般用于多表关联查询时才用"
  219. />
  220. </Form.Item>
  221. <Form.Item label="切分主键" name="reader_split_pk">
  222. <Input placeholder="切分主键" />
  223. </Form.Item>
  224. <Form.Item
  225. label="条件语句"
  226. name="where_param"
  227. wrapperCol={{ span: 10 }}>
  228. <Input.TextArea rows={5} placeholder="where条件" />
  229. </Form.Item>
  230. </>
  231. ) : (
  232. <>
  233. <Form.Item
  234. label="选择路径"
  235. name="reader_path"
  236. wrapperCol={{ span: 10 }}
  237. rules={[
  238. {
  239. required: true,
  240. message: '请选择路径!',
  241. },
  242. ]}>
  243. <Input.TextArea rows={5} />
  244. </Form.Item>
  245. <Form.Item
  246. label="HDFS"
  247. name="reader_default_fs"
  248. rules={[
  249. {
  250. required: true,
  251. message: '请输入HDFS',
  252. },
  253. ]}>
  254. <Input placeholder="Hadoop hdfs文件系统namenode节点地址" />
  255. </Form.Item>
  256. <Form.Item
  257. label="文件类型"
  258. name="reader_file_type"
  259. rules={[
  260. {
  261. required: true,
  262. message: '请选择文件类型',
  263. },
  264. ]}>
  265. <Select
  266. options={[
  267. { label: 'text', value: 'text' },
  268. { label: 'csv', value: 'csv' },
  269. { label: 'orc', value: 'orc' },
  270. ]}
  271. allowClear
  272. placeholder="文件的类型"
  273. />
  274. </Form.Item>
  275. <Form.Item
  276. label="分隔符"
  277. name="reader_field_delimiter"
  278. rules={[
  279. {
  280. required: true,
  281. message: '请输入分隔符!',
  282. },
  283. ]}>
  284. <Input placeholder="读取字段的分隔符" />
  285. </Form.Item>
  286. </>
  287. )}
  288. </Form>
  289. </>
  290. )
  291. }