static void Main(string[] args) { if (args.Length < 1) { Console.WriteLine("Usage: dmloader <log1> [log2] [log3]"); Console.WriteLine(" dmloader -watch <directory>"); return; } _debug = ConfigurationManager.AppSettings["Debug"] == "true"; _cluster = new Cluster("couchbaseClients/couchbase"); _bucket = _cluster.OpenBucket(); if (args[0].StartsWith("-w")) { if (args.Length < 2 || !Directory.Exists(args[1])) { Console.WriteLine("Please specify a valid directory."); return; } GenerateViews().Wait(); WatchDirectory(args[1]); Console.Read(); } else { ImportFiles(args); } }
private (c.Cluster cluster, CouchbaseConfig couchbaseConfig) LoadCouchbaseConfig(string config) { if (config == null) { throw new System.NullReferenceException("传入的配置信息为空。"); } CouchbaseConfig r = null; try { r = GetCouchbaseConfig(config); } catch (System.Exception) { throw new ConfigurationException("读取配置文件引发此异常,检查配置信息是否正确。"); } if (r.Cluster.UserName == null || r.Cluster.Password == null) { throw new ClusterAuthenticatorException("连接集群时引发此异常,用户名、密码验证失败。"); } var serverUrl = new List <Uri>(); try { foreach (var item in r.Cluster.Servers) { serverUrl.Add(new Uri($"{item.Ip}:{item.Port}")); } } catch (System.Exception) { throw new ClusterAuthenticatorException(@"读取配置文件引发此异常,检查""Servers""节点配置信息。"); } c.Cluster cluster = null; try { cluster = new c.Cluster(new ClientConfiguration { Servers = serverUrl, UseSsl = r.Cluster.UseSSL, SslPort = r.Cluster.SSLPort }); } catch (System.Exception) { throw new ClusterAuthenticatorException(@"读取配置文件引发此异常,检查""Cluster""节点配置信息。"); } var authenticator = new PasswordAuthenticator(r.Cluster.UserName, r.Cluster.Password); cluster.Authenticate(authenticator); return(cluster, r); }
public static void LoadDir(string path="../../TestDocs"){ Console.WriteLine (path); if (Directory.Exists (path)) { var fileList = Directory.GetFiles (path); foreach (var filePath in fileList) { Console.WriteLine (filePath); using (var cluster = new Cluster (config)) { using (var bucket = cluster.OpenBucket ("doc-store")) { LoadFile (filePath, (CouchbaseBucket)bucket); } } } } }
static void Main(string[] args) { using (var cluster = new Cluster()) { using (var bucket = cluster.OpenBucket("tutorial")) { var contacts = from c in bucket.Queryable<Contact>() select c; foreach (var contact in contacts) { Console.WriteLine("\tName={0}, Age={1}, Email={2}", contact.FirstName, contact.Age, contact.Title); } } } Console.Read(); }
public static void TestInsert () { using (var cluster = new Cluster(config)) { using (var bucket = cluster.OpenBucket ("doc-store")) { var document = new Document<dynamic> () { Id = "test1", Content = new { name = "Couchbase" } }; var upsert = bucket.Upsert (document); if (upsert.Success) { var get = bucket.GetDocument<dynamic> (document.Id); document = get.Document; var msg = string.Format ("{0} {1}!", document.Id, document.Content.name); Console.WriteLine (msg); } } } }
public static bool WriteFile(string name="hello2.txt.zip", string path="../../TestOut") { using (var cluster = new Cluster(config)) { using (var bucket = cluster.OpenBucket ("doc-store")) { var fullPath = path + '/' + name; var get = bucket.GetDocument<dynamic> (name); var document = get.Document; var msg = string.Format ("{0}!", document.Id); Console.WriteLine (msg); var bytes = (byte[]) System.Convert.FromBase64String(document.Content); File.WriteAllBytes (fullPath, bytes); if (File.Exists (fullPath)) return true; return false; } } }
//bucket must have flush enabled public static void EmptyBucket(string name="doc-store") { using (var cluster = new Cluster(config)) { using (var bucket = cluster.OpenBucket (name)) { var manager = bucket.CreateManager("Administrator", ""); //var result = manager.Flush(); } } }
public CouchbaseFacade() { _cluster = new Cluster(); _bucket = _cluster.OpenBucket("default"); }
private static async Task GenerateViews() { using (var cluster = new Cluster("couchbaseClients/couchbase")) { using (var bucket = cluster.OpenBucket()) { var manager = bucket.CreateManager(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"]); var entities = await manager.GetDesignDocumentAsync("entities"); var links = await manager.GetDesignDocumentAsync("links"); if (entities == null || string.IsNullOrEmpty(entities.Value) || entities.Exception != null) { using (StreamReader sr = new StreamReader("entities.json")) { var json = await sr.ReadToEndAsync(); var res = await manager.InsertDesignDocumentAsync("entities", json); } } if (links == null || string.IsNullOrEmpty(links.Value) || links.Exception != null) { using (StreamReader sr = new StreamReader("links.json")) { var json = await sr.ReadToEndAsync(); var res = await manager.InsertDesignDocumentAsync("links", json); } } } } }
/// <summary> /// Initializes a new instance of the <see cref="CouchbaseDAO"/> class. /// </summary> public CouchbaseDAO() { m_Cluster = new Cluster(); }
public CouchBaseDBExtension(ExtendedActorSystem system) { if (system == null) throw new ArgumentNullException("system"); // Initialize fallback configuration defaults system.Settings.InjectTopLevelFallback(CouchBaseDBPersistence.DefaultConfiguration()); var HOCON_CB_JournalConfig = system.Settings.Config.GetConfig("akka.persistence.journal.couchbase"); JournalSettings = new CouchBaseJournalSettings(HOCON_CB_JournalConfig); if(JournalSettings.UseClusterHelper) { JournalCBBucket = ClusterHelper.GetBucket(JournalSettings.BucketName); JournalCBCluster = JournalCBBucket.Cluster; } else { // Instantiate the connection to the cluster JournalCBCluster = new Cluster(JournalSettings.CBClientConfiguration); //Open the bucket and make a reference to the CB Client Configuration JournalCBBucket = (CouchbaseBucket)JournalCBCluster.OpenBucket(JournalSettings.BucketName); } // Throw an exception if we reach this point without a CB Cluster, CB Config, or Bucket if (JournalCBCluster == null) throw new Exception("CouchBase Journal Cluster could not initialized."); // Had to do it this way since by default CB always initializes the default bucket. if (JournalCBBucket.Name!=JournalSettings.BucketName) { throw new Exception("CouchBase Journal bucket could not initialized."); } // Add Journal Indexes // Add here to create Global Secondary Indexes that cover (See covering Indexes in Couchbase website) to improve performance // First check if the index exists // SELECT * FROM system:indexes WHERE name = 'idxDocumentType_PersistenceId_SequenceNr' // SELECT * FROM system:indexes WHERE name = 'idxDocumentType_PersistenceId' // Create using these: // CREATE INDEX idxDocumentType_PersistenceId_SequenceNr on `SSA` (PersistenceId,SequenceNr,DocumentType) USING GSI // CREATE INDEX idxDocumentType_PersistenceId on `SSA` (DocumentType,PersistenceId) USING GSI //string N1QLQueryString = "SELECT * FROM system:indexes WHERE name = 'idxDocumentType_PersistenceId_SequenceNr'"; //var result = JournalCBBucket.Query<dynamic>(N1QLQueryString); //if (result.Rows.Count == 0 && result.Success == true) //{ // N1QLQueryString = "CREATE INDEX idxDocumentType_PersistenceId_SequenceNr on `" + JournalCBBucket.Name + "` (PersistenceId,SequenceNr,DocumentType) USING GSI"; // result = JournalCBBucket.Query<dynamic>(N1QLQueryString); // //if (result.Success != true) // // Debug.Write("Could not create index:idxDocumentType_PersistenceId_SequenceNr"); //} //N1QLQueryString = "SELECT * FROM system:indexes WHERE name = 'idxDocumentType_PersistenceId"; //result = JournalCBBucket.Query<dynamic>(N1QLQueryString); //if (result.Rows.Count == 0 && result.Success == true) //{ // N1QLQueryString = "CREATE INDEX idxDocumentType_PersistenceId on `" + JournalCBBucket.Name + "` (PersistenceId,DocumentType) USING GSI"; // result = JournalCBBucket.Query<dynamic>(N1QLQueryString); // //if (result.Success != true) // // Debug.Write("Could not create index:idxDocumentType_PersistenceId"); //} var HOCON_CB_SnapshotConfig = system.Settings.Config.GetConfig("akka.persistence.snapshot-store.couchbase"); SnapShotStoreSettings = new CouchbaseSnapshotSettings(HOCON_CB_SnapshotConfig); if(SnapShotStoreSettings.UseClusterHelper) { SnapShotStoreCBBucket = ClusterHelper.GetBucket(SnapShotStoreSettings.BucketName); SnapShotStoreCBCluster = SnapShotStoreCBBucket.Cluster; } else { // Are we using the same cluster as the journal? if (SnapShotStoreSettings.CBClientConfiguration.Servers.All(JournalSettings.CBClientConfiguration.Servers.Contains)) { SnapShotStoreCBCluster = JournalCBCluster; // Since we are using the same cluster are we using the same bucket? if (SnapShotStoreSettings.BucketName == JournalSettings.BucketName) { SnapShotStoreCBBucket = JournalCBBucket; } } else // Instantiate the connection to the new cluster { SnapShotStoreCBCluster = new Cluster(SnapShotStoreSettings.CBClientConfiguration); //Open the bucket and make a reference to the CB Client Configuration SnapShotStoreCBBucket = (CouchbaseBucket)JournalCBCluster.OpenBucket(SnapShotStoreSettings.BucketName); } } // Throw an exception if we reach this point without a CB Cluster, CB Config, or Bucket if (SnapShotStoreCBCluster == null) throw new Exception("CouchBase Snapshot Store Cluster could not initialized."); if (SnapShotStoreCBBucket == null) { throw new Exception("CouchBase Snapshot Store bucket could not initialized."); } // Add Snapshot indexes // Add here to create Global Secondary Indexes that cover (See covering Indexes in Couchbase website) to improve performance // First check if the index exists // SELECT * FROM system:indexes WHERE name = 'idxDocumentType_PersistenceId_SequenceNr' // SELECT * FROM system:indexes WHERE name = 'idxDocumentType_PersistenceId_Timestamp' // SELECT * FROM system:indexes WHERE name = 'idxDocumentType_PersistenceId' // Create it if it does not // CREATE INDEX idxDocumentType_PersistenceId_SequenceNr on `SSA` (PersistenceId,SequenceNr,DocumentType) USING GSI // CREATE INDEX idxDocumentType_PersistenceId_Timestamp on `SSA` (PersistenceId,Timestamp,DocumentType) USING GSI // CREATE INDEX idxDocumentType_PersistenceId on `SSA` (DocumentType,PersistenceId) USING GSI //N1QLQueryString = "SELECT * FROM system:indexes WHERE name = 'idxDocumentType_PersistenceId_SequenceNr'"; //result = SnapShotStoreCBBucket.Query<dynamic>(N1QLQueryString); //if (result.Rows.Count == 0 && result.Success == true) //{ // N1QLQueryString = "CREATE INDEX idxDocumentType_PersistenceId_SequenceNr on `" + SnapShotStoreCBBucket.Name + "` (PersistenceId,SequenceNr,DocumentType) USING GSI"; // result = SnapShotStoreCBBucket.Query<dynamic>(N1QLQueryString); // //if (result.Success != true) // // Debug.Write("Could not create index:idxDocumentType_PersistenceId_SequenceNr"); //} //N1QLQueryString = "SELECT * FROM system:indexes WHERE name = 'idxDocumentType_PersistenceId_Timestamp"; //result = SnapShotStoreCBBucket.Query<dynamic>(N1QLQueryString); //if (result.Rows.Count == 0 && result.Success == true) //{ // N1QLQueryString = "CREATE INDEX idxDocumentType_PersistenceId_Timestamp on `" + SnapShotStoreCBBucket.Name + "` (PersistenceId,Timestamp,DocumentType) USING GSI"; // result = SnapShotStoreCBBucket.Query<dynamic>(N1QLQueryString); // //if (result.Success != true) // // Debug.Write("Could not create index:idxDocumentType_PersistenceId_Timestamp"); //} //N1QLQueryString = "SELECT * FROM system:indexes WHERE name = 'idxDocumentType_PersistenceId_Timestamp"; //result = SnapShotStoreCBBucket.Query<dynamic>(N1QLQueryString); //if (result.Rows.Count == 0 && result.Success == true) //{ // N1QLQueryString = "CREATE INDEX idxDocumentType_PersistenceId on `" + SnapShotStoreCBBucket.Name + "` (DocumentType,PersistenceId) USING GSI"; // result = SnapShotStoreCBBucket.Query<dynamic>(N1QLQueryString); // //if (result.Success != true) // // Debug.Write("Could not create index:idxDocumentType_PersistenceId"); //} }
private static void Bootstrap() { _cluster = new Cluster("couchbaseClients/couchbase"); }
public static async Task<GrainStateCouchbaseDataManager> Initialize(CouchbaseClientSection configSection) { var instance = new GrainStateCouchbaseDataManager(); var config = new ClientConfiguration(configSection); _cluster = new Cluster(config); var tcs = new TaskCompletionSource<IBucket>(); Action initAction; if (configSection.Buckets.Count > 0) { var buckets = new BucketElement[configSection.Buckets.Count]; configSection.Buckets.CopyTo(buckets, 0); var bucketSetting = buckets.First(); initAction = () => { tcs.SetResult(_cluster.OpenBucket(bucketSetting.Name)); }; } else initAction = () => { tcs.SetResult(_cluster.OpenBucket()); }; WaitCallback initBucket = (state) => { try { initAction(); } catch (Exception ex) { tcs.SetException(new Exception("GrainStateCouchbaseDataManager initialize exception", ex)); } }; ThreadPool.QueueUserWorkItem(initBucket, null); instance._bucket = await tcs.Task; return instance; }
public override void Init(int flowCount, long flowRecordCount) { buckets = new IBucket[flowCount]; ClientConfiguration client = new ClientConfiguration(); client.Servers.Add(new Uri(ConnectionString)); cluster = new Cluster(client); for (int i = 0; i < flowCount; i++) buckets[i] = cluster.OpenBucket(CollectionName); IBucket bucket = buckets[0]; IViewQuery query = bucket.CreateQuery(DocDesignName, ViewName, false); // TODO: find a faster way for dropping the database. foreach (var item in buckets[0].Query<dynamic>(query).Rows) buckets[0].Remove(item.Key.ToString()); }