using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aliyun.OTS;
using Aliyun.OTS.DataModel;
using Aliyun.OTS.Request;
using Aliyun.OTS.Response;
using Aliyun.OTS.DataModel.ConditionalUpdate;
using Aliyun.OTS.Samples; namespace Sample1
{
class Program
{
private static string TableName = "SampleTable";
static void Main(string[] args)
{ #region 打开连接
// 使用OTSClientConfig创建一个OtsClient对象
var otsClient = Config.GetClient();
#endregion #region 创建表
////创建主键列的schema,包括PK的个数,名称和类型
////第一个PK列为整数,名称是pk0,这个同时也是分片列
////第二个PK列为字符串,名称是pk1
//var primaryKeySchema = new PrimaryKeySchema();
//primaryKeySchema.Add("pk0", ColumnValueType.Integer);
//primaryKeySchema.Add("pk1", ColumnValueType.String);
////通过表名和主键列的schema创建一个tableMeta
//var tableMeta = new TableMeta("SampleTable", primaryKeySchema);
//// 设定预留读吞吐量为0,预留写吞吐量为0
//var reservedThroughput = new CapacityUnit(0, 0);
//try
//{
// // 构造CreateTableRequest对象
// var request = new CreateTableRequest(tableMeta, reservedThroughput);
// // 调用client的CreateTable接口,如果没有抛出异常,则说明成功,否则失败
// otsClient.CreateTable(request);
// Console.WriteLine("Create table succeeded.");
//}
//// 处理异常
//catch (Exception ex)
//{
// Console.WriteLine("Create table failed, exception:{0}", ex.Message);
//}
#endregion #region 列出所有表
//var request = new ListTableRequest();
//try
//{
// var response = otsClient.ListTable(request);
// foreach (var tableName in response.TableNames)
// {
// Console.WriteLine("Table name:{0}", tableName);
// }
// Console.WriteLine("List table succeeded.");
//}
//catch (Exception ex)
//{
// Console.WriteLine("List table failed, exception:{0}", ex.Message);
//}
#endregion #region 获取表的描述信息
//try
//{
// var request = new DescribeTableRequest("SampleTable");
// var response = otsClient.DescribeTable(request);
// // 打印表的描述信息
// Console.WriteLine("Describe table succeeded.");
// Console.WriteLine("LastIncreaseTime: {0}", response.ReservedThroughputDetails.LastIncreaseTime);
// Console.WriteLine("LastDecreaseTime: {0}", response.ReservedThroughputDetails.LastDecreaseTime);
// Console.WriteLine("NumberOfDecreaseToday: {0}", response.ReservedThroughputDetails.LastIncreaseTime);
// Console.WriteLine("ReadCapacity: {0}", response.ReservedThroughputDetails.CapacityUnit.Read);
// Console.WriteLine("WriteCapacity: {0}", response.ReservedThroughputDetails.CapacityUnit.Write);
//}
//catch (Exception ex)
//{
// //如果抛出异常,则说明执行失败,打印错误信息
// Console.WriteLine("Describe table failed, exception:{0}", ex.Message);
//}
#endregion #region 删除表
//var request = new DeleteTableRequest("SampleTable");
//try
//{
// otsClient.DeleteTable(request);
// Console.Writeline("Delete table succeeded.");
//}
//catch (Exception ex)
//{
// Console.WriteLine("Delete table failed, exception:{0}", ex.Message);
//}
#endregion /*表格存储的 SDK 提供了 PutRow、GetRow、UpdateRow 和 DeleteRow 等单行操作的接口。*/ #region 插入数据
///*
// RowExistenceExpectation.IGNORE 表示不管此行是否已经存在,都会插入新数据,如果之前有会被覆盖。
// RowExistenceExpectation.EXPECT_EXIST 表示只有此行存在时,才会插入新数据,此时,原有数据也会被覆盖。
// RowExistenceExpectation.EXPECT_NOT_EXIST 表示只有此行不存在时,才会插入数据,否则不执行。
// */
//// 定义行的主键,必须与创建表时的TableMeta中定义的一致
//var primaryKey = new PrimaryKey();
//primaryKey.Add("pk0", new ColumnValue(0));
//primaryKey.Add("pk1", new ColumnValue("abc"));
//// 定义要写入改行的属性列
//var attribute = new AttributeColumns();
//attribute.Add("col0", new ColumnValue(0));
//attribute.Add("col1", new ColumnValue("a"));
//attribute.Add("col2", new ColumnValue(true));
//try
//{
// // 构造插入数据的请求对象,RowExistenceExpectation.IGNORE表示不管此行是否存在都执行
// var request = new PutRowRequest("SampleTable", 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);
//}
#endregion #region 设置条件插入数据
/*
条件不仅支持单个条件,也支持多个条件组合。例如,col1 大于 5 且 pk2 小于’xyz’时插入数据。
属性列和主键列都支持条件。
当条件中的列在某行不存在时,可以通过 RelationCondition 中的 PassIfMissing 控制,默认是 true。
*/
//// 定义行的主键,必须与创建表时的TableMeta中定义的一致
//var primaryKey = new PrimaryKey();
//primaryKey.Add("pk0", new ColumnValue(0));
//primaryKey.Add("pk1", new ColumnValue("abc"));
//// 定义要写入改行的属性列
//AttributeColumns attribute = new AttributeColumns();
//attribute.Add("col0", new ColumnValue(0));
//attribute.Add("col1", new ColumnValue("a"));
//attribute.Add("col2", new ColumnValue(true));
//var request = new PutRowRequest("SampleTable", new Condition(RowExistenceExpectation.EXPECT_EXIST),
// primaryKey, attribute);
//// 当col0列的值大于24的时候,允许再次put row,覆盖掉原值
//try
//{
// request.Condition.ColumnCondition = new RelationalCondition("col0",
// RelationalCondition.CompareOperator.GREATER_THAN,
// new ColumnValue(24));
// otsClient.PutRow(request);
// Console.WriteLine("Put row succeeded.");
//}
//catch (Exception ex)
//{
// Console.WriteLine("Put row failed. error:{0}", ex.Message);
//}
#endregion #region 读取一行数据
//// 定义行的主键,必须与创建表时的TableMeta中定义的一致
//PrimaryKey primaryKey = new PrimaryKey();
//primaryKey.Add("pk0", new ColumnValue(0));
//primaryKey.Add("pk1", new ColumnValue("abc"));
//try
//{
// // 构造查询请求对象,这里未指定读哪列,默认读整行
// var request = new GetRowRequest("SampleTable", primaryKey);
// // 调用GetRow接口查询数据
// var response = otsClient.GetRow(request);
// // 输出此行的数据,这里省略,详见下面GitHub的链接
// // 如果没有抛出异常,则说明成功
// PrimaryKey primaryKeyRead = response.PrimaryKey;
// AttributeColumns attributesRead = response.Attribute; // Console.WriteLine("Primary key read: ");
// foreach (KeyValuePair<string, ColumnValue> entry in primaryKeyRead)
// {
// Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
// } // Console.WriteLine("Attributes read: ");
// foreach (KeyValuePair<string, ColumnValue> entry in attributesRead)
// {
// Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
// } // Console.WriteLine("Get row succeed.");
//}
//catch (Exception ex)
//{
// // 如果抛出异常,说明执行失败,打印出错误信息
// Console.WriteLine("Update table failed, exception:{0}", ex.Message);
//}
#endregion #region 更新一行数据
//// 定义行的主键,必须与创建表时的TableMeta中定义的一致
//PrimaryKey primaryKey = new PrimaryKey();
//primaryKey.Add("pk0", new ColumnValue(0));
//primaryKey.Add("pk1", new ColumnValue("abc"));
//// 定义要写入改行的属性列
//UpdateOfAttribute attribute = new UpdateOfAttribute();
//attribute.AddAttributeColumnToPut("col0", new ColumnValue(0));
//attribute.AddAttributeColumnToPut("col1", new ColumnValue("b")); // 将原先的值'a'改为'b'
//attribute.AddAttributeColumnToPut("col2", new ColumnValue(true));
//try
//{
// // 构造更新行的请求对象,RowExistenceExpectation.IGNORE表示不管此行是否存在都执行
// var request = new UpdateRowRequest("SampleTable", new Condition(RowExistenceExpectation.IGNORE),
// primaryKey, attribute);
// // 调用UpdateRow接口执行
// otsClient.UpdateRow(request);
// // 如果没有抛出异常,则说明执行成功
// Console.WriteLine("Update row succeeded.");
//}
//catch (Exception ex)
//{
// // 如果抛出异常,说明执行失败,打印异常信息
// Console.WriteLine("Update row failed, exception:{0}", ex.Message);
//}
#endregion #region 删除一行数据
// 要删除的行的PK列分别为0和"abc"
//var primaryKey = new PrimaryKey();
//primaryKey.Add("pk0", new ColumnValue(0));
//primaryKey.Add("pk1", new ColumnValue("abc"));
//try
//{
// // 构造请求,Condition.EXPECT_EXIST表示只有此行存在时才执行
// var condition = new Condition(RowExistenceExpectation.EXPECT_EXIST);
// var deleteRowRequest = new DeleteRowRequest("SampleTable", condition, primaryKey);
// // 调用DeleteRow接口执行删除
// otsClient.DeleteRow(deleteRowRequest);
// // 如果没有抛出异常,则表示成功
// Console.WriteLine("Delete table succeeded.");
//}
//catch (Exception ex)
//{
// // 如果抛出异常,说明删除失败,打印粗错误信息
// Console.WriteLine("Delete table failed, exception:{0}", ex.Message);
//}
#endregion #region 批量删除数据
//BatchWriteRow();
#endregion #region 根据条件获取多条数据
//GetRange();
#endregion Console.ReadKey();
} private static void PrepareTable()
{
// 创建表
OTSClient otsClient = Config.GetClient(); IList<string> tables = otsClient.ListTable(new ListTableRequest()).TableNames;
if (tables.Contains(TableName))
{
return;
} PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema();
primaryKeySchema.Add("pk0", ColumnValueType.Integer);
primaryKeySchema.Add("pk1", ColumnValueType.String);
TableMeta tableMeta = new TableMeta(TableName, primaryKeySchema); CapacityUnit reservedThroughput = new CapacityUnit(1, 1);
CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput);
otsClient.CreateTable(request); } private static void PrepareData()
{
OTSClient otsClient = Config.GetClient(); // 插入100条数据
for (int i = 0; i < 100; i++)
{
PrimaryKey primaryKey = new PrimaryKey();
primaryKey.Add("pk0", new ColumnValue(i));
primaryKey.Add("pk1", new ColumnValue("abc")); // 定义要写入改行的属性列
AttributeColumns attribute = new AttributeColumns();
attribute.Add("col0", new ColumnValue(0));
attribute.Add("col1", new ColumnValue("a"));
attribute.Add("col2", new ColumnValue(i % 3 != 0));
PutRowRequest request = new PutRowRequest(TableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute); otsClient.PutRow(request);
}
} private static void BatchWriteRow()
{
Console.WriteLine("Start batch write row...");
PrepareTable();
PrepareData();
OTSClient otsClient = Config.GetClient(); // 一次批量导入100行数据
var request = new BatchWriteRowRequest();
var rowChanges = new RowChanges();
for (int i = 0; i < 100; i++)
{
PrimaryKey primaryKey = new PrimaryKey();
primaryKey.Add("pk0", new ColumnValue(i));
primaryKey.Add("pk1", new ColumnValue("abc")); // 定义要写入改行的属性列
UpdateOfAttribute attribute = new UpdateOfAttribute();
attribute.AddAttributeColumnToPut("col0", new ColumnValue(0));
attribute.AddAttributeColumnToPut("col1", new ColumnValue("a"));
attribute.AddAttributeColumnToPut("col2", new ColumnValue(true)); rowChanges.AddUpdate(new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);
} request.Add(TableName, rowChanges); var response = otsClient.BatchWriteRow(request);
var tableRows = response.TableRespones;
var rows = tableRows[TableName]; int succeedRows = 0;
int failedRows = 0;
foreach (var row in rows.UpdateResponses)
{
// 注意:batch操作可能部分成功部分失败,需要为每行检查状态
if (row.IsOK)
{
succeedRows++;
}
else
{
Console.WriteLine("Read row failed: " + row.ErrorMessage);
failedRows++;
}
} Console.WriteLine("SucceedRows: " + succeedRows);
Console.WriteLine("FailedRows: " + failedRows);
} public static void GetRange()
{
Console.WriteLine("Start get range..."); PrepareTable();
PrepareData(); OTSClient otsClient = Config.GetClient();
// 读取 (0, INF_MIN)到(100, INF_MAX)这个范围内的所有行
PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey();
inclusiveStartPrimaryKey.Add("pk0", new ColumnValue(0));
inclusiveStartPrimaryKey.Add("pk1", ColumnValue.INF_MIN); PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey();
exclusiveEndPrimaryKey.Add("pk0", new ColumnValue(100));
exclusiveEndPrimaryKey.Add("pk1", ColumnValue.INF_MAX);
GetRangeRequest request = new GetRangeRequest(TableName, GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey); GetRangeResponse response = otsClient.GetRange(request);
IList<RowDataFromGetRange> rows = response.RowDataList;
PrimaryKey nextStartPrimaryKey = response.NextPrimaryKey;
while (nextStartPrimaryKey != null)
{
request = new GetRangeRequest(TableName, GetRangeDirection.Forward, nextStartPrimaryKey, exclusiveEndPrimaryKey);
response = otsClient.GetRange(request);
nextStartPrimaryKey = response.NextPrimaryKey;
foreach (RowDataFromGetRange row in response.RowDataList)
{
rows.Add(row);
}
} foreach (RowDataFromGetRange row in rows)
{
Console.WriteLine("-----------------");
foreach (KeyValuePair<string, ColumnValue> entry in row.PrimaryKey)
{
Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
}
foreach (KeyValuePair<string, ColumnValue> entry in row.Attribute)
{
Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
}
Console.WriteLine("-----------------");
} Console.WriteLine("TotalRowsRead: " + rows.Count);
} private static string PrintColumnValue(ColumnValue value)
{
switch (value.Type)
{
case ColumnValueType.String: return value.StringValue;
case ColumnValueType.Integer: return value.IntegerValue.ToString();
case ColumnValueType.Boolean: return value.BooleanValue.ToString();
case ColumnValueType.Double: return value.DoubleValue.ToString();
case ColumnValueType.Binary: return value.BinaryValue.ToString();
}
throw new Exception("Unknow type.");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aliyun.OTS; namespace Aliyun.OTS.Samples
{
internal class Config
{
public static string AccessKeyId = "";
public static string AccessKeySecret = "";
public static string Endpoint = "";
public static string InstanceName = "";
private static OTSClient OtsClient = null; public static OTSClient GetClient()
{
if (OtsClient != null)
{
return OtsClient;
} OTSClientConfig config = new OTSClientConfig(Endpoint, AccessKeyId, AccessKeySecret, InstanceName);
config.OTSDebugLogHandler = null;
config.OTSErrorLogHandler = null;
OtsClient = new OTSClient(config);
return OtsClient;
}
}
}

阿里云AliYun表格存储(Table Store)相关案例的更多相关文章

  1. iOS使用阿里云OSS对象存储 (SDK 2.1.1)

    最近项目中用到了阿里云OSS对象存储,用来存储APP中图片.音频等一些数据.但坑爹的阿里云居然在11月20日将SDK版本更新到了2.1.1,然而网上给出的教程都是1.*版本的(针对iOS),两个版本所 ...

  2. .Net程序测试阿里云OSS开放存储服务

    阿里云官网有提供OSS相关的操作API文档和.Net程序的 SDK,也可以在这里下载OSS相关文件 但是API文档里面的都是通过http请求和响应的消息来描述如何操作OSS的 而一般在程序中需要的是O ...

  3. 阿里云的NoSQL存储服务OTS的应用分析

    这篇文章主要介绍了阿里云的NoSQL存储服务OTS的应用分析,OTS作为阿里巴巴开发的NoSQL存储技术服务现已面向用户商业化,需要的朋友可以参考下. 1. 概要  OTS是构建在阿里云飞天分布式系统 ...

  4. 阿里云Oss对象存储

    将文件保存到阿里云上. 1.添加对象存储OSS空间 (新建Bucket) 可以在阿里云后台对象存储里面添加,也可以通过api添加.添加之后设置权限. skd使用. 1安装 Aliyun.OSS.SDK ...

  5. C# .net Ueditor实现图片上传到阿里云OSS 对象存储

    在学习的时候,项目中需要实现在Ueditor编辑器中将图片上传到云储存中,老师演示的是上传到又拍云存储,既然看了一遍,直接照搬不算本事,咱们可以依葫芦画瓢自己来动手玩玩其它的云存储服务. 现在云计算产 ...

  6. Java使用阿里云OSS对象存储上传图片

    原 Java使用阿里云OSS对象存储上传图片 2017年03月27日 10:47:28 陌上桑花开花 阅读数 26804更多 分类专栏: 工作案例总结 版权声明:本文为博主原创文章,遵循CC 4.0 ...

  7. SpringBoot整合阿里云OSS对象存储实现文件上传

    1. 准备工作: 一.首先登录阿里云OSS对象存储控制台创建一个Bucket作为你的存储空间. 二.创建Access Keyan按要求创建进行,这里的方法步骤我就不展现出来了,你们可以自行查询阿里云文 ...

  8. 阿里云OSS的 存储包、下行流量包、回流流量包 三者有啥关系

    阿里云OSS的 存储包.下行流量包.回流流量包 三者有啥关系 一.总结 一句话总结: 你把文件放 oss,会占用存储空间,存储包覆盖这部分费用 你访问存储在 oss 里面的文件,会产生下行流量,就是从 ...

  9. PHP 上传文件至阿里云OSS对象存储

    简述 1.阿里云开通对象存储服务 OSS 并创建Bucket 2.下载PHP SDK至框架扩展目录,点我下载 3.码上code 阿里云操作 开通对象存储服务 OSS 创建 Bucket 配置Acces ...

随机推荐

  1. 【小白成长撸】--多项式求圆周率PI

    /*程序的版权和版本声明部分: *Copyright(c) 2016,电子科技大学本科生 *All rights reserved. *文件名:多项式求PI *程序作用:计算圆周率PI *作者:Amo ...

  2. 关于"设计模式“

    夜深了,人静了,该写点儿东西了.这是第一篇博客,写点儿对设计模式的粗浅理解吧.   什么是设计模式? 上学那会儿初次听到这个名字一点儿概念都没有,不知道它是用来干嘛的,感觉听上去挺抽象的一个东西. 工 ...

  3. jsp---jstl配置

    关于eclipse中jstl标准标签库的配置问题 我的eclipse的版本是:Version: Neon.3 Release (4.6.3) 用的1.8.0_121的jre,Tomcat用的9.0, ...

  4. React——组件

    一.创建组件 在React中有两种创建组件的方式,分别是函数形式的组件和类形式的组件 //函数形式: function Welcome(props){ return <p>this is ...

  5. Spring mvc 转发、重定向

    spring控制器最后返回一个ModelAndView(urlName),其中urNamel可以是一个视图名称,由视图解析器负责解析后将响应流写回客户端;也可以通过redirect/forward:u ...

  6. [转载]Python实现浏览器自动化操作

    原文地址:Python实现浏览器自动化操作作者:rayment   最近在研究网站自动登录的问题,涉及到需要实现浏览器自动化操作,网上有不少介绍,例如使用pamie,但是只是支持IE,而且项目也较久没 ...

  7. ServletRequest的一些知识点

    浏览器向服务器的请求(浏览器将数据发送给服务器时,数据存放的地方) 请求方式:GET和POST * GET:发送的数据,追加在请求的URL之上 * POST:发送的数据在HTTP请求体中 浏览器发送数 ...

  8. 如何将RDS的数据同步到本地自建数据库

    长期以来有很多的用户咨询如何将RDS的数据同步到本地的数据库环境中,本篇文章以在阿里云的ECS服务器为例来说明如何将RDS的数据同步到本地数据库中.RDS对外提供服务是一个DNS地址+端口3306,这 ...

  9. 201521123076《Java程序设计》第2周学习总结

    1.本周学习总结 ①学会简单地管理原始码与位码文档(包的概念) ②了解了一些新的名词,如:完全限定(吻合)名 ③String类相关 ④in.nextInt(),in.next(),in.nextLin ...

  10. 201521123096《Java程序设计》第十周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-2 1.1 截图你的提交结果(出 ...