Copy existing an existing table in Google Cloud Big Query

Google Cloud Platform is a set of modular cloud-based services that allow you to create anything from simple websites to complex applications.

Reference: https://cloud.google.com/

Google Cloud Platform

Google Cloud Platform

This guide contains everything you need to know how to copy existing an existing table in Google Cloud Big Query.

Getting Started.

Download bq Command-Line Tool here, https://cloud.google.com/sdk/. (This tutorial focusing on Mac platform only). For Windows user can find it here, https://cloud.google.com/sdk/#windows.

Download and install Google Cloud SDK by running the following command in your shell or Terminal:

curl https://sdk.cloud.google.com | bash
Google Cloud SDK installation

Google Cloud SDK installation

Restart your shell using this command:

exec -l $SHELL

Authenticate to Google Cloud Platform by running gcloud auth login.

gcloud auth login
gcloud auth login

gcloud auth login

Choose your google account and accept the permission.

Google Cloud SDK ask your permission.

Google Cloud SDK ask your permission.

You will redirect to this page after you accept the permission.

Google Cloud SDK auth_success page.

Google Cloud SDK auth_success page.

Google Cloud SDK saved application default credentials.

Google Cloud SDK saved application default credentials.

Before you copy the table, make sure you enable your billing first (don’t worry, it won’t charge you for 14 days trial account). To copy a table, run the bq cp command:

bq cp <source_table> <destination_table>

For example, a sample copy command might look like the following:

bq cp dataset.mytable dataset2.mynewtable

You can also copy tables across different projects by specifying the project id in the source and destination path:

bq cp 123456789123:dataset.mytable 0987654321098:dataset2.mynewtable
Copy an Existing Table

Copy an Existing Table

You can see the results below. From this picture you can determine yourself which one is project id, dataset and table.

Google BigQuery

Google BigQuery

Have fun!!

Develop a simple iOS mobile app (toDoList) using Swift – Part 2

In the first part of the simple iOS mobile app (toDoList) tutorial series, you covered the basics of setting environment and user interface/autolayout.

In this second and final part of the tutorial series, we’ll cover Parse integration.

We’ll start where we left off last tutorial, so open your project from last time, or go through the previous tutorial first. http://blog.revivalx.com/2015/05/13/develop-a-simple-ios-mobile-app-todolist-using-swift-part-1/

Select UITableViewController in Main.Storyboard and edit Attribute Inspector according to this setting:

Style: Basic
Identifier: TaskCell
Accessory: Disclosure Indicator

Attribute Inspector.

Attribute Inspector.

UiTableViewController

UiTableViewController

Go to UIViewController and make sure the user interface is similar like this one. Don’t forget to set constraints for each elements. If you don’t know how to do it, feel free to refer it here, https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/DesigningaUserInterface.html.

UIViewController.

UIViewController.

Sign up for your new account here, https://www.parse.com/.

Parse

Parse

Click Create a New App.

Create a New App

Create a New App

Go to Keys and save your Application ID and Client Key. Open AppDelegate.swift file and update the Application ID and Client Key.

import UIKit
import ParseFacebookUtils
 
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
 
  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; Bool {
    // [Optional] Power your app with Local Datastore. For more info, go to
    // https://parse.com/docs/ios_guide#localdatastore/iOS
    Parse.enableLocalDatastore()
 
    // Initialize Parse.
    Parse.setApplicationId(&quot;xxxxx&quot;,
      clientKey: &quot;xxxxx&quot;)
 
    // [Optional] Track statistics around application opens.
    PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
 
    // ...
}
 
// ...

Create two Cocoa Touch Class. One is TableViewController.swift (UITableViewController) and the other one is ViewController.swift (UIViewController).

Xcode 6.4 - New File.

Xcode 6.4 – New File.

Cocoa Touch Class

Cocoa Touch Class

UITableViewController

UITableViewController

UIViewController

UIViewController

Project navigator

Project navigator

Then make sure you attach both files into each controllers in Identity inspector.

Identity inspector - UITableViewController

Identity inspector – UITableViewController

Identity inspector -UIViewController

Identity inspector -UIViewController

Select Table View Cell, go to Identity Inspector and select PFTableViewCell.

PFTableViewCell

PFTableViewCell

Create Task model.

Swift File.

Swift File.

Project navigator

Project navigator

Go to the Task.swift file and replace with this code.

import Foundation
import UIKit

class Task: NSObject {
    var taskId: Int
    var name: String
    
    init(taskId: Int, name: String) {
        self.taskId = taskId
        self.name = name
        super.init()
    }
}

Open TableViewController.swift and update the with this code.

import UIKit
import Parse
import ParseUI

class TableViewController: PFQueryTableViewController {
    
    var id: String!
    var name: String!
    var descriptions: String!
    
    override init(style: UITableViewStyle, className: String?) {
        super.init(style: style, className: className)
    }
    
    @IBAction func addNewTask(sender: AnyObject) {
        self.id = nil
        self.performSegueWithIdentifier("MySegue", sender: self)
    }
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        self.parseClassName = "Task"
        self.textKey = "name"
        self.pullToRefreshEnabled = true
        self.paginationEnabled = false
        
    }
    
    override func queryForTable() -> PFQuery {
        var query = PFQuery(className: "Task")
        query.orderByAscending("name")
        
        
        return query
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tableView.estimatedRowHeight = 44
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.reloadData()
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
        var cell = tableView.dequeueReusableCellWithIdentifier("TaskCell") as! PFTableViewCell!
        if cell == nil {
            cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
        }
        
        cell.textLabel?.text = object!["name"] as? String
        
        return cell
    }

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            let objectToDelete = objects?[indexPath.row] as! PFObject
            objectToDelete.deleteInBackgroundWithBlock {
                (success: Bool, error: NSError?) -> Void in
                if (success) {
                    self.tableView.reloadData()
                }
            }
        }
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let object = objects?[indexPath.row] as! PFObject
        var query = PFQuery(className:"Task")
        query.getObjectInBackgroundWithId(object.objectId!) {
            (task: PFObject?, error: NSError?) -> Void in
            if error == nil && task != nil {
                self.id = task!.objectId!
                self.name = task!["name"] as! String
                self.descriptions = task!["description"] as! String
                self.performSegueWithIdentifier("MySegue", sender: self)
            } else {
                print(error)
            }
        }
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "MySegue" {
            let viewController: ViewController = segue.destinationViewController as! ViewController
            viewController.id = id
            viewController.name = name
            viewController.descriptions = descriptions
        }
    }
}

Open ViewController.swift and replace the with this code.

import UIKit
import Parse

class ViewController: UIViewController {

    @IBOutlet weak var txtName: UITextField!
    @IBOutlet weak var txtDescription: UITextField!
    var id: String!
    var name: String!
    var descriptions: String!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if id != nil {
            txtName.text = name
            txtDescription.text = descriptions
        }
    }

    @IBAction func btnSubmit(sender: AnyObject) {
        if id != nil {
            var query = PFQuery(className:"Task")
            query.getObjectInBackgroundWithId(id) {
                (task: PFObject?, error: NSError?) -> Void in
                if error != nil {
                    print(error)
                } else if let task = task {
                    task["name"] = self.txtName.text
                    task["description"] = self.txtDescription.text
                    task.saveInBackground()
                    self.navigationController!.popViewControllerAnimated(true)
                }
            }
        } else {
            var task = PFObject(className:"Task")
            task["name"] = txtName.text
            task["description"] = txtDescription.text
            task.saveEventually()
            navigationController!.popViewControllerAnimated(true)
        }
    }
}

Result

ToDoList Screenshot 1

ToDoList Screenshot 1

 

ToDoList Screenshot 2

ToDoList Screenshot 2

How To Install Ruby on Rails with rbenv on CentOS 7

Ruby on Rails, or simply Rails, is a web application framework written in Ruby under MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web pages. It encourages and facilitates the use of web standards such as JSON or XML for data transfer, and HTML, CSS and JavaScript for display and user interfacing. In addition to MVC, Rails emphasizes the use of other well-known software engineering patterns and paradigms, including convention over configuration (CoC), don’t repeat yourself (DRY), and the active record pattern.

Ruby on Rails

Ruby on Rails

Reference: https://en.wikipedia.org/wiki/Ruby_on_Rails

Getting Started.

Open your terminal and type this command to login via SSH. (Use your own IP server)

ssh root@128.160.213.199
Login via SSH

Login via SSH

Install the rbenv and Ruby dependencies with yum using this command:

sudo yum install -y git-core zlib zlib-devel gcc-c++ patch readline readline-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison curl sqlite-devel
Install the rbenv and Ruby dependencies with yum:

Install the rbenv and Ruby dependencies with yum:

Download rbenv and set path.

git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
exec $SHELL

git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bash_profile
exec $SHELL

Next, install Ruby,

RUBY_CONFIGURE_OPTS=--disable-install-doc rbenv install 2.2.2

Then we install Rails,

sudo yum install -y epel-release
sudo yum install -y rubygem-nokogiri

You’re now ready to start developing your new Ruby on Rails application. Good luck!

Calculate age from birth date using Swift

Here is how to calculate age from birth date using Swift.

import UIKit

class _ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let myDOB = NSCalendar.currentCalendar().dateWithEra(1, year: 1970, month: 09, day: 10, hour: 0, minute: 0, second: 0, nanosecond: 0)!
        let myAge = myDOB.age
        println(self.calculateAge(myDOB))
    }

    func calculateAge (birthday: NSDate) -> Int {
        return NSCalendar.currentCalendar().components(.CalendarUnitYear, fromDate: birthday, toDate: NSDate(), options: nil).year
    }

}

extension NSDate {
    var age:Int {
        return NSCalendar.currentCalendar().components(.CalendarUnitYear, fromDate: self, toDate: NSDate(), options: nil).year
    }
}