Create STRUCT object array with data
Stay organized with collections
Save and categorize content based on your preferences.
Create an array of STRUCT objects populated with data.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
C++
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
// Cloud Spanner STRUCT<> types with named fields are represented by
// std::tuple<std::pair<std::string, T>...>, create an alias to make it easier
// to follow this code.
usingSingerName=std::tuple<std::pair<std::string,std::string>,
std::pair<std::string,std::string>>;
automake_name=[](std::stringfirst_name,std::stringlast_name){
returnstd::make_tuple(std::make_pair("FirstName",std::move(first_name)),
std::make_pair("LastName",std::move(last_name)));
};
std::vector<SingerName>singer_info{
make_name("Elena","Campbell"),
make_name("Gabriel","Wright"),
make_name("Benjamin","Martinez"),
};
C#
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
varbandMembers=newList<SpannerStruct>
{
newSpannerStruct{{"FirstName",SpannerDbType.String,"Elena"},{"LastName",SpannerDbType.String,"Campbell"}},
newSpannerStruct{{"FirstName",SpannerDbType.String,"Gabriel"},{"LastName",SpannerDbType.String,"Wright"}},
newSpannerStruct{{"FirstName",SpannerDbType.String,"Benjamin"},{"LastName",SpannerDbType.String,"Martinez"}},
};
Go
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
varbandMembers=[]nameType{
{"Elena","Campbell"},
{"Gabriel","Wright"},
{"Benjamin","Martinez"},
}
Java
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
List<Struct>bandMembers=newArrayList<>();
bandMembers.add(
Struct.newBuilder().set("FirstName").to("Elena").set("LastName").to("Campbell").build());
bandMembers.add(
Struct.newBuilder().set("FirstName").to("Gabriel").set("LastName").to("Wright").build());
bandMembers.add(
Struct.newBuilder().set("FirstName").to("Benjamin").set("LastName").to("Martinez").build());
Node.js
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
constbandMembersType={
type:'array',
child:nameType,
};
constbandMembers=[
Spanner.struct({
FirstName:'Elena',
LastName:'Campbell',
}),
Spanner.struct({
FirstName:'Gabriel',
LastName:'Wright',
}),
Spanner.struct({
FirstName:'Benjamin',
LastName:'Martinez',
}),
];
PHP
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
$bandMembers = [
(new StructValue)
->add('FirstName', 'Elena')
->add('LastName', 'Campbell'),
(new StructValue)
->add('FirstName', 'Gabriel')
->add('LastName', 'Wright'),
(new StructValue)
->add('FirstName', 'Benjamin')
->add('LastName', 'Martinez')
];
Python
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
band_members = [
("Elena", "Campbell"),
("Gabriel", "Wright"),
("Benjamin", "Martinez"),
]
Ruby
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
band_members=[name_type.struct(["Elena","Campbell"]),
name_type.struct(["Gabriel","Wright"]),
name_type.struct(["Benjamin","Martinez"])]
What's next
To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.