Shows how to create column-oriented data and access individual columns.
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <wiredtiger.h>
const char *home = "WT_TEST";
typedef struct {
char country[5];
uint16_t year;
uint64_t population;
} POP_RECORD;
POP_RECORD pop_data[] = {
{ "USA", 1980, 226542250 },
{ "USA", 2009, 307006550 },
{ "UK", 2008, 61414062 },
{ "CAN", 2008, 33311400 },
{ "AU", 2008, 21431800 }
};
int main(void)
{
int ret;
POP_RECORD *p, *endp;
const char *country;
uint64_t recno;
uint16_t year;
if (ret != 0)
fprintf(stderr, "Error connecting to %s: %s\n",
ret = session->
create(session,
"table:population",
"key_format=r,"
"value_format=5sHQ,"
"columns=(id,country,year,population),"
"colgroups=(main,population)");
ret = session->
create(session,
"colgroup:population:main",
"columns=(country,year)");
ret = session->
create(session,
"colgroup:population:population",
"columns=(population)");
ret = session->
create(session,
"index:population:country_year",
"columns=(country,year)");
ret = session->
open_cursor(session,
"table:population",
NULL, "append", &cursor);
endp = pop_data + (sizeof (pop_data) / sizeof(pop_data[0]));
for (p = pop_data; p < endp; p++) {
cursor->
set_value(cursor, p->country, p->year, p->population);
}
ret = cursor->
close(cursor);
"index:population:country_year(id)",
NULL, NULL, &cursor);
while ((ret = cursor->
next(cursor)) == 0) {
cursor->
get_key(cursor, &country, &year);
printf("Got country %s : row ID %d\n", country, (int)recno);
}
ret = conn->
close(conn, NULL);
return (ret);
}