# @aws-cdk/aws-glue 中文文档

规范地址：https://npmcn.com/package/%40aws-cdk/aws-glue/

版本：1\.204\.0
npm 元数据更新时间：2023-11-21T10:15:32.143Z
中文译文更新时间：2026-11-07T15:16:01.226Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40aws-cdk/aws-glue
原文对照：https://npmcn.com/package/%40aws-cdk/aws-glue/?lang=original
项目主页：https://github.com/aws/aws-cdk
源码仓库：https://github.com/aws/aws-cdk

弃用说明：AWS CDK v1 has reached End-of-Support on 2023-06-01\. This package is no longer being updated, and users should migrate to AWS CDK v2\. For more information on how to migrate, see https://docs\.aws\.amazon\.com/cdk/v2/guide/migrating-v2\.html

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：3.6 MB，31 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>= 14\.15\.0
- 运行依赖：12 个
- peer 依赖：@aws-cdk/assets 1\.204\.0，@aws-cdk/aws-cloudwatch 1\.204\.0，@aws-cdk/aws-ec2 1\.204\.0，@aws-cdk/aws-events 1\.204\.0，@aws-cdk/aws-iam 1\.204\.0，@aws-cdk/aws-kms 1\.204\.0，@aws-cdk/aws-logs 1\.204\.0，@aws-cdk/aws-s3 1\.204\.0，@aws-cdk/aws-s3-assets 1\.204\.0，@aws-cdk/core 1\.204\.0，@aws-cdk/custom-resources 1\.204\.0，constructs ^3\.3\.69
- 首次发布：2018-07-31T13:35:30.041Z
- 最新版本发布：2023-06-19T21:08:44.416Z
- 近 30 天下载量：10553（2026-10-08 至 2026-11-06，npm 官方下载统计）
- 已发布版本数：288（近 12 个月 0 个）

## 中文简介

AWS: Glue 的 CDK 构建库

## 安装

```sh
npm install @aws-cdk/aws-glue
```

也可以使用：`pnpm add @aws-cdk/aws-glue`、`yarn add @aws-cdk/aws-glue`、`bun add @aws-cdk/aws-glue`

## README

# AWS 粘结构造库

* * *

![End-of-Support](https://img.shields.io/badge/End--of--Support-critical.svg?style=for-the-badge)

> AWS CDK v1在2023-06-01上达到了支持结束. 此软件包不再更新,用户应该迁移到 AWS CDK v2.
> 
> 关于如何移徙的更多信息,请参见: [_移动到 AWS CDK v2_ 指南](https://docs.aws.amazon.com/cdk/v2/guide/migrating-v2.html)。 。 。 .

* * *

这个模块是 [AWS 云开发套件](https://github.com/aws/aws-cdk) 项目.

## 任务

A 类 `Job` 封装连接数据源的脚本,对其进行处理,然后将输出写入数据目标.

AWS Glue 支持的工作有3个类型: Spark ETL, Spark Streaming, 和 Python Shell 工作.

这个 `glue.JobExecutable` 允许您指定任务类型、使用的语言和任务所需的代码资产.

`glue.Code` 允许您从现有的 S3 位置或本地文件路径引用任务所需的不同代码资产.

### 火花任务

这些工作在 AWS Glue 管理的 Apache Spark 环境中运行 .

#### ETL 作业

使用 Apache Spark 批次处理 ETL 工作数据 .

```ts
declare const bucket: s3.Bucket;
new glue.Job(this, 'ScalaSparkEtlJob', {
  executable: glue.JobExecutable.scalaEtl({
    glueVersion: glue.GlueVersion.V2_0,
    script: glue.Code.fromBucket(bucket, 'src/com/example/HelloWorld.scala'),
    className: 'com.example.HelloWorld',
    extraJars: [glue.Code.fromBucket(bucket, 'jars/HelloWorld.jar')],
  }),
  description: 'an example Scala ETL job',
});
```

#### 流动工作

流线工作类似于ETL工作,只是它在数据流上执行ETL. 它使用Apache Spark结构流式框架. 一些 Spark 工作特性无法流出 ETL 工作 .

```ts
new glue.Job(this, 'PythonSparkStreamingJob', {
  executable: glue.JobExecutable.pythonStreaming({
    glueVersion: glue.GlueVersion.V2_0,
    pythonVersion: glue.PythonVersion.THREE,
    script: glue.Code.fromAsset(path.join(__dirname, 'job-script/hello_world.py')),
  }),
  description: 'an example Python Streaming job',
});
```

### Python 贝壳作业

一个 Python shell 任务将 Python 脚本作为 shell 运行,并支持一个依赖于您正在使用的 AWS Glue 版本的 Python 版本. 这可用于安排和运行不需要Apache Spark环境的任务.

```ts
declare const bucket: s3.Bucket;
new glue.Job(this, 'PythonShellJob', {
  executable: glue.JobExecutable.pythonShell({
    glueVersion: glue.GlueVersion.V1_0,
    pythonVersion: glue.PythonVersion.THREE,
    script: glue.Code.fromBucket(bucket, 'script.py'),
  }),
  description: 'an example Python Shell job',
});
```

见 [文档](https://docs.aws.amazon.com/glue/latest/dg/add-job.html) 用于获取有关在Glue中添加工作的更多信息.

## 连接

A 类 `Connection` 允许 Glue 任务,爬行者和开发端点访问某些类型的数据存储. 例如,要创建网络连接以连接VPC内的数据源:

```ts
declare const securityGroup: ec2.SecurityGroup;
declare const subnet: ec2.Subnet;
new glue.Connection(this, 'MyConnection', {
  type: glue.ConnectionType.NETWORK,
  // The security groups granting AWS Glue inbound access to the data source within the VPC
  securityGroups: [securityGroup],
  // The VPC subnet which contains the data source
  subnet,
});
```

如果您需要使用一个不存在作为静态成员的连接类型 `ConnectionType`你可以即兴表演 `ConnectionType` 对象,例如: `new glue.ConnectionType('NEW_TYPE')`。 。 。 .

见 [正在将连接添加到您的数据库](https://docs.aws.amazon.com/glue/latest/dg/populate-add-connection.html) 财务报告和审定财务报表 [连接结构](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-api-catalog-connections.html#aws-glue-api-catalog-connections-Connection) 文档中关于所支持的数据存储及其配置的更多信息.

## 安全配置

A 类 `SecurityConfiguration` 是一组安全属性,可用于AWS Glue在休息时加密数据.

```ts
new glue.SecurityConfiguration(this, 'MySecurityConfiguration', {
  securityConfigurationName: 'name',
  cloudWatchEncryption: {
    mode: glue.CloudWatchEncryptionMode.KMS,
  },
  jobBookmarksEncryption: {
    mode: glue.JobBookmarksEncryptionMode.CLIENT_SIDE_KMS,
  },
  s3Encryption: {
    mode: glue.S3EncryptionMode.KMS,
  },
});
```

默认情况下,会创建一个共享的 KMS 密钥,与需要的加密配置一起使用. 您也可以为每个加密配置提供自己的密钥, 例如用于 Cloud Watch 加密 :

```ts
declare const key: kms.Key;
new glue.SecurityConfiguration(this, 'MySecurityConfiguration', {
  securityConfigurationName: 'name',
  cloudWatchEncryption: {
    mode: glue.CloudWatchEncryptionMode.KMS,
    kmsKey: key,
  },
});
```

见 [文档](https://docs.aws.amazon.com/glue/latest/dg/encryption-security-configuration.html) 用于 Glue 加密由 Crawlers, Jobs, and Development Endpoints 编写的数据.

## 数据库

A 类 `Database` 是逻辑组合 `Tables` 在胶片目录.

```ts
new glue.Database(this, 'MyDatabase', {
  databaseName: 'my_database',
});
```

## 图表

Glue表描述了S3中的数据表格:其结构(列名和类型),数据位置(S3对象在S3桶中有一个共同的前缀),以及文件格式(Json, Avro, Parquet等)

```ts
declare const myDatabase: glue.Database;
new glue.Table(this, 'MyTable', {
  database: myDatabase,
  tableName: 'my_table',
  columns: [{
    name: 'col1',
    type: glue.Schema.STRING,
  }, {
    name: 'col2',
    type: glue.Schema.array(glue.Schema.STRING),
    comment: 'col2 is an array of strings' // comment is optional
  }],
  dataFormat: glue.DataFormat.JSON,
});
```

默认情况下, 将会创建 S3 桶来存储表格的数据, 但您可以手动通过 `bucket` 财务报告和审定财务报表 `s3Prefix`数字 :

```ts
declare const myBucket: s3.Bucket;
declare const myDatabase: glue.Database;
new glue.Table(this, 'MyTable', {
  bucket: myBucket,
  s3Prefix: 'my-table/',
  // ...
  database: myDatabase,
  tableName: 'my_table',
  columns: [{
    name: 'col1',
    type: glue.Schema.STRING,
  }],
  dataFormat: glue.DataFormat.JSON,
});
```

默认情况下,将创建一个S3桶来存储表格的数据并存储到桶根. 您也可以手动通过 `bucket` 财务报告和审定财务报表 `s3Prefix`数字 :

### 分区密钥

为了提高查询性能,表格可以指定 `partitionKeys` 数据分别存储和询问。 例如,您可以将表格按 `year` 财务报告和审定财务报表 `month` 以时间窗口优化查询:

```ts
declare const myDatabase: glue.Database;
new glue.Table(this, 'MyTable', {
  database: myDatabase,
  tableName: 'my_table',
  columns: [{
    name: 'col1',
    type: glue.Schema.STRING,
  }],
  partitionKeys: [{
    name: 'year',
    type: glue.Schema.SMALL_INT,
  }, {
    name: 'month',
    type: glue.Schema.SMALL_INT,
  }],
  dataFormat: glue.DataFormat.JSON,
});
```

### 分区索引

提高查询性能的另一种方法是指定分区索引. 如果没有分区索引 在表格中显示, AWS 粘贴加载表格的所有分区并使用 查询表达式。 随着分区数量的增加,查询需要更多的时间来运行. 与一个 索引,查询将尝试取取分区的子集,而不是装入分区的全部分区 页:1.

分区索引的密钥必须是表格分区密钥的一个子集. 你可以有一个 每个表格最多3个分区索引。 要指定分区索引, 您可以使用 `partitionIndexes` 属性 :

```ts
declare const myDatabase: glue.Database;
new glue.Table(this, 'MyTable', {
  database: myDatabase,
  tableName: 'my_table',
  columns: [{
    name: 'col1',
    type: glue.Schema.STRING,
  }],
  partitionKeys: [{
    name: 'year',
    type: glue.Schema.SMALL_INT,
  }, {
    name: 'month',
    type: glue.Schema.SMALL_INT,
  }],
  partitionIndexes: [{
    indexName: 'my-index', // optional
    keyNames: ['year'],
  }], // supply up to 3 indexes
  dataFormat: glue.DataFormat.JSON,
});
```

或者,你可以打电话 `addPartitionIndex()` 表格上的函数 :

```ts
declare const myTable: glue.Table;
myTable.addPartitionIndex({
  indexName: 'my-index',
  keyNames: ['year'],
});
```

## [加密](https://docs.aws.amazon.com/athena/latest/ug/encryption.html)

您可以启用表格数据的加密 :

-   `Unencrypted` - 文件没有加密。 默认加密设置 .
-   [S3 管理](https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingServerSideEncryption.html) - 服务器侧加密(`SSE-S3`) 有亚马逊S3管理的密钥.

```ts
declare const myDatabase: glue.Database;
new glue.Table(this, 'MyTable', {
  encryption: glue.TableEncryption.S3_MANAGED,
  // ...
  database: myDatabase,
  tableName: 'my_table',
  columns: [{
    name: 'col1',
    type: glue.Schema.STRING,
  }],
  dataFormat: glue.DataFormat.JSON,
});
```

-   [键](https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingKMSEncryption.html) - 服务器端加密(`SSE-KMS`AWS KMS 密钥由账户所有人管理.

```ts
declare const myDatabase: glue.Database;
// KMS key is created automatically
new glue.Table(this, 'MyTable', {
  encryption: glue.TableEncryption.KMS,
  // ...
  database: myDatabase,
  tableName: 'my_table',
  columns: [{
    name: 'col1',
    type: glue.Schema.STRING,
  }],
  dataFormat: glue.DataFormat.JSON,
});

// with an explicit KMS key
new glue.Table(this, 'MyTable', {
  encryption: glue.TableEncryption.KMS,
  encryptionKey: new kms.Key(this, 'MyKey'),
  // ...
  database: myDatabase,
  tableName: 'my_table',
  columns: [{
    name: 'col1',
    type: glue.Schema.STRING,
  }],
  dataFormat: glue.DataFormat.JSON,
});
```

-   [管理](https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingKMSEncryption.html) - 服务器端加密(`SSE-KMS`比如说 `Kms`,但有一个由 AWS 密钥管理处管理的 AWS KMS 密钥除外.

```ts
declare const myDatabase: glue.Database;
new glue.Table(this, 'MyTable', {
  encryption: glue.TableEncryption.KMS_MANAGED,
  // ...
  database: myDatabase,
  tableName: 'my_table',
  columns: [{
    name: 'col1',
    type: glue.Schema.STRING,
  }],
  dataFormat: glue.DataFormat.JSON,
});
```

-   [客户端](https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingClientSideEncryption.html#client-side-encryption-kms-managed-master-key-intro) - 客户端加密(`CSE-KMS`AWS KMS 密钥由账户所有人管理.

```ts
declare const myDatabase: glue.Database;
// KMS key is created automatically
new glue.Table(this, 'MyTable', {
  encryption: glue.TableEncryption.CLIENT_SIDE_KMS, 
  // ...
  database: myDatabase,
  tableName: 'my_table',
  columns: [{
    name: 'col1',
    type: glue.Schema.STRING,
  }],
  dataFormat: glue.DataFormat.JSON,
});

// with an explicit KMS key
new glue.Table(this, 'MyTable', {
  encryption: glue.TableEncryption.CLIENT_SIDE_KMS,
  encryptionKey: new kms.Key(this, 'MyKey'),
  // ...
  database: myDatabase,
  tableName: 'my_table',
  columns: [{
    name: 'col1',
    type: glue.Schema.STRING,
  }],
  dataFormat: glue.DataFormat.JSON,
});
```

_注意: 您无法提供 `Bucket` 创建时 `Table` 如果您想要使用服务器侧加密( u)`KMS`, (中文(简体) ), `KMS_MANAGED` 或者说 `S3_MANAGED`(中文(简体) )_。 。 。 .

## 类型

一张桌子的图案是一列,每列都有 `name` (单位:千美元) `type`类型是递归结构,由原始和复杂的类型组成:

```ts
declare const myDatabase: glue.Database;
new glue.Table(this, 'MyTable', {
  columns: [{
    name: 'primitive_column',
    type: glue.Schema.STRING,
  }, {
    name: 'array_column',
    type: glue.Schema.array(glue.Schema.INTEGER),
    comment: 'array<integer>',
  }, {
    name: 'map_column',
    type: glue.Schema.map(
      glue.Schema.STRING,
      glue.Schema.TIMESTAMP),
    comment: 'map<string,string>',
  }, {
    name: 'struct_column',
    type: glue.Schema.struct([{
      name: 'nested_column',
      type: glue.Schema.DATE,
      comment: 'nested comment',
    }]),
    comment: "struct<nested_column:date COMMENT 'nested comment'>",
  }],
  // ...
  database: myDatabase,
  tableName: 'my_table',
  dataFormat: glue.DataFormat.JSON,
});  
```

### 初级

#### 数字

| 名称 | 类型 | 评论 |
| --- | --- | --- |
| 浮图 | 常数 | 32 位单精度浮点数 |
| 进口 | 常数 | 一个32位的签名值,以两个补充格式,最小值为-2^31,最大值为-2^31- 1个 |
| 丢弃 | 常数 | 64位双精度浮点数 |
| 大键 | 常数 | 一个64位签名的INTEGER,以两个补充格式,最小值为-2^63,最大值为-2^63-1 |
| 小型小号 | 常数 | 一个16位签名的INTEGER,以两个补充格式,最小值为-2^15,最大值为-2^15- 1个 |
| 小叮当 INT | 常数 | 一个8位签名的INTEGER,2个补充格式,最小值为-2^7,最大值为-2^7- 1个 |

#### 日期和时间

| 名称 | 类型 | 评论 |
| --- | --- | --- |
| 日期 | 常数 | 一个UNIX格式的日期,例如YYY-MM-DD. |
| 时间 | 常数 | 在UNiX格式中的日期和时间即时,如yyy-mm-dd hh:mm:s\[.f.\]. 例如,TIMESTAMP '2008-09-15 03:04:05.324'. 此格式使用会话时区. |

#### 字符串

| 名称 | 类型 | 评论 |
| --- | --- | --- |
| 绳子 | 常数 | 以单引号或双引号附加的字符串文字 |
| 小数点( 精度: 数字, 缩放? ) | 函数 | `precision` 是数字的总数. `scale` (可选)是分数部分的位数,默认为0. 例如,使用这些类型定义:小数(11,5),小数(15) |
| 字符( 长度: 数字) | 函数 | 固定长度字符数据, 指定长度在 1 至 255 之间, 如 char(10) |
| varchar(长:数字) | 函数 | 可变长度字符数据,具体长度介于1至65535之间,例如 varchar(10) |

#### 杂项

| 名称 | 类型 | 评论 |
| --- | --- | --- |
| 玻利维亚 | 常数 | 值为 `true` 财务报告和审定财务报表 `false` |
| 比利时 | 常数 | 值为二进制 |

### 复杂

| 名称 | 类型 | 评论 |
| --- | --- | --- |
| 数组( 项目类型: 类型) | 函数 | 其他类型的数组 |
| 映射( 密钥类型: 类型, 值) | 函数 | 某个原始密钥类型的映射到任何值类型的地图 |
| struct(列:列 \[\]) | 函数 | 包含单独命名和打入的圆柱形结构 |

## 常见问题

### @aws-cdk/aws-glue 是什么？

AWS: Glue 的 CDK 构建库注意：该包已被作者标记为弃用——AWS CDK v1 has reached End-of-Support on 2023-06-01\. This package is no longer being updated, and users should migrate to AWS CDK v2\. For more information on how to migrate, see https://docs\.aws\.amazon\.com/cdk/v2/guide/migrating-v2\.html

### 如何安装 @aws-cdk/aws-glue？

在项目目录运行 npm install @aws-cdk/aws-glue，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@aws-cdk/assets（1\.204\.0）、@aws-cdk/aws-cloudwatch（1\.204\.0）、@aws-cdk/aws-ec2（1\.204\.0）、@aws-cdk/aws-events（1\.204\.0）、@aws-cdk/aws-iam（1\.204\.0）、@aws-cdk/aws-kms（1\.204\.0）、@aws-cdk/aws-logs（1\.204\.0）、@aws-cdk/aws-s3（1\.204\.0）、@aws-cdk/aws-s3-assets（1\.204\.0）、@aws-cdk/core（1\.204\.0）、@aws-cdk/custom-resources（1\.204\.0）、constructs（^3\.3\.69）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @aws-cdk/aws-glue 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @aws-cdk/aws-glue 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @aws-cdk/aws-glue 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>= 14\.15\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @aws-cdk/aws-glue 可以商用吗？

@aws-cdk/aws-glue 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @aws-cdk/aws-glue 的下载量有多少？

根据 npm 官方下载统计，@aws-cdk/aws-glue 在 2026/10/08 – 2026/11/06 这 30 天内被下载 10,553 次，日均约 352 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @aws-cdk/aws-glue 还在维护吗？

最新版本 1\.204\.0 发布于 2023/06/20（3 年前）；至今共发布 288 个版本，其中过去 12 个月发布了 0 个；首次发布于 2018/07/31。作者已将其标记为弃用，新项目建议寻找替代方案。

