複数の表の行の読取り

一度に表から複数の行を読み取るには、store.GetAll()またはstore.Search()を使用します。これらの関数では、参照キーとして機能するstore.CreateRow()を使用して空の行を作成する必要があります。提供するキーには、使用する関数に応じて異なる制限が適用されます。

ここに示す例では、提供するキーに少なくともすべての表のshardキーが含まれていることを必要とするStore.GetAll()を使用します。存在しないshardキーがある場合、関数は例外を返します。store.GetAll()により、結果セット内の各部分で使用可能な行を取得するために反復するListが移入されます。

たとえば、次のような表を設計するとします。

CREATE TABLE university_data (
		university STRING, 
		course STRING,
		studentID STRING,
		studentName STRING,
		studentAddress STRING,
		studentEmail STRING,
		PRIMARY KEY (SHARD(university, course), studentID)
)

また、これに次のようなデータを移入します。

using oracle.kv.client;
using oracle.kv.client.config;
...
static void Main(string[] args) {
	... //connecting to the store and creating the table is skipped for brevity
	putRow(store, "XYZ University", "Science", "14SC123", "John Doe", "US", "john.doe@example.com");
	putRow(store, "XYZ University", "Science", "14SC124", "Mike Ruben", "China", "mike.ruben@example.com");
	putRow(store, "ABC University", "Arts", "14AR101", "Ram Paul", "India", "ram.paul@example.com");
	putRow(store, "ABC University", "Arts", "14AR102", "Edward Snow", "UK", "edward.snow@example.com");
	getMultiRows(store, "XYZ University", "Science")
	getAllRows(store, "university_data");
}

public static void putRow(IKVStore store, String university, String course, String studentID, String studentName, String studentAddress, String studentEmail)
{
	String tableName = "university_data";
	var row = store.CreateRow(tableName);
	IRow insertedRow = null;
	row["university"] = university;
	row["course"] = course;
	row["studentID"] = studentID;
	row["studentName"] = studentName;
	row["studentAddress"] = studentAddress;
	row["studentEmail"] = studentEmail;            
	insertedRow = store.Put(row);
	Console.WriteLine(insertedRow); //prints the inserted row in JSON format
}

これで、shardキーのみを指定することで、XYZ Universityのすべての学生調査Scienceのデータを取得できます。

public static void getMultiRows(IKVStore store, String university, String course)
{
	String tableName = "university_data";
	List<IRow> fetchedRow = null;
	var row = store.CreateRow(tableName);
	row["university"] = university;
	row["course"] = course;
	fetchedRow = store.GetAll(row, null);
	fetchedRow.ForEach(Console.WriteLine); //prints the fetched row in JSON format
}

store.GetAllKeys()は、フェッチされた行のキーのみ(この例では、university、courseおよびstudentIDのフィールド)を表示するために使用できるstore.GetAll()のバージョンです。

表のすべての行を表示する場合は、keyパラメータに空の行を指定したStore.Search()を使用します。

public static void getAllRows(IKVStore store, String tableName)
{           
	var row = store.CreateRow(tableName);
	var fetchedRows = store.Search(row, null);
	foreach (IRow fetchedRow in fetchedRows)
		Console.WriteLine(fetchedRow);
}

store.SearchAsync()は、行の非同期検索に使用できるstore.Search()のバージョンです。