JSONフィールドの索引付け

JSONフィールドに対して索引を作成できます。索引を作成するには、索引付けするJSONフィールドのデータ型を定義する必要があることを除き、他の索引と同様に指定します。

索引付けできるJSONフィールドのデータ型にはいくつかの制限があることに注意してください。詳細は、JSON索引を参照してください。

索引を作成するには、まず表を作成します。

CREATE Table JSONPersons (
    id INTEGER,
    person JSON,
    PRIMARY KEY (id)
) 

索引を作成するには、ドット表記法を使用して索引付けするJSONフィールドを指定する必要があります。次のような表の行があるとします。

  "id":1,
  "person" : {
      "firstname":"David",
      "lastname":"Morrison",
      "age":25,
      "income":100000,
      "lastLogin" : "2016-10-29T18:43:59.8319",
      "address":{"street":"150 Route 2",
                 "city":"Antioch",
                 "state":"TN",
                 "zipcode" : 37013,
                 "phones":[{"type":"home", "areacode":423, 
                            "number":8634379}]
                },
      "connections":[2, 3],
      "expenses":{"food":1000, "gas":180}
  } 

表をストアに追加した後、次のようなJSONフィールドの1つに索引を作成できます。

CREATE INDEX idx_json_income on JSONPersons (person.income AS integer)

JSON索引を使用してデータを取得するには、まず名前を使用して索引を取得し、索引参照の実行に使用するIndexKeyのインスタンスを作成します。次を使用して、person.incomeフィールドが100000の表の行をすべて取得します。

Index jsonIndex = myTable.getIndex("idx_json_income");
IndexKey indexKey = jsonIndex.createIndexKey(); 
indexKey.put("person.income", 100000); 

索引参照を実行すると、返される行は、指定したフィールド値を持つJSONフィールドを持つ行のみになります。その後、一致する表の行を取得し、別の索引タイプと同じ方法で繰り返します。次に例を示します。

TableIterator<Row> iter = tableH.tableIterator(indexKey, null, null);
System.out.println("Results for person.income, value 100000: ");
try {
    while (iter.hasNext()) {
        Row rowRet = iter.next();
        int id = rowRet.get("id").asInteger().get();
        System.out.println("id: " + id);
        MapValue mapRet = rowRet.get("person").asMap();
        System.out.println("person: " + mapRet.toString());
    }
} finally {
    if (iter != null) {
        iter.close();
    }
} 

JSON索引などのJSONデータ・フィールドの使用例は、例によるJSONを参照してください。