本文主要为您介绍如何通过表格存储的Node.js SDK创建全局二级索引、读取全局二级索引表中的数据、删除全局二级索引。

创建全局二级索引

  • 创建主表同时创建全局二级索引
    示例如下:
    var client = require('./client');
    var TableStore = require('../index.js');
    
    var params = {
      tableMeta: {
        tableName: 'sdkGlobalTest',
        primaryKey: [
          {
            name: 'pk1',
            type: TableStore.PrimaryKeyType.INTEGER
          },
          {
            name: 'pk2',
            type: TableStore.PrimaryKeyType.INTEGER
          }
        ],
        definedColumn: [
          {
            "name": "col1",
            "type": TableStore.DefinedColumnType.DCT_INTEGER
          },
          {
            "name": "col2",
            "type": TableStore.DefinedColumnType.DCT_INTEGER
          }
        ],
      },
      reservedThroughput: {
        capacityUnit: {
          read: 0,
          write: 0
        }
      },
      tableOptions: {
        timeToLive: -1,// 数据的过期时间,单位秒, -1代表永不过期。 假如设置过期时间为一年,即为 365 * 24 * 3600
        maxVersions: 1// 保存的最大版本数,设置为1即代表每列上最多保存一个版本(保存最新的版本)
      },
      streamSpecification: {
        enableStream: false, //globalIndex不支持开启Stream
      },
      indexMetas: [
        {
          name: "sdkIndex1",
          primaryKey: ["pk2"],
          definedColumn: ["col1", "col2"]
        },
        {
          name: "sdkIndex2",
          primaryKey: ["col1"],
          definedColumn: ["col2"]
        }
      ]
    };
    
    client.createTable(params, function (err, data) {
      if (err) {
        console.log('error:', err);
        return;
      }
      console.log('success:', data);
    });
  • 单独创建全局二级索引
    您可以在一张已经存在的主表上创建全局二级索引表,示例如下:
    var client = require('./client');
    var TableStore = require('../index.js');
    
    client.createIndex({
        mainTableName: "sdkGlobalTest",
        indexMeta: {
            name: "sdkIndex2",
            primaryKey: ["col1"],
            definedColumn: ["col2"],
            includeBaseData: false,
            indexUpdateMode: TableStore.IndexUpdateMode.IUM_ASYNC_INDEX,//默认增量
            indexType: TableStore.IndexType.IT_GLOBAL_INDEX,//IT_GLOBAL_INDEX
        }
    }, function (err, data) {
        if (err) {
            console.log('error:', err);
            return;
        }
        console.log('success:', JSON.stringify(data, null, 2));
    });

删除全局二级索引

您可以删除主表上的全局二级索引表,示例如下:
var client = require('./client');

client.dropIndex({
  mainTableName: "sdkGlobalTest",
  indexName: "sdkIndex1"
}, function (err, data) {
  if (err) {
    console.log('error:', err);
    return;
  }
  console.log('success:', JSON.stringify(data, null, 2));
});

读取二级索引表中的数据

示例如下:
  • 单行读取全局二级索引表
    var TableStore = require('../index.js');
    var Long = TableStore.Long;
    var client = require('./client');
    
    var params = {
      tableName: "index1",
      primaryKey: [ {'pk2': Long.fromNumber(2)}, {'pk1': Long.fromNumber(1)}]
    };
    
    client.getRow(params, function (err, data) {
      if (err) {
        console.log('error:', err);
        return;
      }
      console.log('success:', JSON.stringify(data, null, 2));
    });
  • 范围读取全局二级索引表
    var TableStore = require('../index.js');
    var Long = TableStore.Long;
    var client = require('./client');
    
    var params = {
      tableName: "sdkIndex1", //假定pk2是索引表第一个主键列,pk1是第二个主键列。
      direction: TableStore.Direction.FORWARD,
      maxVersions: 10,
      inclusiveStartPrimaryKey: [{ "pk2": TableStore.INF_MIN }, { "pk1": TableStore.INF_MIN }],
      exclusiveEndPrimaryKey: [{ "pk2": TableStore.INF_MAX }, { "pk1": TableStore.INF_MAX }],
      limit: 2
    };
    
    var resultRows = []
    
    var getRange = function () {
      client.getRange(params, function (err, data) {
        if (err) {
          console.log('error:', err);
          return;
        }
        resultRows = resultRows.concat(data.rows)
    
        //如果data.next_start_primary_key不为空,说明需要继续读取。
        if (data.nextStartPrimaryKey) {
          params.inclusiveStartPrimaryKey = [
            { "pk2": data.nextStartPrimaryKey[0].value },
            { "pk1": data.nextStartPrimaryKey[1].value }
          ];
          getRange()
        } else {
          console.log(JSON.stringify(resultRows));
        }
      });
    }
    
    getRange()