Post by account_disabled on Jan 27, 2024 6:58:56 GMT 1
本文最初由 TeamSQL发表。感谢您对合作伙伴的支持,使 SitePoint 成为可能。 使用 COPY 命令可以轻松将大量数据导入 Redshift。为了演示这一点,我们将导入公开可用的数据集“用于情绪分析的 Twitter 数据”(有关更多信息,请参阅Sentiment140)。 注意:您可以使用 TeamSQL 连接到 AWS Redshift,TeamSQL 是一个多平台数据库客户端,可与 Redshift、PostgreSQL、MySQL 和 Microsoft SQL Server 配合使用,并在 Mac、Linux 和 Windows 上运行。您可以免费下载 TeamSQL。 在此处下载包含训练数据的 ZIP 文件。 红移星团 出于本示例的目的,Redshift 集群的配置规范如下: 集群类型:单节点 节点类型:dc1.large 区域:us-east-1a 在 Redshift 中创建数据库 运行以下命令在集群中创建新数据库: CREATE DATABASE sentiment; 在情感数据库中创建架构 运行以下命令在新创建的数据库中创建方案: CREATE SCHEMA tweets; 训练数据的模式(结构) CSV 文件包含已删除所有表情符号的 Twitter 数据。
有六列: 推文的极性(关键:0 = 负极,2 = 中性,4 = 正极) 推文的 ID(例如 2087) 推文的日期(例如 UTC 2009 年 5 月 16 日星期六 23:58:44) 查询(例如 lyx)。如果没有查询,则该值为NO_QUERY。 发推文的用户(例如 robotskilldozr) 推文 WhatsApp 号码数据 的文字(例如 Lyx 很酷) 创建训练数据表 首先在数据库中创建一个表来保存训练数据。您可以使用以下命令: CREATE TABLE tweets.training ( polarity int, id BIGINT, date_of_tweet varchar, query varchar, user_id varchar, tweet varchar(max) ) 将 CSV 文件上传到 S3 要使用 Redshift 的 COPY 命令,您必须将数据源(如果是文件)上传到 S3。 要将 CSV 文件上传到 S3: 解压缩您下载的文件。您将看到 2 个 CSV 文件:一个是测试数据(用于显示原始数据集的结构),另一个(文件名包含原始数据。我们将上传并使用后一个文件。 压缩文件。如果您使用的是 macOS 或 Linux,则可以通过在终端中运行以下命令来使用 GZIP 压缩文件:gzip training.1600000.processed.noemoticon.csv 使用 AWS S3 仪表板上传您的文件。
或者,您可以使用终端/命令行上传文件。为此,您必须安装AWS CLI,并在安装后使用aws configure您的访问权限和密钥对其进行配置(在终端中运行以启动配置向导)。 将 TeamSQL 连接到 Redshift 集群并创建架构 打开 TeamSQL(如果您没有 TeamSQL 客户端,请从teamsql.io下载)并添加新连接。 单击“创建连接”以启动“添加连接”窗口。 创建连接 选择 Redshift 并提供所需的详细信息以设置新连接。 不要忘记输入默认数据库名称! 测试连接,测试成功则保存。 默认情况下,TeamSQL 会在左侧导航面板中显示您添加的连接。要启用连接,请单击套接字图标。 右键单击默认数据库以打开新选项卡。 新标签 运行此命令以在数据库中创建新架构。 CREATE SCHEMA tweets; 右键单击连接项刷新左侧导航面板中的数据库列表。 为训练数据创建一个新表。创建新表查询 刷新连接,您的表应该出现在左侧列表中。 表已创建 使用COPY命令导入数据 要将数据从源文件复制到数据表,请运行以下命令此命令加载 CSV 文件并将数据导入到我们的tweets.training表中。
有六列: 推文的极性(关键:0 = 负极,2 = 中性,4 = 正极) 推文的 ID(例如 2087) 推文的日期(例如 UTC 2009 年 5 月 16 日星期六 23:58:44) 查询(例如 lyx)。如果没有查询,则该值为NO_QUERY。 发推文的用户(例如 robotskilldozr) 推文 WhatsApp 号码数据 的文字(例如 Lyx 很酷) 创建训练数据表 首先在数据库中创建一个表来保存训练数据。您可以使用以下命令: CREATE TABLE tweets.training ( polarity int, id BIGINT, date_of_tweet varchar, query varchar, user_id varchar, tweet varchar(max) ) 将 CSV 文件上传到 S3 要使用 Redshift 的 COPY 命令,您必须将数据源(如果是文件)上传到 S3。 要将 CSV 文件上传到 S3: 解压缩您下载的文件。您将看到 2 个 CSV 文件:一个是测试数据(用于显示原始数据集的结构),另一个(文件名包含原始数据。我们将上传并使用后一个文件。 压缩文件。如果您使用的是 macOS 或 Linux,则可以通过在终端中运行以下命令来使用 GZIP 压缩文件:gzip training.1600000.processed.noemoticon.csv 使用 AWS S3 仪表板上传您的文件。
或者,您可以使用终端/命令行上传文件。为此,您必须安装AWS CLI,并在安装后使用aws configure您的访问权限和密钥对其进行配置(在终端中运行以启动配置向导)。 将 TeamSQL 连接到 Redshift 集群并创建架构 打开 TeamSQL(如果您没有 TeamSQL 客户端,请从teamsql.io下载)并添加新连接。 单击“创建连接”以启动“添加连接”窗口。 创建连接 选择 Redshift 并提供所需的详细信息以设置新连接。 不要忘记输入默认数据库名称! 测试连接,测试成功则保存。 默认情况下,TeamSQL 会在左侧导航面板中显示您添加的连接。要启用连接,请单击套接字图标。 右键单击默认数据库以打开新选项卡。 新标签 运行此命令以在数据库中创建新架构。 CREATE SCHEMA tweets; 右键单击连接项刷新左侧导航面板中的数据库列表。 为训练数据创建一个新表。创建新表查询 刷新连接,您的表应该出现在左侧列表中。 表已创建 使用COPY命令导入数据 要将数据从源文件复制到数据表,请运行以下命令此命令加载 CSV 文件并将数据导入到我们的tweets.training表中。