Skip to content

Commit 4c23ce2

Browse files
committed
doc
1 parent 85aeaa1 commit 4c23ce2

1 file changed

Lines changed: 26 additions & 14 deletions

File tree

README.md

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ console.log(rows) // [{ value: 1 }]
4848
```
4949

5050
## Sample (Streaming + Presigned URL)
51-
Stream external links into S3, then return a single presigned URL:
51+
Stream external links into S3 with gzip compression, then return a single presigned URL:
5252

5353
```ts
5454
import { executeStatement, mergeExternalLinks } from '@bitofsky/databricks-sql'
55-
import { GetObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3'
55+
import { GetObjectCommand, HeadObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3'
5656
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
57+
import { createGzip } from 'zlib'
58+
import { pipeline } from 'stream/promises'
59+
import { PassThrough } from 'stream'
5760

5861
const auth = {
5962
token: process.env.DATABRICKS_TOKEN!,
@@ -72,31 +75,40 @@ const result = await executeStatement(
7275

7376
const merged = await mergeExternalLinks(result, auth, {
7477
mergeStreamToExternalLink: async (stream) => {
75-
const key = `merged-${Date.now()}.csv`
76-
await s3.send(
78+
const key = `merged-${Date.now()}.csv.gz`
79+
const gzip = createGzip() // Compress with gzip and upload to S3
80+
const passThrough = new PassThrough()
81+
82+
const uploadPromise = s3.send(
7783
new PutObjectCommand({
7884
Bucket: bucket,
7985
Key: key,
80-
Body: stream,
86+
Body: passThrough,
8187
ContentType: 'text/csv',
88+
ContentEncoding: 'gzip',
8289
})
8390
)
8491

85-
const externalLink = await getSignedUrl(
86-
s3,
87-
new GetObjectCommand({ Bucket: bucket, Key: key }),
88-
{ expiresIn: 3600 }
89-
)
92+
await Promise.all([
93+
pipeline(stream, gzip, passThrough),
94+
uploadPromise,
95+
])
96+
97+
// Get actual uploaded size via HeadObject
98+
const head = await s3.send(new HeadObjectCommand({ Bucket: bucket, Key: key }))
99+
// Generate presigned URL valid for 1 hour
100+
const externalLink = await getSignedUrl(s3, new GetObjectCommand({ Bucket: bucket, Key: key }),{ expiresIn: 3600 })
90101

91102
return {
92-
externalLink,
93-
byte_count: 0,
94-
expiration: new Date(Date.now() + 3600 * 1000).toISOString(),
103+
externalLink, // Presigned URL to merged gzip CSV
104+
byte_count: head.ContentLength ?? 0, // Actual compressed size
105+
expiration: new Date(Date.now() + 3600 * 1000).toISOString(), // 1 hour from now
95106
}
96107
},
97108
})
98109

99-
console.log(merged.result?.external_links?.[0].external_link) // Presigned URL to merged CSV
110+
console.log(merged.result?.external_links?.[0].external_link) // Presigned URL to merged gzip CSV
111+
console.log(merged.result?.external_links?.[0].byte_count) // Actual compressed size
100112
```
101113

102114
## Sample (Progress with Metrics)

0 commit comments

Comments
 (0)