65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 HP Vertica 进行 .NET 编程

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.94/5 (7投票s)

2015 年 1 月 12 日

CPOL

5分钟阅读

viewsIcon

39253

downloadIcon

342

一篇关于使用大数据 HP Vertica 平台进行 .NET 数据层编程的文章

引言

作为我去年撰写的关于 Cassandra/.NET 编程的 CodeProject 文章的延续,今年的文章与 HP Vertica 的 .NET 编程相关,HP Vertica 是另一个大数据平台。

本文详细介绍了三个方面。首先是 HP 大数据平台 Vertica 及其维度。其次,.NET 应用程序层利用 HP Vertica 数据存储。最后,使用 .NET 代码示例进行 CRUD(创建、读取、更新、删除)数据操作。

数据仓库

数据仓库是企业各种业务系统收集的所有或重要部分数据的中央存储库。该术语由 W. H. Inmon 提出。

数据仓库的应用包括数据挖掘、网络挖掘和决策支持系统 (DSS)。

传统数据仓库

传统上,数据仓库托管在企业大型机服务器上。来自各种在线事务处理 (OLTP) 应用程序和其他源的数据被选择性地提取并组织在数据仓库数据库中,供分析应用程序和用户查询使用。数据仓库强调从不同源捕获数据以进行有用的分析和访问。

Traditional Data Warehouse

最新数据仓库

在最新的行业趋势中,数据仓库不仅与内部系统构建,还与相关的外部系统构建,以获得更好的清晰度和粒度。

商业智能 (BI) 和数据仓库的世界在不断发展。分析数据库服务器、大数据、NoSQL、Mashups、SaaS BI、数据虚拟化服务器、高级分析和 SOA 等技术已成为可用,并为组织提供了新的可能性。

Latest Data Warehouse

比较

通过详细分析,传统仓库和最新大数据仓库的比较如下:

Comparision between Traditional Data Warehouse and Big Data

HP Vertica

它是什么?

Vertica Systems 是一家分析数据库管理软件公司,成立于 2005 年,于 2011 年 3 月被 HP(惠普)收购。

基于集群、面向列的 Vertica 分析平台旨在管理大量快速增长的数据,并在用作数据仓库和其他查询密集型应用程序时提供非常快的查询性能。

概念

HP Vertica 建立在四个独特特征(四个 C)的基础上

  • 列存储:存储数据以优化访问
  • 压缩:用更少的物理存储存储更多数据
  • 集群:轻松扩展数据库,无论您需要三个节点还是 300 个节点
  • 持续性能:持续优化数据库,自动进行

为什么选择 Vertica?

对于传统数据存储程序员来说,这是一个非常直接的问题。

在现代商业中,可操作数据比以往任何时候都更加重要。快速有效地管理数据的能力是巨大的竞争优势——使用 Vertica 分析平台可以轻松获得这种优势。Vertica 是一个强大的分析数据库,允许用户以闪电般的速度从海量数据中提取价值。上述 4C 概念可以实现大数据需求。

谁在使用它?

一些最大的数据公司,包括 Facebook。Facebook 选择 HP Vertica 分析平台作为其大数据基础设施的一个组成部分。参考链接

Vertica 外部表

使用最新版本的 Vertica,现在可以在不将数据加载到 Vertica 的情况下,使用 Vertica 分析引擎的完整表达能力及其分析功能。下图对此进行了说明。

HP Vertica external tables

Microsoft 连接性

HP Vertica 允许您将 HP Vertica 服务器与包含先前安装在您系统上的 Microsoft 组件的环境集成。Connectivity Pack 包括 ADO.NET 客户端驱动程序以及用于与 Microsoft Visual Studio 和 Microsoft SQL Server 集成的附加工具。

HP Vertica 的 Microsoft Connectivity Pack 允许您将 HP Vertica 服务器与包含先前安装在您系统上的 Microsoft 组件的环境集成。Connectivity Pack 包括 ADO.NET 客户端驱动程序以及用于与 Microsoft Visual Studio 和 Microsoft SQL Server 集成的附加工具。

具体而言,Connectivity Pack 是一个增强的 .NET 数据提供程序包,并提供与以下 Microsoft 组件的集成:

  • 用于 Visual Studio 2008 的 Business Intelligence Development Studio (BIDS),用于 SQL Server 2012。BIDS 是一个基于客户端的应用程序,用于开发商业智能解决方案,并基于 Microsoft Visual Studio 开发环境;BIDS 包括特定于 SQL Server Business Intelligence 的附加项目类型。开发人员使用 BIDS 来开发业务解决方案。
  • 用于 Visual Studio 2010/2012 的 SQL Server Data Tool - Business Intelligence (SSDT-BI),用于 SQL Server 2012。SSDT-BI 取代了 Visual Studio 2010 和 2012 的 BIDS。它与 BIDS 具有相同的功能,提供了一个开发环境来开发商业智能解决方案。
  • SQL Server Analysis Services (SSAS),用于 SQL Server 2008 和 2012。使用 SSAS 进行 OLAP 和数据挖掘,使用 HP Vertica 作为创建立方体的数据源。
  • SQL Server Integration Services (SSIS),用于 SQL Server 2008 和 2012,它提供 SQL Server 类型映射,用于映射 HP Vertica 和 SQL Server 之间的数据类型。使用 SSIS 进行数据迁移、数据集成和工作流以及 ETL。
  • SQL Server Reporting Services (SSRS)。使用 SSRS 生成报表。

Connectivity between Microsoft and HP Vertica

Vertica .NET 集成

基础设施架构

在 Vertica .NET 集成工作中,基础设施架构设计如下:

App Infra Model

应用程序服务器层增加了 Vertica ODBC 连接组件,以建立 .NET 应用程序和 HP Vertica 数据层之间的通信。

数据层包含基于集群的 HP Vertica DB。表示层保持不变。

逻辑架构

根据行业标准的 n 层架构,HP Vertica .NET 集成应用程序的逻辑架构如下:

App Logical Model

连接代码

程序员应使用连接属性来配置 ADO.NET 客户端应用程序和 HP Vertica 数据库之间的连接。

您可以通过两种方式设置连接属性:

  1. 将属性名称和值作为连接字符串的一部分包含在传递给 VerticaConnection 的连接字符串中。
  2. VerticaConnectionStringBuilder 对象中设置属性,然后将该对象作为 string 传递给 VerticaConnection
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vertica.Data.VerticaClient;

namespace VerticaApp
{
    class Connectivity
    {
        static void Main(string[] args)
        {
            /*
             * HP Vertica ADO.NET drivers connection string builder
             */
            VerticaConnectionStringBuilder builder = new VerticaConnectionStringBuilder();

            /*
             * In the connection string, you need to specify the Host, 
             * Database, and User at the minimum
             */
            builder.Host = "192.10.10.10";
            builder.Database = "TestDB";
            builder.User = "AppUser";

            /*
             * Build an HP Vertica connection object by the connection strings
             * Then, initiate the connection by Open method
             */
            VerticaConnection conn = new VerticaConnection(builder.ToString());
            conn.Open();

            /*
             * Perform some data operations here..
             * Finally, connection object is closed
             */
            conn.Close();
        }
    }
}

CRUD 操作

CRUD 操作是解释应用程序和数据存储层之间功能的最佳用例。在提供的示例中,已详细附加了工作代码(删除除外)。

创建代码

您可以使用带参数的预编译语句分批加载数据。您还可以使用事务在遇到任何错误时回滚批处理加载。

如果您正在加载大量数据(超过 100MB),请考虑使用直接批插入。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vertica.Data.VerticaClient;

namespace VerticaApp
{
    class Create
    {
        static void Main(string[] args)
        {
            /*
             * HP Vertica ADO.NET drivers connection string builder
             */
            VerticaConnectionStringBuilder builder = new VerticaConnectionStringBuilder();

            /*
             * In the connection string, you need to specify the Host, 
             * Database, and User at the minimum
             */
            builder.Host = "192.10.10.10";
            builder.Database = "TestDB";
            builder.User = "AppUser";

            /*
             * Build an HP Vertica connection object by the connection strings
             * Then, initiate the connection by Open method
             */
            VerticaConnection conn = new VerticaConnection(builder.ToString());
            conn.Open();

            /*
             * Vertica INSERT operation into the existing Employee table with ID, 
             * First, Last, Mail, Department fields
             */
            VerticaCommand command = conn.CreateCommand();
            command.CommandText =
                  "INSERT into Employee values(1001, 'FirstName', 'LastName', 'eMail', 'Dept7')";
            Int32 rowsAdd = command.ExecuteNonQuery();
            Console.WriteLine(rowsAdd + " rows added!");

            /*
             * Finally, connection object is closed
             */
            conn.Close();
        }
    }
}

读取代码

附加的代码片段用于将内容从现有的 vertica 表检索到 .NET 应用程序层。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vertica.Data.VerticaClient;

namespace VerticaApp
{
    class Read
    {
        static void Main(string[] args)
        {
            /*
             * HP Vertica ADO.NET drivers connection string builder
             */
            VerticaConnectionStringBuilder builder = new VerticaConnectionStringBuilder();

            /*
             * In the connection string, you need to specify the Host, 
             * Database, and User at the minimum
             */
            builder.Host = "192.10.10.10";
            builder.Database = "TestDB";
            builder.User = "AppUser";

            /*
             * Build an HP Vertica connection object by the connection strings
             * Then, initiate the connection by Open method
             */
            VerticaConnection conn = new VerticaConnection(builder.ToString());
            conn.Open();

            /*
             * Vertica SELECT operation from the existing 2 relational tables
             * Employee and Department with DeptID field as reference
             */
            VerticaCommand command = conn.CreateCommand();
            command.CommandText =
                  "SELECT Mast.EmpID, Mast.FirstName, Mast.LastName, Mast.Mail, Ref.DeptName " +
                  "   FROM Employee Mast, Department Ref " +
                  "   WHERE Mast.DeptID == Ref.DeptID " +
                  "   ORDER BY Mast.EmpID " +
                  "   LIMIT 100 ";
            /*
             * To read data from the database use VerticaDataReader, 
             * an implementation of DbDataReader
             */
            VerticaDataReader reader = command.ExecuteReader();
            Console.WriteLine(" EmployeeName\t  Department");
            Console.WriteLine("------------\t  -------------------");
            
            /*
             * Data Read query is parsed to navigate the final result
             */
            int rows = 0;
            if (VerticaDataReader.HasRows())
            {
                while (reader.Read())
                {
                    Console.WriteLine("     " + reader[1] + reader[2] + "    \t  " + reader[4]);
                    ++rows;
                }
            }
            Console.WriteLine("Total Read Rows: (" + rows + ")");
            reader.Close();

            /*
             * Finally, connection object is closed
             */
            conn.Close();
        }
    }
}

更新代码

更新场景是根据给定条件修改现有 Vertica 表元素的內容。它由 Vertica 客户端库按如下方式派生:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vertica.Data.VerticaClient;

namespace VerticaApp
{
    class Update
    {
        static void Main(string[] args)
        {
            /*
             * HP Vertica ADO.NET drivers connection string builder
             */
            VerticaConnectionStringBuilder builder = new VerticaConnectionStringBuilder();

            /*
             * In the connection string, you need to specify the Host, 
             * Database, and User at the minimum
             */
            builder.Host = "192.10.10.10";
            builder.Database = "TestDB";
            builder.User = "AppUser";

            /*
             * Build an HP Vertica connection object by the connection strings
             * Then, initiate the connection by Open method
             */
            VerticaConnection conn = new VerticaConnection(builder.ToString());
            conn.Open();

            /*
             * Vertica UPDATE operation in the existing table Employee
             * Mail info is updated for the given FirstName and LastName
             * Updation driving fields are parameterized
             */
            VerticaCommand command = new VerticaCommand();
            command.Text =
                  "UPDATE Employee " +
                  "   SET Mail = " + "test@test.com" +
                  "   WHERE FirstName = @First AND " +
                  "   LastName = @Last ";
            command.Parameters.Add(new VerticaParameter( “First”, VerticaType.VarChar));
            command.Parameters.Add(new VerticaParameter( “Last”, VerticaType.VarChar));
            command.Parameters["First"] = 'Ganesan';
            command.Parameters["Last"] = 'Senthilvel';

            Int32 rowsUpdate = command.ExecuteNonQuery();


            /*
             * Finally, connection object is closed
             */
            conn.Close();
        }
    }
}

关注点

了解 HP Vertica 大数据和 .NET 编程概念以及 CRUD 代码级实现是很有趣的。

历史

  • 初始版本
© . All rights reserved.