|
| 1 | +// Run this using dotnet-script: https://github.com/filipw/dotnet-script |
| 2 | +// |
| 3 | +// dotnet script Import.csx |
| 4 | +// |
| 5 | + |
| 6 | +#r "nuget: CouchbaseNetClient, 3.2.5" |
| 7 | + |
| 8 | +#r "nuget: CsvHelper, 27.2.1" |
| 9 | + |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Couchbase; |
| 12 | +using Couchbase.KeyValue; |
| 13 | + |
| 14 | +// tag::csv-tsv-import[] |
| 15 | +using CsvHelper; |
| 16 | +using CsvHelper.Configuration; |
| 17 | +using System.Globalization; |
| 18 | +// end::csv-tsv-import[] |
| 19 | + |
| 20 | +// tag::json-jsonl-import[] |
| 21 | +using Newtonsoft.Json; |
| 22 | +using Newtonsoft.Json.Linq; |
| 23 | +// end::json-jsonl-import[] |
| 24 | + |
| 25 | + |
| 26 | +using System.Runtime.CompilerServices; |
| 27 | + |
| 28 | +// https://github.com/filipw/dotnet-script |
| 29 | +public static string GetScriptFolder([CallerFilePath] string path = null) => Path.GetDirectoryName(path); |
| 30 | +Directory.SetCurrentDirectory(GetScriptFolder()); |
| 31 | + |
| 32 | +// tag::connect[] |
| 33 | +var cluster = await Cluster.ConnectAsync( |
| 34 | + "couchbase://localhost", |
| 35 | + "Administrator", "password"); |
| 36 | +var bucket = await cluster.BucketAsync("travel-sample"); |
| 37 | +var scope = await bucket.ScopeAsync("inventory"); |
| 38 | +var _collection = await scope.CollectionAsync("airline"); |
| 39 | +// end::connect[] |
| 40 | + |
| 41 | +// tag::importCSV[] |
| 42 | +public async Task importCSV(string filename) |
| 43 | +{ |
| 44 | + using (var reader = new StreamReader(filename)) |
| 45 | + using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) |
| 46 | + { |
| 47 | + var records = csv.GetRecords<dynamic>(); |
| 48 | + foreach (dynamic record in records) { |
| 49 | + await upsertDocument(record); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | +// end::importCSV[] |
| 54 | +await importCSV("import.csv"); |
| 55 | + |
| 56 | +// tag::importTSV[] |
| 57 | +public async Task importTSV(string filename) |
| 58 | +{ |
| 59 | + using (var reader = new StreamReader("import.tsv")) |
| 60 | + using (var tsv = new CsvReader(reader, |
| 61 | + new CsvConfiguration(CultureInfo.InvariantCulture) |
| 62 | + { Delimiter = "\t" })) |
| 63 | + { |
| 64 | + var records = tsv.GetRecords<dynamic>(); |
| 65 | + foreach (dynamic record in records) { |
| 66 | + await upsertDocument(record); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +// end::importTSV[] |
| 71 | +await importTSV("import.tsv"); |
| 72 | + |
| 73 | +// tag::importJSON[] |
| 74 | +public async Task importJSON(string filename) |
| 75 | +{ |
| 76 | + using (var reader = new StreamReader("import.json")) |
| 77 | + { |
| 78 | + var jsonReader = new JsonTextReader(reader); |
| 79 | + JArray arr = (JArray)JToken.ReadFrom(jsonReader); |
| 80 | + |
| 81 | + foreach (JObject record in arr) |
| 82 | + { |
| 83 | + await upsertDocument(record); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | +// end::importJSON[] |
| 88 | +await importJSON("import.json"); |
| 89 | + |
| 90 | +// tag::importJSONL[] |
| 91 | +public async Task importJSONL(string filename) |
| 92 | +{ |
| 93 | + using (var reader = new StreamReader("import.jsonl")) |
| 94 | + { |
| 95 | + var jsonlReader = new JsonTextReader(reader) |
| 96 | + { |
| 97 | + SupportMultipleContent = true |
| 98 | + }; |
| 99 | + while (jsonlReader.Read()) |
| 100 | + { |
| 101 | + var record = (JObject)JToken.ReadFrom(jsonlReader); |
| 102 | + await upsertDocument(record); |
| 103 | + |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | +// end::importJSONL[] |
| 108 | +await importJSONL("import.jsonl"); |
| 109 | + |
| 110 | + |
| 111 | +// tag::upsertDocument[] |
| 112 | +// CsvHelper emits `dynamic` records |
| 113 | +public async Task upsertDocument(dynamic record) { |
| 114 | + // define the key |
| 115 | + string key = record.type + "_" + record.id; |
| 116 | + |
| 117 | + // do any additional processing |
| 118 | + record.importer = ".NET SDK"; |
| 119 | + |
| 120 | + // upsert the document |
| 121 | + await _collection.UpsertAsync(key, record); |
| 122 | + |
| 123 | + // any required logging |
| 124 | + Console.WriteLine(key); |
| 125 | +} |
| 126 | + |
| 127 | +// Newtonsoft.Json.Linq emits `JObjects` |
| 128 | +public async Task upsertDocument(JObject record) { |
| 129 | + // define the key |
| 130 | + string key = record["type"] + "_" + record["id"]; |
| 131 | + |
| 132 | + // do any additional processing |
| 133 | + record["importer"] = ".NET SDK"; |
| 134 | + |
| 135 | + // upsert the document |
| 136 | + await _collection.UpsertAsync(key, record); |
| 137 | + |
| 138 | + // any required logging |
| 139 | + Console.WriteLine(key); |
| 140 | +} |
| 141 | +// end::upsertDocument[] |
0 commit comments