文辉居士

通过Java SDK进行操作

通过Java SDK进行操作

 

下载Java SDK

 

请访问http://oss.aliyuncs.com/bzzx/aliyun_java_sdk_20121101.zip下载。

解压Java SDK,aliyun-openservices.jar是SDK的包,lib目录下包含的是所有依赖的jar包,doc目录下包含的是Java SDK的文档。

 

加入Java SDK引用

 

在NetBeans或Eclipse等IDE中导入Java SDK包aliyun-openservices.jar和位于SDK lib目录下的相关依赖包: commons-codec-1.4.jar, commons-logging-1.1.1.jar, httpclient-4.1.2.jar, httpcore-4.1.2.jar, jdom.jar.

 

 

 

 

创建表

 

下面这段Java代码演示了如何通过create table API来创建一个名为UserTable而且有一个主键列为UserID的表,并通过调用list table API验证了这个Table创建成功。

package otsdemo;

 

import com.aliyun.openservices.ClientException;

import com.aliyun.openservices.ots.OTSClient;

import com.aliyun.openservices.ots.OTSException;

import com.aliyun.openservices.ots.model.PrimaryKeyType;

import com.aliyun.openservices.ots.model.TableMeta;

 

 

public class OTSDemo {

 

    public static void main(String[] args) throws OTSException, ClientException {

 

        final String endpoint = "http://service.ots.aliyun.com";         // OTS服务地址

        final String accessId = "XXXXXXXXXXXXXXXXXXXXXXXX";                  // AccessID

        final String accessKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXX=";        // AccessKey

       

        OTSClient otsClient = new OTSClient(endpoint, accessId, accessKey);

       

        TableMeta tableMeta = new TableMeta("UserTable");

        tableMeta.addPrimaryKey("UserID", PrimaryKeyType.STRING);

        tableMeta.setPagingKeyLen(0);                                                 // 构造表结构

       

        otsClient.createTable(tableMeta);                                                       // 调用OTS进行创建表操作

    }

}

 

 

插入数据

 

下面这段Java代码演示了如果通过put data API来向名为UserTable的表中插入数据,在如下的示例代码中,我们插入了两列数据,主键列分别为U0001和U0002。

package otsdemo;

 

import com.aliyun.openservices.ClientException;

import com.aliyun.openservices.ots.OTSClient;

import com.aliyun.openservices.ots.OTSException;

import com.aliyun.openservices.ots.model.PrimaryKeyValue;

import com.aliyun.openservices.ots.model.RowPutChange;

 

 

public class OTSDemo {

 

    public static void main(String[] args) throws OTSException, ClientException {

 

        final String endpoint = "http://service.ots.aliyun.com";                                     // OTS服务地址

        final String accessId = "XXXXXXXXXXXXXXXXXXXXXXXX";                                              // AccessID

        final String accessKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXX=";                                    // AccessKey

       

        OTSClient otsClient = new OTSClient(endpoint, accessId, accessKey);

       

        RowPutChange rowChange = new RowPutChange();

        rowChange.addPrimaryKey("UserID", PrimaryKeyValue.fromString("U0001"));    // 构造插入行的PK

        otsClient.putData("UserTable", rowChange, "");                                                       // 调用OTS插入一行

       

        rowChange = new RowPutChange();

        rowChange.addPrimaryKey("UserID", PrimaryKeyValue.fromString("U0002"));    // 构造插入行的PK

        otsClient.putData("UserTable", rowChange, "");                                                       // 调用OTS插入一行

 

    }

}

 

查询数据

 

1.通过主键列查询数据

package otsdemo;

 

import com.aliyun.openservices.ClientException;

import com.aliyun.openservices.ots.OTSClient;

import com.aliyun.openservices.ots.OTSException;

import com.aliyun.openservices.ots.model.PrimaryKeyValue;

import com.aliyun.openservices.ots.model.SingleRowQueryCriteria;

import com.aliyun.openservices.ots.model.Row;

 

 

public class OTSDemo {

 

    public static void main(String[] args) throws OTSException, ClientException {

 

        final String endpoint = "http://service.ots.aliyun.com";                            // OTS服务地址

        final String accessId = "XXXXXXXXXXXXXXXXXXXXXXXX";                                     // AccessID

        final String accessKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXX=";                           // AccessKey

       

        OTSClient otsClient = new OTSClient(endpoint, accessId, accessKey);

       

        SingleRowQueryCriteria rowQuery = new SingleRowQueryCriteria("UserTable");

        rowQuery.addPrimaryKey("UserID", PrimaryKeyValue.fromString("U0001"));      // 构造查询单行的PK

 

        Row row = otsClient.getRow(rowQuery, "");                                                  // 调用OTS进行单行查询操作

       

        System.out.println(row.getColumns());

    }

}

 

这样,我们就能读取主键列为U0001的行。

2.通过范围查询数据

package otsdemo;

 

import com.aliyun.openservices.ClientException;

import com.aliyun.openservices.ots.OTSClient;

import com.aliyun.openservices.ots.OTSException;

import com.aliyun.openservices.ots.model.PrimaryKeyValue;

import com.aliyun.openservices.ots.model.RangeRowQueryCriteria;

import com.aliyun.openservices.ots.model.Row;

import java.util.List;

 

public class OTSDemo {

 

    public static void main(String[] args) throws OTSException, ClientException {

 

        final String endpoint = "http://service.ots.aliyun.com";                            // OTS服务地址

        final String accessId = "XXXXXXXXXXXXXXXXXXXXXXXX";                                     // AccessID

        final String accessKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXX=";                           // AccessKey

       

        OTSClient otsClient = new OTSClient(endpoint, accessId, accessKey);

       

        RangeRowQueryCriteria rowQuery = new RangeRowQueryCriteria("UserTable");

 

        rowQuery.setRange("UserID", PrimaryKeyValue.fromString("A"),

                PrimaryKeyValue.fromString("Z"));                                                     // 构造查询的范围

       

        List rows = otsClient.getRowsByRange(rowQuery, "");                          // 调用OTS进行范围查询

 

        for (Row row : rows)

            System.out.println(row.getColumns());

    }

}

 

这样,我们就能读取主键列为U0001和U0002的行。

 

修改数据

 

1. 插入数据。向已经存在的行中插入值。插入两列值,列名分别为Gender和Name。

package otsdemo;

 

import com.aliyun.openservices.ClientException;

import com.aliyun.openservices.ots.OTSClient;

import com.aliyun.openservices.ots.OTSException;

import com.aliyun.openservices.ots.model.PrimaryKeyValue;

import com.aliyun.openservices.ots.model.ColumnValue;

import com.aliyun.openservices.ots.model.RowPutChange;

import com.aliyun.openservices.ots.model.SingleRowQueryCriteria;

import com.aliyun.openservices.ots.model.Row;

 

 

public class OTSDemo {

 

    public static void main(String[] args) throws OTSException, ClientException {

 

        final String endpoint = "http://service.ots.aliyun.com";                            // OTS服务地址

        final String accessId = "XXXXXXXXXXXXXXXXXXXXXXXX";                                     // AccessID

        final String accessKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXX=";                           // AccessKey

       

        OTSClient otsClient = new OTSClient(endpoint, accessId, accessKey);

       

        RowPutChange rowChange = new RowPutChange();

        rowChange.addPrimaryKey("UserID", PrimaryKeyValue.fromString("U0001"));

        rowChange.addAttributeColumn("Name", ColumnValue.fromString("Bob"));

        rowChange.addAttributeColumn("Gender", ColumnValue.fromString("Male"));// 构造更新数据的PK和列

       

        otsClient.putData("UserTable", rowChange, "");                                              // 调用OTS进行修改数据操作

       

        SingleRowQueryCriteria rowQuery = new SingleRowQueryCriteria("UserTable");

        rowQuery.addPrimaryKey("UserID", PrimaryKeyValue.fromString("U0001"));      // 构造单行查询PK

 

      

        Row row = otsClient.getRow(rowQuery, "");                                                  // 调用OTS进行单行查询

 

        System.out.println(row.getColumns());

    }

}

读取一行数据之后,多了两列值,Bob和Male。

2. 删除数据。删除PK为U0001,列名为Gender的值。

package otsdemo;

 

import com.aliyun.openservices.ClientException;

import com.aliyun.openservices.ots.OTSClient;

import com.aliyun.openservices.ots.OTSException;

import com.aliyun.openservices.ots.model.PrimaryKeyValue;

import com.aliyun.openservices.ots.model.RowDeleteChange;

import com.aliyun.openservices.ots.model.SingleRowQueryCriteria;

import com.aliyun.openservices.ots.model.Row;

 

 

public class OTSDemo {

 

    public static void main(String[] args) throws OTSException, ClientException {

 

        final String endpoint = "http://service.ots.aliyun.com";                            // OTS服务地址

        final String accessId = "XXXXXXXXXXXXXXXXXXXXXXXX";                                     // AccessID

        final String accessKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXX=";                           // AccessKey

       

        OTSClient otsClient = new OTSClient(endpoint, accessId, accessKey);

       

        RowDeleteChange rowChange = new RowDeleteChange();

        rowChange.addPrimaryKey("UserID", PrimaryKeyValue.fromString("U0001"));

        rowChange.deleteColumn("Gender");                                                                     //构造删除数据的PK和列

       

        otsClient.deleteData("UserTable", rowChange, "");                                // 调用OTS进行删除数据操作

       

        SingleRowQueryCriteria rowQuery = new SingleRowQueryCriteria("UserTable");

        rowQuery.addPrimaryKey("UserID", PrimaryKeyValue.fromString("U0001"));      // 构造单行查询PK

 

      

        Row row = otsClient.getRow(rowQuery, "");                                                  // 调用OTS进行单行查询

 

        System.out.println(row.getColumns());

    }

}

 

可以从读取一行的结果中看到,列名为Gender的列已经被删除了。

 

删除表

 

删除我们创建的表UserTable。

package otsdemo;

 

import com.aliyun.openservices.ClientException;

import com.aliyun.openservices.ots.OTSClient;

import com.aliyun.openservices.ots.OTSException;

 

 

public class OTSDemo {

 

    public static void main(String[] args) throws OTSException, ClientException {

 

        final String endpoint = "http://service.ots.aliyun.com";                            // OTS服务地址

        final String accessId = "XXXXXXXXXXXXXXXXXXXXXXXX";                                     // AccessID

        final String accessKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXX=";                           // AccessKey

       

        OTSClient otsClient = new OTSClient(endpoint, accessId, accessKey);

       

        otsClient.deleteTable("UserTable");                                                               // 调用OTS删除表

    }

}

 

 

 

posted on 2013-04-08 17:06  restService  阅读(1072)  评论(0编辑  收藏  举报

导航


我是有底线的赠送场