DataFlare SDK

使用我们的官方SDK快速集成DataFlare平台功能

SDK概述
DataFlare SDK提供了与DataFlare平台交互的简单方式,支持多种编程语言。

使用我们的SDK,您可以轻松地:

  • 创建和管理数据流水线
  • 部署和配置SmartModules
  • 监控数据处理性能和指标
  • 设置告警和通知
  • 管理连接器和数据源

我们的SDK遵循一致的API设计,无论您选择哪种编程语言,都能获得相似的开发体验。

最新版本: v1.2.0

安装

npm install @dataflare/sdk --save

# 或使用 yarn
yarn add @dataflare/sdk
javascript

认证

在使用SDK之前,您需要获取API密钥。您可以在DataFlare控制台的API密钥设置页面生成新的密钥。

安全提示: 请妥善保管您的API密钥,不要将其提交到公共代码仓库或分享给未授权人员。

基本用法

import { DataflareClient } from '@dataflare/sdk';

// 初始化客户端
const client = new DataflareClient({
  apiKey: 'YOUR_API_KEY',
  endpoint: 'https://api.dataflare.io'
});

// 创建数据流水线
async function createPipeline() {
  try {
    const pipeline = await client.pipelines.create({
      name: 'my-first-pipeline',
      description: '我的第一个数据流水线',
      source: {
        type: 'kafka',
        config: {
          brokers: ['kafka:9092'],
          topic: 'input-data'
        }
      },
      processors: [
        {
          type: 'smartmodule',
          id: 'filter-module',
          config: {
            field: 'amount',
            threshold: 100
          }
        }
      ],
      sink: {
        type: 'elasticsearch',
        config: {
          hosts: ['http://elasticsearch:9200'],
          index: 'processed-data'
        }
      }
    });
    
    console.log('流水线创建成功:', pipeline.id);
    return pipeline;
  } catch (error) {
    console.error('创建流水线失败:', error);
    throw error;
  }
}

// 启动流水线
async function startPipeline(pipelineId) {
  try {
    await client.pipelines.start(pipelineId);
    console.log('流水线已启动');
  } catch (error) {
    console.error('启动流水线失败:', error);
    throw error;
  }
}
javascript

高级用法

import { DataflareClient } from '@dataflare/sdk';

// 初始化客户端
const client = new DataflareClient({
  apiKey: 'YOUR_API_KEY',
  endpoint: 'https://api.dataflare.io'
});

// 创建自定义 SmartModule
async function deploySmartModule() {
  try {
    const module = await client.smartmodules.deploy({
      name: 'custom-transformer',
      description: '自定义数据转换模块',
      version: '1.0.0',
      // 上传编译好的 WebAssembly 模块
      wasmFile: './path/to/module.wasm',
      // 模块配置模式
      configSchema: {
        type: 'object',
        properties: {
          fieldMapping: {
            type: 'object',
            additionalProperties: { type: 'string' }
          },
          defaultValues: {
            type: 'object',
            additionalProperties: true
          }
        }
      }
    });
    
    console.log('SmartModule 部署成功:', module.id);
    return module;
  } catch (error) {
    console.error('部署 SmartModule 失败:', error);
    throw error;
  }
}

// 监控流水线指标
async function monitorPipeline(pipelineId) {
  try {
    // 获取实时指标
    const metrics = await client.pipelines.getMetrics(pipelineId, {
      interval: '1m',
      duration: '1h'
    });
    
    console.log('处理速率:', metrics.processingRate, '记录/秒');
    console.log('延迟:', metrics.latency, 'ms');
    console.log('错误率:', metrics.errorRate, '%');
    
    // 设置告警
    await client.alerts.create({
      name: 'high-latency-alert',
      pipelineId,
      condition: {
        metric: 'latency',
        operator: '>',
        threshold: 1000, // 1秒
        duration: '5m'
      },
      actions: [
        {
          type: 'email',
          recipients: ['alerts@example.com']
        }
      ]
    });
    
    console.log('告警设置成功');
  } catch (error) {
    console.error('监控操作失败:', error);
    throw error;
  }
}
javascript

相关资源

API文档
详细了解DataFlare API的端点和参数
教程
通过实践教程学习如何使用SDK
示例项目
查看使用SDK构建的完整示例项目

需要帮助?

如果您在使用SDK时遇到任何问题,可以通过以下方式获取支持: