// Get the CustomTargetingService.
CustomTargetingServiceInterface customTargetingService =
user.getService(DfpService.V201208.CUSTOM_TARGETING_SERVICE);
// Create predefined key.
CustomTargetingKey genderKey = new CustomTargetingKey();
genderKey.setDisplayName("gender");
genderKey.setName("g");
genderKey.setType(CustomTargetingKeyType.PREDEFINED);
// Create predefined key that may be used for content targeting.
CustomTargetingKey genreKey = new CustomTargetingKey();
genreKey.setDisplayName("genre");
genreKey.setName("genre");
genreKey.setType(CustomTargetingKeyType.PREDEFINED);
// Create free-form key.
CustomTargetingKey carModelKey = new CustomTargetingKey();
carModelKey.setDisplayName("car model");
carModelKey.setName("c");
carModelKey.setType(CustomTargetingKeyType.FREEFORM);
// Create the custom targeting keys on the server.
CustomTargetingKey[] keys = customTargetingService.createCustomTargetingKeys(
new CustomTargetingKey[] {genderKey, genreKey, carModelKey});