Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            // Connect to cassandra cluster  (Cassandra API on Azure Cosmos DB supports only TLSv1.2)
            var options = new Cassandra.SSLOptions(SslProtocols.Tls12, true, ValidateServerCertificate);

            options.SetHostNameResolver((ipAddress) => CassandraContactPoint);
            Cluster  cluster = Cluster.Builder().WithCredentials(UserName, Password).WithPort(CassandraPort).AddContactPoint(CassandraContactPoint).WithSSL(options).Build();
            ISession session = cluster.Connect();

            session = cluster.Connect("uprofile");
            IMapper mapper = new Mapper(session);

            // Inserting Data into user table
            int i = 0;

            while (true)
            {
                try
                {
                    Thread.Sleep(250);
                    Console.WriteLine("inserting record:" + i);
                    mapper.Insert <User>(new User(i, "record" + i, "record" + i));
                    i++;
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error writing record:" + e);
                }
            }
        }
Exemplo n.º 2
0
        public DBService(IConfiguration configuration, ILogger <DBService> logger)
        {
            _configuration = configuration;
            _logger        = logger;

            var options = new Cassandra.SSLOptions(SslProtocols.Tls12, true, ValidateServerCertificate);

            options.SetHostNameResolver((ipAddress) => _configuration["DatabaseSettings:ContactPoint"]);

            cluster = Cluster.Builder().WithCredentials(_configuration["DatabaseSettings:UserName"], _configuration["DatabaseSettings:Password"])
                      .WithPort(Convert.ToInt32(_configuration["DatabaseSettings:Port"])).AddContactPoint(_configuration["DatabaseSettings:ContactPoint"]).WithSSL(options).Build();
        }
Exemplo n.º 3
0
        public static void Main(string[] args)
        {
            // Connect to cassandra cluster  (Cassandra API on Azure Cosmos DB supports only TLSv1.2)
            var options = new Cassandra.SSLOptions(SslProtocols.Tls12, true, ValidateServerCertificate);

            options.SetHostNameResolver((ipAddress) => CassandraContactPoint);
            Cluster cluster = Cluster.Builder().WithCredentials(UserName, Password).WithPort(CassandraPort).AddContactPoint(CassandraContactPoint).WithSSL(options).Build();


            //Cluster cluster = Cluster.Builder().WithConnectionString("AccountEndpoint=https://mydatabasegraph.documents.azure.com:443/;AccountKey=sQfe98WUdRPkN4PSYNDArSjPO4sGpBTFEy9S44CSkdjtO1vD3yLgvIRqyAOHqDRXbTrVMDATTmyV7ir5VZ9WTg==;ApiKind=Gremlin;").WithPort(CassandraPort).AddContactPoint(CassandraContactPoint).WithSSL(options).Build();
            ISession session = cluster.Connect();

            // Creating KeySpace and table
            session.Execute("DROP KEYSPACE IF EXISTS uprofile");
            session.Execute("CREATE KEYSPACE uprofile WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 1 };");
            Console.WriteLine(String.Format("created keyspace uprofile"));
            session.Execute("CREATE TABLE IF NOT EXISTS uprofile.user (user_id int PRIMARY KEY, user_name text, user_bcity text)");
            Console.WriteLine(String.Format("created table user"));

            session = cluster.Connect("uprofile");
            IMapper mapper = new Mapper(session);

            // Inserting Data into user table
            mapper.Insert <User>(new User(1, "LyubovK", "Dubai"));
            mapper.Insert <User>(new User(2, "JiriK", "Toronto"));
            mapper.Insert <User>(new User(3, "IvanH", "Mumbai"));
            mapper.Insert <User>(new User(4, "LiliyaB", "Seattle"));
            mapper.Insert <User>(new User(5, "JindrichH", "Buenos Aires", "Edu"));
            Console.WriteLine("Inserted data into user table");

            Console.WriteLine("Select ALL");
            Console.WriteLine("-------------------------------");
            foreach (User user in mapper.Fetch <User>("Select * from user"))
            {
                Console.WriteLine(user);
            }

            Console.WriteLine("Getting by id 3");
            Console.WriteLine("-------------------------------");
            User userId3 = mapper.FirstOrDefault <User>("Select * from user where user_id = ?", 3);

            Console.WriteLine(userId3);

            // Clean up of Table and KeySpace
            session.Execute("DROP table user");
            session.Execute("DROP KEYSPACE uprofile");

            // Wait for enter key before exiting
            Console.ReadLine();
        }
        public static void Main(string[] args)
        {
            // Connect to cassandra cluster  (Cassandra API on Azure Cosmos DB supports only TLSv1.2)
            var options = new Cassandra.SSLOptions(SslProtocols.Tls12, true, ValidateServerCertificate);

            options.SetHostNameResolver((ipAddress) => CassandraContactPoint);
            Cluster  cluster = Cluster.Builder().WithCredentials(Username, Password).WithPort(CassandraPort).AddContactPoint(CassandraContactPoint).WithSSL(options).Build();
            ISession session = cluster.Connect();

            // Creating KeySpace and table
            session.Execute("DROP KEYSPACE IF EXISTS uprofile");
            session.Execute("CREATE KEYSPACE uprofile WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 1 };");
            Console.WriteLine(String.Format("created keyspace uprofile"));
            session.Execute("CREATE TABLE IF NOT EXISTS uprofile.user (user_id int PRIMARY KEY, user_name text, user_bcity text)");
            Console.WriteLine(String.Format("created table user"));

            session = cluster.Connect("uprofile");
            IMapper mapper = new Mapper(session);

            // Inserting Data into user table
            mapper.Insert <UserEntity>(new UserEntity(1, "LyubovK", "Dubai"));
            mapper.Insert <UserEntity>(new UserEntity(2, "JiriK", "Toronto"));
            mapper.Insert <UserEntity>(new UserEntity(3, "IvanH", "Mumbai"));
            mapper.Insert <UserEntity>(new UserEntity(4, "LiliyaB", "Seattle"));
            mapper.Insert <UserEntity>(new UserEntity(5, "JindrichH", "Buenos Aires"));
            Console.WriteLine("Inserted data into user table");

            Console.WriteLine("Select ALL");
            Console.WriteLine("-------------------------------");
            foreach (UserEntity user in mapper.Fetch <UserEntity>("Select * from user"))
            {
                Console.WriteLine(user);
            }

            Console.WriteLine("Getting by id 3");
            Console.WriteLine("-------------------------------");
            UserEntity userId3 = mapper.FirstOrDefault <UserEntity>("Select * from user where user_id = ?", 3);

            Console.WriteLine(userId3);

            // Clean up of Table and KeySpace
            session.Execute("DROP table user");
            session.Execute("DROP KEYSPACE uprofile");

            // Wait for enter key before exiting
            Console.ReadLine();
        }
Exemplo n.º 5
0
        private ICluster ConfigureCluster(IConfiguration configuration)
        {
            var host     = configuration["Cassandra:Hosts"];
            var port     = int.Parse(configuration["Cassandra:Port"]);
            var user     = configuration["Cassandra:Username"];
            var password = configuration["Cassandra:Password"];

            var options = new Cassandra.SSLOptions(SslProtocols.Tls12, true, ValidateServerCertificate);

            options.SetHostNameResolver((ipAddress) => host);

            return(Cluster.Builder()
                   .WithCredentials(user, password)
                   .WithPort(port)
                   .AddContactPoint(host)
                   .WithSSL(options)
                   .Build());
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            // Connect to cassandra cluster  (Cassandra API on Azure Cosmos DB supports only TLSv1.2)
            var options = new Cassandra.SSLOptions(SslProtocols.Tls12, true, ValidateServerCertificate);

            options.SetHostNameResolver((ipAddress) => CassandraContactPoint);
            Cluster cluster = Cluster.Builder().WithCredentials(UserName, Password)
                              .WithPort(CassandraPort).AddContactPoint(CassandraContactPoint)
                              .WithSSL(options).Build();
            ISession session = cluster.Connect();

            session = cluster.Connect("keyspace01");

            IMapper mapper = new Mapper(session);

            mapper.Insert <People>(new People(1, "Reza", "*****@*****.**"));
            mapper.Insert <People>(new People(2, "John", "*****@*****.**"));
            mapper.Insert <People>(new People(3, "Mary", "*****@*****.**"));
        }
 /// <summary>
 ///  Enables the use of SSL for the created Cluster using the provided options. 
 /// </summary>
 /// <remarks>
 /// If SSL is enabled, the driver will not connect to any
 /// Cassandra nodes that doesn't have SSL enabled and it is strongly
 /// advised to enable SSL on every Cassandra node if you plan on using
 /// SSL in the driver. Note that SSL certificate common name(CN) on Cassandra node must match Cassandra node hostname.
 /// </remarks>
 /// <param name="sslOptions">SSL options to use.</param>
 /// <returns>this builder</returns>        
 // ReSharper disable once InconsistentNaming
 public new DseClusterBuilder WithSSL(SSLOptions sslOptions)
 {
     base.WithSSL(sslOptions);
     return this;
 }
Exemplo n.º 8
0
 /// <summary>
 /// Creates a new instance of TcpSocket using the endpoint and options provided.
 /// </summary>
 public TcpSocket(IPEndPoint ipEndPoint, SocketOptions options, SSLOptions sslOptions)
 {
     IPEndPoint = ipEndPoint;
     Options = options;
     SSLOptions = sslOptions;
 }
Exemplo n.º 9
0
 /// <summary>
 ///  Enables the use of SSL for the created Cluster using the provided options. 
 /// </summary>
 /// <remarks>
 /// If SSL is enabled, the driver will not connect to any
 /// Cassandra nodes that doesn't have SSL enabled and it is strongly
 /// advised to enable SSL on every Cassandra node if you plan on using
 /// SSL in the driver. Note that SSL certificate common name(CN) on Cassandra node must match Cassandra node hostname.
 /// </remarks>
 /// <param name="sslOptions">SSL options to use.</param>
 /// <returns>this builder</returns>        
 public Builder WithSSL(SSLOptions sslOptions)
 {
     _sslOptions = sslOptions;
     return this;
 }
Exemplo n.º 10
0
 /// <summary>
 ///  Enables the use of SSL for the created Cluster. Calling this method will use default SSL options. 
 /// </summary>
 /// <remarks>
 /// If SSL is enabled, the driver will not connect to any
 /// Cassandra nodes that doesn't have SSL enabled and it is strongly
 /// advised to enable SSL on every Cassandra node if you plan on using
 /// SSL in the driver. Note that SSL certificate common name(CN) on Cassandra node must match Cassandra node hostname.
 /// </remarks>
 /// <returns>this builder</returns>
 public Builder WithSSL()
 {
     _sslOptions = new SSLOptions();
     return this;
 }
Exemplo n.º 11
0
 /// <summary>
 ///  Enables the use of SSL for the created Cluster using the provided options.
 /// </summary>
 /// <remarks>
 /// If SSL is enabled, the driver will not connect to any
 /// Cassandra nodes that doesn't have SSL enabled and it is strongly
 /// advised to enable SSL on every Cassandra node if you plan on using
 /// SSL in the driver. Note that SSL certificate common name(CN) on Cassandra node must match Cassandra node hostname.
 /// </remarks>
 /// <param name="sslOptions">SSL options to use.</param>
 /// <returns>this builder</returns>
 public Builder WithSSL(SSLOptions sslOptions)
 {
     _addedSsl   = true;
     _sslOptions = sslOptions;
     return(this);
 }
Exemplo n.º 12
0
 /// <summary>
 ///  Enables the use of SSL for the created Cluster. Calling this method will use default SSL options.
 /// </summary>
 /// <remarks>
 /// If SSL is enabled, the driver will not connect to any
 /// Cassandra nodes that doesn't have SSL enabled and it is strongly
 /// advised to enable SSL on every Cassandra node if you plan on using
 /// SSL in the driver. Note that SSL certificate common name(CN) on Cassandra node must match Cassandra node hostname.
 /// </remarks>
 /// <returns>this builder</returns>
 public Builder WithSSL()
 {
     _addedSsl   = true;
     _sslOptions = new SSLOptions();
     return(this);
 }
Exemplo n.º 13
0
 /// <summary>       
 /// Creates a new ProtocolOptions instance using the provided port and SSL context.        
 /// </summary>
 /// <param name="port">the port to use for the binary protocol.</param>
 /// <param name="sslOptions">sslOptions the SSL options to use. Use null if SSL is not to be used.</param>
 public ProtocolOptions(int port, SSLOptions sslOptions)
 {
     this._port = port;
     this._sslOptions = sslOptions;
 }
Exemplo n.º 14
0
        public static void Main(string[] args)
        {
            // validate UserName
            if (string.IsNullOrEmpty(UserName))
            {
                Console.WriteLine("Invalid User Name\n\nExport cname environment value");
                return;
            }

            // validate password
            if (string.IsNullOrEmpty(Password))
            {
                Console.WriteLine("Invalid Password\n\nExport cpass environment value");
                return;
            }

            string CassandraContactPoint = UserName + ".cassandra.cosmosdb.azure.com";
            int    CassandraPort         = 10350;

            // Connect to cassandra cluster  (Cassandra API on Azure Cosmos DB supports only TLSv1.2)
            var options = new Cassandra.SSLOptions(SslProtocols.Tls12, true, ValidateServerCertificate);

            options.SetHostNameResolver((ipAddress) => CassandraContactPoint);
            Cluster  cluster = Cluster.Builder().WithCredentials(UserName, Password).WithPort(CassandraPort).AddContactPoint(CassandraContactPoint).WithSSL(options).Build();
            ISession session = cluster.Connect("myapp");

            Console.WriteLine("connected to myapp");

            // create a new table each time the app runs
            session.Execute("DROP TABLE IF EXISTS user;");

            // Create table
            // Set CosmosDB RUs to 400 (lowest possible)
            session.Execute("CREATE TABLE IF NOT EXISTS user (userid int, name text, PRIMARY KEY (userid)) WITH cosmosdb_provisioned_throughput=400");

            const string sql = "insert into user (userid, name) values ({0}, '{1}')";
            int          id  = 0;

            // insert via execute
            session.Execute(string.Format(sql, ++id, "Bart"));
            session.Execute(string.Format(sql, ++id, "Carla"));
            session.Execute(string.Format(sql, ++id, "Joshua"));
            session.Execute(string.Format(sql, ++id, "Sasha"));

            IMapper mapper = new Mapper(session);

            // insert via mapper (just another way to insert)
            mapper.Insert <User>(new User(++id, "Matthew"));

            // query via mapper
            Console.WriteLine("Select ALL");
            Console.WriteLine("-------------------------------");
            foreach (User user in mapper.Fetch <User>("Select * from user"))
            {
                Console.WriteLine(user);
            }

            Console.WriteLine("\n\nSelect one user");
            Console.WriteLine("-------------------------------");

            // query via RowSet
            var rs = session.Execute("select userid, name from user where userid = 1");

            foreach (var r in rs)
            {
                int    userid = r.GetValue <int>(0);
                string name   = r.GetValue <string>(1);
                Console.WriteLine(string.Format(" {0} | {1} ", userid, name));
            }
        }
Exemplo n.º 15
0
 /// <summary>
 /// Creates a new instance of TcpSocket using the endpoint and options provided.
 /// </summary>
 public TcpSocket(IPEndPoint ipEndPoint, SocketOptions options, SSLOptions sslOptions)
 {
     this.IPEndPoint = ipEndPoint;
     this.Options    = options;
     this.SSLOptions = sslOptions;
 }
Exemplo n.º 16
0
 /// <summary>
 /// Creates a new instance of TcpSocket using the endpoint and options provided.
 /// </summary>
 public TcpSocket(IPEndPoint ipEndPoint, SocketOptions options, SSLOptions sslOptions)
 {
     this.IPEndPoint = ipEndPoint;
     this.Options = options;
     this.SSLOptions = sslOptions;
 }