十年河东,十年河西,莫欺少年穷
学无止境,精益求精
帮助类:
public class OtsHelper
{
public static string Endpoint = "https://xxx.cn-shanghai.ots.aliyuncs.com";
public static string InstanceName = "xxx";
/// <summary>
/// 获取阿里云ots客户端
/// </summary>
/// <returns></returns>
public static OTSClient GetOTSClient()
{
var config = new OTSClientConfig(Endpoint, AliyunParm.AccessKey, AliyunParm.AccessSecret, InstanceName);
// 禁止输出日志,默认是打开的。
config.OTSDebugLogHandler = null;
config.OTSErrorLogHandler = null;
// 使用OTSClientConfig创建一个OtsClient对象。
var otsClient = new OTSClient(config);
return otsClient;
}
/// <summary>
/// https://help.aliyun.com/zh/tablestore/developer-reference/updatetable-by-using-net-sdk?spm=a2c4g.11186623.0.0.5b01f732mxXX7K
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static CreateTableResponse CreateTable()
{
var otsClient = GetOTSClient();
var primaryKeySchema = new PrimaryKeySchema();
primaryKeySchema.Add("DeviceNo", ColumnValueType.String);
primaryKeySchema.Add("CreateTime", ColumnValueType.Integer);
//通过表名和主键列的schema创建一个tableMeta。
var tableMeta = new TableMeta("chargerecord", primaryKeySchema);
//设置预留读吞吐量为0,预留写吞吐量为0。
var reservedThroughput = new CapacityUnit(0, 0);
try
{
//构造CreateTableRequest对象。
var request = new CreateTableRequest(tableMeta, reservedThroughput);
//allowUpdate为false时,表示禁止UpdateRow相关更新写入操作。
request.TableOptions.AllowUpdate = true;
//数据生命周期至少为86400秒(一天)或-1(数据永不过期)。
request.TableOptions.TimeToLive = -1;
//调用client的CreateTable接口,如果没有抛出异常,则说明执行成功。
return otsClient.CreateTable(request);
}
//如果抛出异常,则说明失败,处理异常。
catch (Exception ex)
{
Console.WriteLine("Create table failed, exception:{0}", ex.Message);
}
return null;
}
public static void InsertData(OtsModels data)
{
var otsClient = GetOTSClient();
//定义行的主键,必须与创建表时的TableMeta中定义的一致。
var primaryKey = new PrimaryKey();
primaryKey.Add("DeviceNo", new ColumnValue(data.Deviceno));
primaryKey.Add("CreateTime", new ColumnValue(TimeHelper.GetTimestamp(data.Createtime)));
//定义要写入该行的属性列。
var attribute = new AttributeColumns();
attribute.Add("Rxsn", new ColumnValue(data.Rxsn));
attribute.Add("Txsn", new ColumnValue(data.Txsn));
attribute.Add("Chargetime", new ColumnValue(data.Chargetime));
attribute.Add("Elecs", new ColumnValue(data.Elecs));
try
{
//构造插入数据的请求对象,RowExistenceExpectation.IGNORE表示无论此行是否存在均会插入新数据。
var request = new PutRowRequest("chargerecord", new Condition(RowExistenceExpectation.IGNORE),
primaryKey, attribute);
//调用PutRow接口插入数据。
otsClient.PutRow(request);
//如果没有抛出异常,则说明执行成功。
Console.WriteLine("Put row succeeded.");
}
catch (Exception ex)
{
//如果抛出异常,则说明执行失败,处理异常。
Console.WriteLine("Put row failed, exception:{0}", ex.Message);
}
}
/// <summary>
/// 批量写入数据
/// </summary>
/// <param name="lst"></param>
public static void InsertDataBatch(List<OtsModels> lst)
{
var otsClient = GetOTSClient();
var request = new BatchWriteRowRequest();
var rowChanges = new RowChanges("chargerecord");
foreach(var data in lst)
{
var primaryKey = new PrimaryKey();
primaryKey.Add("DeviceNo", new ColumnValue(data.Deviceno));
primaryKey.Add("CreateTime", new ColumnValue(TimeHelper.GetTimestamp(data.Createtime)));
//定义要写入该行的属性列。
UpdateOfAttribute attribute = new UpdateOfAttribute();
attribute.AddAttributeColumnToPut("Rxsn", new ColumnValue(data.Rxsn));
attribute.AddAttributeColumnToPut("Txsn", new ColumnValue(data.Txsn));
attribute.AddAttributeColumnToPut("Chargetime", new ColumnValue(data.Chargetime));
attribute.AddAttributeColumnToPut("Elecs", new ColumnValue(data.Elecs));
rowChanges.AddUpdate(new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);
}
request.Add("chargerecord", rowChanges);
try
{
//调用BatchWriteRow接口写入数据。
var response = otsClient.BatchWriteRow(request);
var tableRows = response.TableRespones;
var rows = tableRows["chargerecord"];
//批量操作可能部分成功部分失败,需要检查每行的状态是否成功,详见示例代码的GitHub链接。
}
catch (Exception ex)
{
//如果抛出异常,则说明执行失败,处理异常。
Console.WriteLine("Batch put row failed, exception:{0}", ex.Message);
}
}
public static void DeleteOtsTable(string tableName)
{
var request = new DeleteTableRequest(tableName);
try
{
GetOTSClient().DeleteTable(request);
}
catch (Exception ex)
{
Console.WriteLine("Delete table failed, exception:{0}", ex.Message);
}
}
}
单表插入不再说了,看下批量插入
注:批量插入最多一次只允许插入200条。
以下案例分批次插入9000条数据
static void Main(string[] args)
{
//OtsHelper.CreateTable();
List<OtsModels> lst = new List<OtsModels>();
for(int i = 10; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
var obj = new OtsModels() { Chargetime = i, Deviceno = "C24000" + i, Elecs = i * j, Rxsn = "923000" + i, Txsn = "8525800" + i, Createtime = DateTime.Now.AddSeconds(i * j) };
lst.Add(obj);
}
}
var Total = lst.Count;
var PageSize = 200;
int pages = Total / PageSize;
int pageCount = Total % PageSize == 0 ? pages : pages + 1;
for(int i=1;i< pageCount+1; i++)
{
var lstresult = lst.Skip((i - 1) * PageSize).Take(PageSize).ToList();
OtsHelper.InsertDataBatch(lstresult);
}
Console.ReadLine();
}
@天才卧龙的波尔卡