/// <inheritdoc/> public void Run(ModuleContext context) { var hive = HiveHelper.Hive; var nodeGroups = hive.Definition.GetHostGroups(excludeAllGroup: true); //----------------------------------------------------------------- // Parse the module arguments. if (!context.ValidateArguments(context.Arguments, validModuleArgs)) { context.Failed = true; return; } var couchbaseArgs = CouchbaseArgs.Parse(context); if (couchbaseArgs == null) { return; } var query = context.ParseString("query", q => !string.IsNullOrWhiteSpace(q)); if (context.HasErrors) { return; } var format = context.ParseEnum <CouchbaseFileFormat>("format"); if (!format.HasValue) { format = CouchbaseFileFormat.JsonLines; } var limit = context.ParseLong("limit", v => v >= 0); if (!limit.HasValue || limit.Value == 0) { limit = long.MaxValue; } var output = context.ParseString("output"); //----------------------------------------------------------------- // Execute the query. using (var bucket = couchbaseArgs.Settings.OpenBucket(couchbaseArgs.Credentials)) { try { var results = bucket.QuerySafeAsync <JObject>(query).Result; var count = Math.Min(results.Count, limit.Value); using (var writer = new CouchbaseQueryResultWriter(context, format.Value, output)) { for (int i = 0; i < count; i++) { var document = results[i]; var isLast = i == count - 1; writer.WriteDocument(document, isLast); } } } catch (AggregateException e) { var queryException = e.Find <CouchbaseQueryResponseException>(); if (queryException == null) { throw; } foreach (var error in queryException.Errors) { context.WriteErrorLine($"Couchbase [{error.Code}]: {error.Message}"); } } } }
/// <inheritdoc/> public void Run(ModuleContext context) { var hive = HiveHelper.Hive; var nodeGroups = hive.Definition.GetHostGroups(excludeAllGroup: true); //----------------------------------------------------------------- // Parse the module arguments. if (!context.ValidateArguments(context.Arguments, validModuleArgs)) { context.Failed = true; return; } var couchbaseArgs = CouchbaseArgs.Parse(context); if (couchbaseArgs == null) { return; } var format = context.ParseEnum <CouchbaseFileFormat>("format"); if (!format.HasValue) { format = default(CouchbaseFileFormat); } var source = context.ParseString("source"); if (string.IsNullOrEmpty(source)) { context.WriteErrorLine("[source] module parameter is required."); return; } if (!File.Exists(source)) { context.WriteErrorLine($"File [{source}] does not exist."); return; } var keyPattern = context.ParseString("key"); var firstKey = context.ParseLong("first_key") ?? 1; if (context.HasErrors) { return; } //----------------------------------------------------------------- // Import the data. using (var bucket = couchbaseArgs.Settings.OpenBucket(couchbaseArgs.Credentials)) { var importer = new CouchbaseImporter(message => context.WriteErrorLine(message), bucket, keyPattern, firstKey, context.CheckMode); switch (format.Value) { case CouchbaseFileFormat.JsonArray: // $todo(jeff.lill): // // Would be nice not to read this whole thing in memory and then // effectibely duplicating it in memory again when parsing. var jToken = JToken.Parse(File.ReadAllText(source)); if (jToken.Type != JTokenType.Array) { context.WriteErrorLine($"[{source}] is not a JSON array of documents."); return; } var jArray = (JArray)jToken; foreach (var item in jArray) { if (item.Type != JTokenType.Object) { context.WriteErrorLine($"[{source}] includes one or more non-document objects in the array."); return; } importer.WriteDocument((JObject)item); } break; case CouchbaseFileFormat.JsonLines: using (var reader = new StreamReader(source, Encoding.UTF8)) { foreach (var line in reader.Lines()) { if (line.Trim() == string.Empty) { continue; // Ignore blank lines } var item = JToken.Parse(line); if (item.Type != JTokenType.Object) { context.WriteErrorLine($"[{source}] includes one or more lines with non-document objects."); return; } importer.WriteDocument((JObject)item); } } break; default: throw new NotImplementedException($"Format [{format}] is not implemented."); } context.Changed = importer.DocumentCount > 0; if (context.CheckMode) { context.WriteLine(AnsibleVerbosity.Info, $"[{importer.DocumentCount}] documents will be added when CHECK-MODE is disabled."); } else { context.WriteLine(AnsibleVerbosity.Info, $"[{importer.DocumentCount}] documents were imported."); } } }