跳到主要内容

快速开发流程

开发大体按以下步骤进行:

  1. 获取token
  2. token在数据源界面的开发者中心获取
  3. 创建数据源
  4. url: ds/create
  5. 参数: 数据源名称
  6. 创建成功后返回ds_id
  7. 示例代码:

python:

import json
import bdp_helper
# 创建数据源

def ds_create():

url = "https://open.bdp.cn/api/ds/create"
param = {
"access_token": "19de5e0297bfd55097733dfbc87ad79e", # token 必填
"name": "opends_ds_name", # 数据源名称 必填
"db_type": "opends" # 数据源类型,默认opends
}

result = bdp_helper.bdp_request(url, param=param)
json_result = json.dumps(result, indent=4)
print json_result
  1. 根据数据源id创建工作表

  2. url: tb/create

  3. 参数: 上一步返回的ds_id, 表名, 表结构等信息

  4. 创建成功后返回tb_id

  5. 示例代码:

python:

import json
import bdp_helper
# 创建工作表
def tb_create():
url = "https://open.bdp.cn/api/tb/create"
param = {
"access_token": "19de5e0297bfd55097733dfbc87ad79e", # token 必填
}

payload = {
"ds_id": "your_ds_id", # 数据源id
"name": "tb_name", # 工作表名
"schema": [ # 描述工作表结构(字段列表)
{
"name": "id", # 字段名
"type": "number", # 字段类型
"remark": "字段备注1", # 字段备注
"title": "字段别名1" # 字段别名
},
{
"name": "name", # 字段名
"type": "string", # 字段类型
"remark": "字段备注2", # 字段备注
},
{
"name": "birth", # 字段名
"type": "date", # 字段类型
"remark": "字段备注3", # 字段备注
}
],
"uniq_key": ["id", "name"], # 主键列表,列表中的字段将作为bdp去重标志
"title": "alias_of_tb", # 工作表别名,
"comment": "工作表备注", # 工作表备注
"dereplication": 1 # 是否自动去重标识, 1 去重, 0 不去重
}

result = bdp_helper.bdp_request(url, param=param, payload=payload)
json_result = json.dumps(result, indent=4)
print json_result
  1. 向工作表中写入数据

  2. url: data/insert

  3. 参数: 上一步返回的tb_id, 数据中的字段(有序的json列表)

  4. 数据: 二维json列表, 需转化为字符串的形式

  5. 示例代码:

python:

import json
import bdp_helper
# 调用data/insert只是将数据写入bdp缓存,要想在bdp看到结果,需要对相应的表调用tb_commit接口
def data_insert():
url = "https://open.bdp.cn/api/data/insert"
param = {
"access_token": "19de5e0297bfd55097733dfbc87ad79e", # token 必填
"tb_id": tb_id, # tb_id 必填
"fields": json.dumps([ # 字段名列表,没有传入的字段将被置空, 注意:这里需要传json字符串
"id",
"name",
"birth"
])
}

payload = [ # 数据列表
[ # 第一行数据
21,
"marry",
"2016-12-20 11:23:45"
],
[ # 第二行数据
23,
"smith",
"2016-12-19 11:25:32"
],

]

result = bdp_helper.bdp_request(url, param=param, payload=payload)
json_result = json.dumps(result, indent=4)
print json_result
  1. 提交数据
  2. url: tb/commit
  3. 参数: tb_id
  4. 示例代码:

python:

import json
import bdp_helper
# 提交工作表数据
def tb_commit():
url = "https://open.bdp.cn/api/tb/commit"
param = {
"access_token": "19de5e0297bfd55097733dfbc87ad79e", # token 必填
"tb_id": tb_id # tb_id 必填
}

result = bdp_helper.bdp_request(url, param=param)
json_result = json.dumps(result, indent=4)
print json_result
  1. 触发视图更新(批量更新接口)

  2. url: tb/update

  3. 参数: tb_id列表

  4. 示例代码:

python:

import json
import bdp_helper
# 级联更新工作表数据
def tb_update():
url = "https://open.bdp.cn/api/tb/update"
param = {
"access_token": "19de5e0297bfd55097733dfbc87ad79e", # token 必填
"tb_ids": json.dumps([tb_id]) # tb_id 必填
}

result = bdp_helper.bdp_request(url, param=param)
json_result = json.dumps(result, indent=4)
print json_result

SDK的快速开发流程,这里以C#为例子来进行说明,具体实现例子如下:

C#:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using System.Collections;

namespace OpenDSDemo
{
public class TBCreateBean
{
public string name { get; set; }
public string ds_id { get; set; }
public List<Dictionary<string, string>> schema { get; set; }
public List<string> uniq_key { get; set; }
public string title { get; set; }
public string remark { get; set; }
public int dereplication { get; set; }
}

class Program
{
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}

public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters = null, string body = null, Encoding charset = null)
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
if (body != null)
{
if (charset == null)
{
charset = Encoding.GetEncoding("utf-8");
}
byte[] data = charset.GetBytes(body);
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
return request.GetResponse() as HttpWebResponse;
}

public static string HttpRequest(string url, IDictionary<string, string> parameters=null, string body=null)
{
if (parameters != null)
{
StringBuilder buffer = new StringBuilder();
int i = 0;
foreach (string key in parameters.Keys)
{
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, parameters[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, parameters[key]);
}
i++;
}
url = string.Format("{0}?{1}", url, buffer.ToString());
}
HttpWebResponse response = Program.CreatePostHttpResponse(url, body: body);
Stream stream = response.GetResponseStream();
string result = "";
try
{
using (StreamReader reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
}
finally
{
stream.Close();
}
return result;
}


public static void DsCreate(string token, string dsName)
{
string url = "https://open.bdp.cn/api/ds/create";
IDictionary<string, string> parameters = new Dictionary<string, string>()
{
{ "access_token", token },
{ "name", dsName }
};
string res = HttpRequest(url, parameters:parameters);
Console.WriteLine(res);
}

public static void TbCreate(string token, string tbName, string dsId, List<Dictionary<string, string>> schema, List<string> uniqKey = null, string title = null, string remark = null, int dereplication = 1)
{
string url = "https://open.bdp.cn/api/tb/create";
Dictionary<string, string> parameters = new Dictionary<string, string>() { { "access_token", token } };
TBCreateBean bean = new TBCreateBean()
{
name=tbName,
ds_id=dsId,
schema=schema,
uniq_key=uniqKey,
title=title,
remark=remark,
dereplication=dereplication
};
string res = HttpRequest(url, parameters: parameters, body: JsonConvert.SerializeObject(bean));
Console.WriteLine(res);
}


public static void DataInsert<T>(string token, string tbId, List<string> fields, List<T> data)
{
string url = "https://open.bdp.cn/api/data/insert";
Dictionary<string, string> parameters = new Dictionary<string, string>()
{
{ "access_token", token },
{ "tb_id", tbId },
{ "fields", JsonConvert.SerializeObject(fields) }
};
string res = HttpRequest(url, parameters: parameters, body: JsonConvert.SerializeObject(data));
Console.WriteLine(res);
}


public static void TbCommit(string token, string tbId)
{
string url = "https://open.bdp.cn/api/tb/commit";
Dictionary<string, string> parameters = new Dictionary<string, string>()
{
{ "access_token", token },
{ "tb_id", tbId }
};
string res = HttpRequest(url, parameters: parameters);
Console.WriteLine(res);
}

static void Main(string[] args)
{
string token = "your_token";
//DsCreate(token, "csharp");

List<Dictionary<string, string>> schema = new List<Dictionary<string, string>>()
{
new Dictionary<string, string>(){ { "name", "id" }, { "type", "number" }, { "comment", "" } },
new Dictionary<string, string>(){ { "name", "name" }, { "type", "string" }, { "comment", "" } },
new Dictionary<string, string>(){ { "name", "birth" }, { "type", "date" }, { "comment", "" } },
};
//TbCreate(token, "csharp", "your_ds_id", schema);

List<string> fields = new List<string>() { "id", "name", "birth" };
List<ArrayList> data = new List<ArrayList>()
{
new ArrayList(){1, "tom", "1997-07-01 00:00:00"}
};
//DataInsert(token, "your_tb_id", fields, data);
//TbCommit(token, "your_tb_id");

Console.ReadLine();
}
}
}