AerospikeTraining.TweetService.createTweets C# (CSharp) Method

createTweets() public method

public createTweets ( ) : void
return void
        public void createTweets()
        {
            string[] randomTweets = { "For just $1 you get a half price download of half of the song and listen to it just once.", "People tell me my body looks like a melted candle", "Come on movie! Make it start!", "Byaaaayy", "Please, please, win! Meow, meow, meow!", "Put. A. Bird. On. It.", "A weekend wasted is a weekend well spent", "Would you like to super spike your meal?", "We have a mean no-no-bring-bag up here on aisle two.", "SEEK: See, Every, EVERY, Kind... of spot", "We can order that for you. It will take a year to get there.", "If you are pregnant, have a soda.", "Hear that snap? Hear that clap?", "Follow me and I may follow you", "Which is the best cafe in Portland? Discuss...", "Portland Coffee is for closers!", "Lets get this party started!", "How about them portland blazers!", "You got school'd, yo", "I love animals", "I love my dog", "What's up Portland", "Which is the best cafe in Portland? Discuss...", "I dont always tweet, but when I do it is on Tweetaspike" };
            Random rnd1 = new Random();
            Random rnd2 = new Random();
            Random rnd3 = new Random();
            Key userKey;
            Record userRecord;
            int totalUsers = 10000;
            int maxTweets = 20;
            string username;
            long ts = 0;
            WritePolicy wPolicy = new WritePolicy();
            wPolicy.recordExistsAction = RecordExistsAction.UPDATE;

            Console.WriteLine("\nCreate up to " + maxTweets + " tweets each for " + totalUsers + " users. Press any key to continue...");
            Console.ReadLine();

            for (int j = 0; j < totalUsers; j++)
            {
                // Check if user record exists
                username = "user" + rnd3.Next(1, 100000);
                userKey = new Key("test", "users", username);
                userRecord = client.Get(null, userKey);
                if (userRecord != null)
                {
                    // create up to maxTweets random tweets for this user
                    int totalTweets = rnd1.Next(1, (maxTweets + 1));
                    for (int k = 1; k <= totalTweets; k++)
                    {
                        // Create timestamp to store along with the tweet so we can query, index and report on it
                        ts = getTimeStamp();
                        Key tweetKey = new Key("test", "tweets", username + ":" + k);
                        Bin bin1 = new Bin("tweet", randomTweets[rnd2.Next(1, randomTweets.Length)]);
                        Bin bin2 = new Bin("ts", ts);
                        Bin bin3 = new Bin("username", username);

                        client.Put(wPolicy, tweetKey, bin1, bin2, bin3);
                    }
                    if (totalTweets > 0)
                    {
                        // Update tweet count and last tweet'd timestamp in the user record
                        client.Put(wPolicy, userKey, new Bin("tweetcount", totalTweets), new Bin("lasttweeted", ts));
                        //Console.WriteLine("INFO: The tweet count now is: " + totalTweets);
                    }
                    Console.WriteLine("Wrote " + totalTweets + " tweets for " + username + "!");
                }
            }
            Console.WriteLine("\nDone creating up to " + maxTweets + " tweets each for " + totalUsers + " users!");
        }

Usage Example

Example #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Welcome to Aerospike Developer Training *****\n");
            AerospikeClient client = null;

            try
            {
                Console.WriteLine("INFO: Connecting to Aerospike cluster...");

                // Connecting to Aerospike cluster

                // TODO: Establish a connection to Aerospike cluster
                // Exercise 1
                Console.WriteLine("\nTODO: Establish a connection to Aerospike cluster");

                // TODO: Check to see if the cluster connection succeeded
                // Exercise 1
                Console.WriteLine("\nTODO: Check to see if the cluster connection succeeded");

                if (false)
                {
                    Console.WriteLine("INFO: Connection to Aerospike cluster succeeded!\n");

                    // Create instance of UserService
                    UserService us = new UserService(client);
                    // Create instance of TweetService
                    TweetService ts = new TweetService(client);

                    // Present options
                    Console.WriteLine("What would you like to do:");
                    Console.WriteLine("1> Create A User And A Tweet");
                    Console.WriteLine("2> Read A User Record");
                    Console.WriteLine("3> Batch Read Tweets For A User");
                    Console.WriteLine("4> Scan All Tweets For All Users");
                    Console.WriteLine("5> Record UDF -- Update User Password");
                    Console.WriteLine("6> Query Tweets By Username And Users By Tweet Count Range");
                    Console.WriteLine("7> Stream UDF -- Aggregation Based on Tweet Count By Region");
                    Console.WriteLine("0> Exit");
                    Console.Write("\nSelect 0-7 and hit enter:");
                    byte feature = byte.Parse(Console.ReadLine());

                    if (feature != 0)
                    {
                        switch (feature)
                        {
                        case 1:
                            Console.WriteLine("\n********** Your Selection: Create User And A Tweet **********\n");
                            us.createUser();
                            ts.createTweet();
                            break;

                        case 2:
                            Console.WriteLine("\n********** Your Selection: Read A User Record **********\n");
                            us.getUser();
                            break;

                        case 3:
                            Console.WriteLine("\n********** Your Selection: Batch Read Tweets For A User **********\n");
                            us.batchGetUserTweets();
                            break;

                        case 4:
                            Console.WriteLine("\n**********  Your Selection: Scan All Tweets For All Users **********\n");
                            ts.scanAllTweetsForAllUsers();
                            break;

                        case 5:
                            Console.WriteLine("\n********** Your Selection: Record UDF -- Update User Password **********\n");
                            //us.updatePasswordUsingUDF();
                            us.updatePasswordUsingCAS();
                            break;

                        case 6:
                            Console.WriteLine("\n**********  Your Selection: Query Tweets By Username And Users By Tweet Count Range **********\n");
                            ts.queryTweetsByUsername();
                            ts.queryUsersByTweetCount();
                            break;

                        case 7:
                            Console.WriteLine("\n**********  Your Selection: Stream UDF -- Aggregation Based on Tweet Count By Region **********\n");
                            us.aggregateUsersByTweetCountByRegion();
                            break;

                        case 12:
                            Console.WriteLine("\n********** Create Users **********\n");
                            us.createUsers();
                            break;

                        case 23:
                            Console.WriteLine("\n********** Create Tweets **********\n");
                            ts.createTweets();
                            break;

                        default:
                            Console.WriteLine("\n********** Invalid Selection **********\n");
                            break;
                        }
                    }
                }
                else
                {
                    Console.Write("ERROR: Connection to Aerospike cluster failed! Please check IP & Port settings and try again!");
                    Console.ReadLine();
                }
            }
            catch (AerospikeException e)
            {
                Console.WriteLine("AerospikeException - Message: " + e.Message);
                Console.WriteLine("AerospikeException - StackTrace: " + e.StackTrace);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception - Message: " + e.Message);
                Console.WriteLine("Exception - StackTrace: " + e.StackTrace);
            }
            finally
            {
                // TODO: Close Aerospike cluster connection
                // Exercise 1
                Console.WriteLine("\nTODO: Close Aerospike cluster connection");
                Console.ReadLine();
            }
        } //main
All Usage Examples Of AerospikeTraining.TweetService::createTweets