zoukankan      html  css  js  c++  java
  • 【阿里云产品公测】结构化数据服务OTS之JavaSDK初体验

    【阿里云产品公测】结构化数据服务OTS之JavaSDK初体验

    作者:阿里云用户蓝色之鹰

    一、OTS简单介绍

    OTS 是构建在阿里云飞天分布式系统之上的NoSQL数据库服务,提供海量结构化数据的存储和实时访问。NoSQL,泛指非关系型的数据库。随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的web2.0纯动态网站已经显得力不从心,暴露了很多难以克服的问题,而非关系型的数据库则由于其本身的特点得到了非常迅速的发展。OTS应用程序可以使用阿里云官方发布的OTS SDK 来访问OTS,也可以通过POST 方法发送HTTP 请求来访问OTS。

    关于http请求访问请参考API

    本文将介绍SDK访问OTS。

    二、准备工作

    1、结构化数据服务 OTS 公测资格申请;参见OTS申请流程 

    2、SDK下载;

        Java SDK下载地址:

        http://oss.aliyuncs.com/aliyun_portal_storage/help/ots/aliyun-openservices-OTS-2.0.4.zip?spm=5176.383723.9.10.yXCDUV&file=aliyun-openservices-OTS-2.0.4.zip

        OSS和OTS SDK可实现maven中央库调用(2012年10月10日起实现)

        Open Services SDK for Java包含了OSS和OTS的SDK,之前一直只在阿里云官方网站上以zip包的形式提供下载链接。用户只能手动下载使用。但在Java世界里,maven已经成为开发者依赖的代码库管理工具,将SDK发布到maven中央库可以方便开发者使用我们的SDK。现在SDK已经在Maven中央库发布成功!今后开发者可以直接在其项目中使用maven -> add dependency来引用我们的SDK了。

    3、Access Key创建

    三、安装部署

    本人使用的IDE是MyEclipse,这个随便找自己习惯的IDE

    1. SDK包结构

    需要说下的是,这个SDK包里默认包含了OTS和OSS;安装和使用基本上都差不多,有兴趣的同学,可以都体验下!

           2、引入工程

    代码:将examples下的com目录拷贝到项目Src下

    JAR包:将aliyun-openservices-OTS-2.0.4.jar和lib目录下的jar包引入工程;

          3、编码设置

    将项目编码设置为:UTF-8

    4、目录展示

    四、测试使用

    1. 创建实例

    登录阿里管理控制台,进入OTS管理控制台创建实例

          2、修改参数

    测试类:OTSSingleDataSample

    1. public static void main(String args[]) {

    2.         final String endPoint = "<your endpoint>"; //http开头+公网访问域名,如:http://myotstest.cn-hangzhou.ots.aliyuncs.com

    3.         final String accessId = "<your access id>";

    4.         final String accessKey = "<your access key>";

    5.         final String instanceName = "<your instance name>";//实例名

    6. //以上四个参数替换请把<>也替换掉

    7. 运行java程序

    结果:

    复制代码

    1. 表已创建

    2. 成功插入数据, 消耗的写CapacityUnit为:1

    3. 本次读操作消耗的读CapacityUnti为:1

    4. name信息:张三:STRING

    5. address信息:中国A地:STRING

    6. age信息:20:INTEGER

    7. 成功更新数据, 消耗的写CapacityUnit为:1

    8. 成功删除数据, 消耗的写CapacityUnit为:1

    9. 表已删除

    五、代码分享

           main方法

    1. public static void main(String args[]) {

    2.         final String endPoint = "<your endpoint>";

    3.         final String accessId = "<your access id>";

    4.         final String accessKey = "<your access key>";

    5.         final String instanceName = "<your instance name>";

    6.         

    7.         OTSClient client = new OTSClient(endPoint, accessId, accessKey, instanceName);

    8.         final String tableName = "sampleTable";

    9.         try{

    10.             // 创建表

    11.             createTable(client, tableName);

    12.             // 注意:创建表只是提交请求,OTS创建表需要一段时间。

    13.             // 这里简单地等待30秒,请根据您的实际逻辑修改。

    14.             Thread.sleep(30000);

    15.             // 插入一条数据。

    16.             putRow(client, tableName);

    17.             // 再取回来看看。

    18.             getRow(client, tableName);

    19.             // 改一下这条数据。

    20.             updateRow(client, tableName);

    21.             // 删除这条数据。

    22.             deleteRow(client, tableName);

    23.         }catch(ServiceException e){

    24.             System.err.println("操作失败,详情:" + e.getMessage());

    25.             // 可以根据错误代码做出处理, OTS的ErrorCode定义在OTSErrorCode中。

    26.             if (OTSErrorCode.QUOTA_EXHAUSTED.equals(e.getErrorCode())){

    27.                 System.err.println("超出存储配额。");

    28.             }

    29.             // Request ID可以用于有问题时联系客服诊断异常。

    30.             System.err.println("Request ID:" + e.getRequestId());

    31.         }catch(ClientException e){

    32.             // 可能是网络不好或者是返回结果有问题

    33.             System.err.println("请求失败,详情:" + e.getMessage());

    34.         } catch (InterruptedException e) {

    35.             System.err.println(e.getMessage());

    36.         }

    37.         finally{

    38.             // 不留垃圾。

    39.             try {

    40.                 deleteTable(client, tableName);

    41.             } catch (ServiceException e) {

    42.                 System.err.println("删除表格失败,原因:" + e.getMessage());

    43.                 e.printStackTrace();

    44.             } catch (ClientException e) {

    45.                 System.err.println("删除表格请求失败,原因:" + e.getMessage());

    46.                 e.printStackTrace();

    47.             }

    48.         }

    49.     }

    这里完成了下面几个动作

    a、创建表

    b、简单地等待30秒

    c、插入一条数据

    d、再取回来看看

    e、修改这条数据

    f、删除这条数据

    g、最后删除表

    创建表的方法

    复制代码

    1. private static void createTable(OTSClient client, String tableName)

    2.             throws ServiceException, ClientException{

    3.         TableMeta tableMeta = new TableMeta(tableName);

    4.         tableMeta.addPrimaryKeyColumn(COLUMN_GID_NAME, PrimaryKeyType.INTEGER);

    5.         tableMeta.addPrimaryKeyColumn(COLUMN_UID_NAME, PrimaryKeyType.INTEGER);

    6.         // 将该表的读写CU都设置为100

    7.         CapacityUnit capacityUnit = new CapacityUnit(100, 100);

    8.         CreateTableRequest request = new CreateTableRequest();

    9.         request.setTableMeta(tableMeta);

    10.         request.setReservedThroughput(capacityUnit);

    11.         client.createTable(request);

    12.         System.out.println("表已创建");

    13.     }

    插入行的方法

    复制代码

    1. private static void putRow(OTSClient client, String tableName)

    2.             throws ServiceException, ClientException{

    3.         RowPutChange rowChange = new RowPutChange(tableName);

    4.         RowPrimaryKey primaryKey = new RowPrimaryKey();

    5.         primaryKey.addPrimaryKeyColumn(COLUMN_GID_NAME, PrimaryKeyValue.fromLong(1));

    6.         primaryKey.addPrimaryKeyColumn(COLUMN_UID_NAME, PrimaryKeyValue.fromLong(101));

    7.         rowChange.setPrimaryKey(primaryKey);

    8.         rowChange.addAttributeColumn(COLUMN_NAME_NAME, ColumnValue.fromString("张三"));

    9.         rowChange.addAttributeColumn(COLUMN_MOBILE_NAME, ColumnValue.fromString("111111111"));

    10.         rowChange.addAttributeColumn(COLUMN_ADDRESS_NAME, ColumnValue.fromString("中国A地"));

    11.         rowChange.addAttributeColumn(COLUMN_AGE_NAME, ColumnValue.fromLong(20));

    12.         rowChange.setCondition(new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST));

    13.         

    14.         PutRowRequest request = new PutRowRequest();

    15.         request.setRowChange(rowChange);

    16.         PutRowResult result = client.putRow(request);

    17.         int consumedWriteCU = result.getConsumedCapacity().getCapacityUnit().getWriteCapacityUnit();

    18.         System.out.println("成功插入数据, 消耗的写CapacityUnit为:" + consumedWriteCU);

    19.     }

    获取行的方法

    复制代码

    1. private static void getRow(OTSClient client, String tableName)

    2.             throws ServiceException, ClientException{

    3.         SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(tableName);

    4.         RowPrimaryKey primaryKeys = new RowPrimaryKey();

    5.         primaryKeys.addPrimaryKeyColumn(COLUMN_GID_NAME, PrimaryKeyValue.fromLong(1));

    6.         primaryKeys.addPrimaryKeyColumn(COLUMN_UID_NAME, PrimaryKeyValue.fromLong(101));

    7.         criteria.setPrimaryKey(primaryKeys);

    8.         criteria.addColumnsToGet(new String[] {

    9.                 COLUMN_NAME_NAME,

    10.                 COLUMN_ADDRESS_NAME,

    11.                 COLUMN_AGE_NAME

    12.         });

    13.         GetRowRequest request = new GetRowRequest();

    14.         request.setRowQueryCriteria(criteria);

    15.         GetRowResult result = client.getRow(request);

    16.         Row row = result.getRow();

    17.         

    18.         int consumedReadCU = result.getConsumedCapacity().getCapacityUnit().getReadCapacityUnit();

    19.         System.out.println("本次读操作消耗的读CapacityUnti为:" + consumedReadCU);

    20.         System.out.println("name信息:" + row.getColumns().get(COLUMN_NAME_NAME));

    21.         System.out.println("address信息:" + row.getColumns().get(COLUMN_ADDRESS_NAME));

    22.         System.out.println("age信息:" + row.getColumns().get(COLUMN_AGE_NAME));

    23.     }

    更新行的方法

    复制代码

    1. private static void updateRow(OTSClient client, String tableName)

    2.             throws ServiceException, ClientException{

    3.         RowUpdateChange rowChange = new RowUpdateChange(tableName);

    4.         RowPrimaryKey primaryKeys = new RowPrimaryKey();

    5.         primaryKeys.addPrimaryKeyColumn(COLUMN_GID_NAME, PrimaryKeyValue.fromLong(1));

    6.         primaryKeys.addPrimaryKeyColumn(COLUMN_UID_NAME, PrimaryKeyValue.fromLong(101));

    7.         rowChange.setPrimaryKey(primaryKeys);

    8.         // 更新以下三列的值

    9.         rowChange.addAttributeColumn(COLUMN_NAME_NAME, ColumnValue.fromString("张三"));

    10.         rowChange.addAttributeColumn(COLUMN_ADDRESS_NAME, ColumnValue.fromString("中国B地"));

    11.         // 删除mobile和age信息

    12.         rowChange.deleteAttributeColumn(COLUMN_MOBILE_NAME);

    13.         rowChange.deleteAttributeColumn(COLUMN_AGE_NAME);

    14.         

    15.         rowChange.setCondition(new Condition(RowExistenceExpectation.EXPECT_EXIST));

    16.         

    17.         UpdateRowRequest request = new UpdateRowRequest();

    18.         request.setRowChange(rowChange);

    19.         UpdateRowResult result = client.updateRow(request);

    20.         int consumedWriteCU = result.getConsumedCapacity().getCapacityUnit().getWriteCapacityUnit();

    21.         System.out.println("成功更新数据, 消耗的写CapacityUnit为:" + consumedWriteCU);

    22.     }

    删除行的方法

    复制代码

    1. private static void deleteRow(OTSClient client, String tableName)

    2.             throws ServiceException, ClientException{

    3.         RowDeleteChange rowChange = new RowDeleteChange(tableName);

    4.         RowPrimaryKey primaryKeys = new RowPrimaryKey();

    5.         primaryKeys.addPrimaryKeyColumn(COLUMN_GID_NAME, PrimaryKeyValue.fromLong(1));

    6.         primaryKeys.addPrimaryKeyColumn(COLUMN_UID_NAME, PrimaryKeyValue.fromLong(101));

    7.         rowChange.setPrimaryKey(primaryKeys);

    8.         

    9.         DeleteRowRequest request = new DeleteRowRequest();

    10.         request.setRowChange(rowChange);

    11.         DeleteRowResult result = client.deleteRow(request);

    12.         int consumedWriteCU = result.getConsumedCapacity().getCapacityUnit().getWriteCapacityUnit();

    13.         System.out.println("成功删除数据, 消耗的写CapacityUnit为:" + consumedWriteCU);

    14.     }

    删除表的方法

    复制代码

    1. private static void deleteTable(OTSClient client, String tableName)

    2.             throws ServiceException, ClientException{

    3.         DeleteTableRequest request = new DeleteTableRequest();

    4.         request.setTableName(tableName);

    5.         client.deleteTable(request);

    6.         System.out.println("表已删除");

    7.     }

    五、测试体会

    OTS 使用还是很简单方便的,基本的稳定和安全还是有保证的。

    1、系统可用性99.9%,自动故障检测与恢复;

    2、用户级别的数据隔离、访问控制和权限管理,用户只能访问有权限的表,数据冗余备份;

    3、ots 主要的优势还是在于大数据量方面,单表百TB级别数据存储,毫秒级别单行读写延迟,十万级别QPS。

    原文地址:http://bbs.aliyun.com/read/178870.html 

         微博互动:http://weibo.com/1644971875/BrlU9CYNQ 

         参加活动:http://promotion.aliyun.com/act/aliyun/freebeta/ 



  • 相关阅读:
    HDU4341 Gold miner 分组背包
    卡特兰数
    欧拉函数
    求一个阶乘数尾零的个数
    线性时间 筛素数,求前n个数的欧拉函数值,求前n个数的约数个数
    HDU4335 What is N? 欧拉函数,欧拉定理
    HDU4336 Card Collector 容斥定理 Or 概率DP
    ie8恶心的bug4个小时的教训
    39个超实用jQuery实例应用特效
    ECSHOP 模板结构说明
  • 原文地址:https://www.cnblogs.com/aliyunblogs/p/4022097.html
Copyright © 2011-2022 走看看