CreateTableInstructions.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import { useParams } from 'common'
  2. import { Eye, EyeOff } from 'lucide-react'
  3. import { useMemo, useState } from 'react'
  4. import {
  5. Accordion,
  6. AccordionContent,
  7. AccordionItem,
  8. AccordionTrigger,
  9. Card,
  10. CardFooter,
  11. CardHeader,
  12. CardTitle,
  13. } from 'ui'
  14. import { CodeBlock } from 'ui-patterns/CodeBlock'
  15. import { useAnalyticsBucketWrapperInstance } from '../useAnalyticsBucketWrapperInstance'
  16. import { getPyicebergSnippet } from './CreateTableInstructions.constants'
  17. import CommandRender from '@/components/interfaces/Functions/CommandRender'
  18. import { convertKVStringArrayToJson } from '@/components/interfaces/Integrations/Wrappers/Wrappers.utils'
  19. import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
  20. import CopyButton from '@/components/ui/CopyButton'
  21. import { InlineLink } from '@/components/ui/InlineLink'
  22. import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query'
  23. import {
  24. getDecryptedValues,
  25. useVaultSecretDecryptedValueQuery,
  26. } from '@/data/vault/vault-secret-decrypted-value-query'
  27. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  28. import { DOCS_URL } from '@/lib/constants'
  29. export const CreateTableInstructions = ({
  30. hideHeader = false,
  31. className,
  32. }: {
  33. hideHeader?: boolean
  34. className?: string
  35. }) => {
  36. const { ref, bucketId } = useParams()
  37. const { data: project } = useSelectedProjectQuery()
  38. const [showKeys, setShowKeys] = useState(false)
  39. const [isFetchingSecretsOnCopy, setIsFetchingSecretsOnCopy] = useState(false)
  40. const { data: projectSettings } = useProjectSettingsV2Query({ projectRef: ref })
  41. const { data: wrapperInstance } = useAnalyticsBucketWrapperInstance({ bucketId })
  42. const wrapperValues = convertKVStringArrayToJson(wrapperInstance?.server_options ?? [])
  43. const s3AccessKeyVaultID = wrapperValues.vault_aws_access_key_id
  44. const s3SecretKeyVaultID = wrapperValues.vault_aws_secret_access_key
  45. const tokenVaultID = wrapperValues.vault_token
  46. const { data: decryptedS3AccessKey, isPending: isDecryptingS3AccessKey } =
  47. useVaultSecretDecryptedValueQuery(
  48. {
  49. projectRef: project?.ref,
  50. connectionString: project?.connectionString,
  51. id: s3AccessKeyVaultID,
  52. },
  53. { enabled: showKeys }
  54. )
  55. const { data: decryptedS3SecretKey, isPending: isDecryptingS3SecretKey } =
  56. useVaultSecretDecryptedValueQuery(
  57. {
  58. projectRef: project?.ref,
  59. connectionString: project?.connectionString,
  60. id: s3SecretKeyVaultID,
  61. },
  62. { enabled: showKeys }
  63. )
  64. const { data: decryptedToken, isPending: isDecryptingToken } = useVaultSecretDecryptedValueQuery(
  65. {
  66. projectRef: project?.ref,
  67. connectionString: project?.connectionString,
  68. id: tokenVaultID,
  69. },
  70. { enabled: showKeys }
  71. )
  72. const isFetchingSecretValues =
  73. showKeys && (isDecryptingS3AccessKey || isDecryptingS3SecretKey || isDecryptingToken)
  74. const snippetContent = useMemo(
  75. () =>
  76. getPyicebergSnippet({
  77. ref,
  78. warehouse: wrapperValues.warehouse,
  79. catalogUri: wrapperValues.catalog_uri,
  80. s3Endpoint: wrapperValues['s3.endpoint'],
  81. s3Region: projectSettings?.region,
  82. s3AccessKey: showKeys ? decryptedS3AccessKey : undefined,
  83. s3SecretKey: showKeys ? decryptedS3SecretKey : undefined,
  84. token: showKeys ? decryptedToken : undefined,
  85. }),
  86. [
  87. decryptedS3AccessKey,
  88. decryptedS3SecretKey,
  89. decryptedToken,
  90. projectSettings?.region,
  91. ref,
  92. showKeys,
  93. wrapperValues,
  94. ]
  95. )
  96. return (
  97. <Card className={className}>
  98. {!hideHeader && (
  99. <CardHeader>
  100. <CardTitle>Create your first table via Pyiceberg</CardTitle>
  101. </CardHeader>
  102. )}
  103. <Accordion defaultValue={['step-1']} type="multiple">
  104. <AccordionItem value="step-1">
  105. <AccordionTrigger className="px-6 py-3 text-sm">
  106. <div className="flex items-center gap-x-4">
  107. <div className="w-6 h-6 rounded-full border flex items-center justify-center text-xs font-mono">
  108. 1
  109. </div>
  110. <p className="prose text-sm font-normal">
  111. Set up Python project with <code>uv</code>
  112. </p>
  113. </div>
  114. </AccordionTrigger>
  115. <AccordionContent className="border-0 px-6 pt-1">
  116. <CommandRender
  117. commands={[
  118. {
  119. comment: '1. Install uv as a preferred package manager',
  120. command: 'curl -LsSf https://astral.sh/uv/install.sh | sh',
  121. jsx: () => 'curl -LsSf https://astral.sh/uv/install.sh | sh',
  122. },
  123. {
  124. comment: '2. Initialize a new Python project with uv',
  125. command: 'uv init <project-name>',
  126. jsx: () => 'uv init <project-name>',
  127. },
  128. {
  129. comment: '3. Install required packages',
  130. command: 'uv add pyiceberg pyarrow pandas',
  131. jsx: () => 'uv add pyiceberg pyarrow pandas',
  132. },
  133. ]}
  134. />
  135. </AccordionContent>
  136. </AccordionItem>
  137. <AccordionItem value="step-2">
  138. <AccordionTrigger className="px-6 py-3 text-sm">
  139. <div className="flex items-center gap-x-4">
  140. <div className="w-6 h-6 rounded-full border flex items-center justify-center text-xs font-mono">
  141. 2
  142. </div>
  143. <p className="prose text-sm font-normal">
  144. Replace <code>main.py</code> with the following snippet
  145. </p>
  146. </div>
  147. </AccordionTrigger>
  148. <AccordionContent className="border-0 px-6 pt-2">
  149. <p className="text-foreground mb-3 prose max-w-full text-sm">
  150. The following snippet creates a namespace <code>default</code>, then creates a sample
  151. table
  152. <code>events</code> into that namespace.
  153. </p>
  154. <div className="relative group">
  155. <CodeBlock
  156. hideCopy
  157. language="python"
  158. className="max-h-[330px]"
  159. value={snippetContent}
  160. />
  161. <div className="flex items-center gap-x-1.5 absolute top-2 right-2">
  162. <CopyButton
  163. type="default"
  164. loading={isFetchingSecretsOnCopy}
  165. asyncText={async () => {
  166. if (!!decryptedS3AccessKey && !!decryptedS3SecretKey && !!decryptedToken) {
  167. return getPyicebergSnippet({
  168. ref,
  169. warehouse: wrapperValues.warehouse,
  170. catalogUri: wrapperValues.catalog_uri,
  171. s3Endpoint: wrapperValues['s3.endpoint'],
  172. s3Region: projectSettings?.region,
  173. s3AccessKey: decryptedS3AccessKey,
  174. s3SecretKey: decryptedS3SecretKey,
  175. token: decryptedToken,
  176. })
  177. } else {
  178. setIsFetchingSecretsOnCopy(true)
  179. const decryptedSecrets = await getDecryptedValues({
  180. projectRef: project?.ref,
  181. connectionString: project?.connectionString,
  182. ids: [s3AccessKeyVaultID, s3SecretKeyVaultID, tokenVaultID],
  183. })
  184. setIsFetchingSecretsOnCopy(false)
  185. return getPyicebergSnippet({
  186. ref,
  187. warehouse: wrapperValues.warehouse,
  188. catalogUri: wrapperValues.catalog_uri,
  189. s3Endpoint: wrapperValues['s3.endpoint'],
  190. s3Region: projectSettings?.region,
  191. s3AccessKey: decryptedSecrets[s3AccessKeyVaultID],
  192. s3SecretKey: decryptedSecrets[s3SecretKeyVaultID],
  193. token: decryptedSecrets[tokenVaultID],
  194. })
  195. }
  196. }}
  197. />
  198. <ButtonTooltip
  199. type="default"
  200. className="w-7"
  201. loading={isFetchingSecretValues}
  202. onClick={() => setShowKeys(!showKeys)}
  203. icon={showKeys ? <EyeOff /> : <Eye />}
  204. tooltip={{
  205. content: {
  206. side: 'bottom',
  207. text: showKeys
  208. ? 'Hide keys'
  209. : isFetchingSecretValues
  210. ? 'Retrieving keys'
  211. : 'Reveal keys',
  212. },
  213. }}
  214. />
  215. </div>
  216. </div>
  217. </AccordionContent>
  218. </AccordionItem>
  219. <AccordionItem value="step-3">
  220. <AccordionTrigger className="px-6 py-3 text-sm">
  221. <div className="flex items-center gap-x-4">
  222. <div className="w-6 h-6 rounded-full border flex items-center justify-center text-xs font-mono">
  223. 3
  224. </div>
  225. <p className="prose text-sm font-normal">Run the Python script</p>
  226. </div>
  227. </AccordionTrigger>
  228. <AccordionContent className="border-0 px-6 pt-2">
  229. <CommandRender
  230. commands={[
  231. {
  232. comment: 'Run the main.py script with uv',
  233. command: 'uv run main.py',
  234. jsx: () => 'uv run main.py',
  235. },
  236. ]}
  237. />
  238. </AccordionContent>
  239. </AccordionItem>
  240. </Accordion>
  241. <CardFooter className="bg">
  242. <p className="text-xs text-foreground-light">
  243. Connecting to bucket with other Iceberg clients? Read more in our{' '}
  244. <InlineLink href={`${DOCS_URL}/guides/storage/analytics/examples/pyiceberg`}>
  245. documentation
  246. </InlineLink>
  247. .
  248. </p>
  249. </CardFooter>
  250. </Card>
  251. )
  252. }