Thursday, February 19, 2009

Tweeting from the Scala interpreter

Verifying ricky_clarkson's post. Nice!

.oO(esmith@esmith-md jtwitter) wget
=> `jtwitter.jar'
Resolving done.
Connecting to[]:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 197,281 [application/java-archive]

100%[==========>] 197,281 175.94K/s ETA 00:00

17:04:22 (175.94 KB/s) - `jtwitter.jar' saved [197281/197281]

.oO(esmith@esmith-md jtwitter) scala -classpath jtwitter.jar
Welcome to Scala version (Java HotSpot(TM)
Client VM, Java 1.5.0_16).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.collection.jcl.Conversions._
import scala.collection.jcl.Conversions._

scala> import winterwell.jtwitter.Twitter
import winterwell.jtwitter.Twitter

scala> val twitter = new Twitter("eishay", "*****")
twitter: winterwell.jtwitter.Twitter =

scala> twitter.getFriendsTimeline() take 10 map (s => s.getUser +
":" + s) foreach println
Slashdot:Security Researcher Kaminsky Pushes DNS Patching
Joe Nuxoll:Hey - everyone going to the Java Posse Roundup 2009,
please follow the twitter account @JPR09
WebGuild:Why Sun's New Cloud CTO Targeting Migration of Legacy
Apps First: By Chris Preimesberger Sun's new cloud computi..
WebGuild:Mobile Ads Stick: According to a study released today
by eMarketer, and reported on, mobile ads ..
WebGuild:Webstock 09 : Russ Weakley: Works at the Australian
Museum. Had an idea for the museum web site 4 years ago and
WebGuild:Cloud Computing Is a Tool, Not a Strategy: This week
IÕm listening in on HP talk to some of its customers about
popurls:Leaked Photo of the Next-Generation Mac Mini?
ZDNet Blogs:The message from GSMA Barcelona: fragmentation -
linkedin_news:"Facebook retains terms of service after users
voice concerns" by usa today [Facebook 100% ]
Technology Geek:Lifehacker - Best Live CDs? [Hive Five Call For Contenders]

scala> twitter.setStatus("Tweeting from the Scala interpreter")
res7: winterwell.jtwitter.Twitter.Status = Tweeting from the Scala interpreter

Monday, February 16, 2009

Fun with Voldemort, Scala and Protobuf

Here is an idea for an alternative for the Voldemort shell. I added one Class and use the Scala interactive shell.
Below is an example of how you can use it to manipulate Java and Protobuf objects (of course it can be applied to other stuff), do scripting/grandfathering and enjoy the interactive shell.

Before we go on, you can use this shell without protobuf or scala, they are in the example only for proof of concept.
For the example I created two 'person' classes, one POJO and the other Java generated form protobuf.
The example simulates put/get/copy/grandfathering of a list of objects.
This is a copy from the shell:

scala>//this is a comment (like in C).
scala>//creating pojo store client. note how it prints some info
about itself after construction.
scala> var pojoStore = new VoldemortClient[String,Person]
Established connection to pojo-store via tcp://localhost:6666
pojoStore: voldemort.VoldemortClient[String,test.pojo.Person] =
store name : pojo-store
bootstrap url : tcp://localhost:6666
key serializer: StringSerializer
val serializer: ObjectSerializer

scala>// creates a person, load it to pojoStore,
download it back and prints its first name
scala> var me = new Person("Lord", "Voldemort", 666)
me: test.pojo.Person = test.pojo.Person@d5ea2c
scala> pojoStore put ("t1", me)
scala> pojoStore get "t1" getFirstName
res31: java.lang.String = Lord

scala>//The line below creates a list of three person objects
scala> val pojos = List(new Person("Harry", "Potter", 1),
new Person("Rubeus", "Hagrid", 2),
new Person("Ron", "Weasley", 3))

scala>// closure that loads a person object with its ID as key
scala> val upload = (p: Person) => pojoStore put (p.getId.toString, p)
upload: (test.pojo.Person) => Unit =

scala>// loads the person objects to the store
scala> pojos foreach upload

scala>// closure that retrieves the loaded persons from store
and prints their first name
scala> val getNames = (p: Person) =>
println(pojoStore get (p.getId.toString) getFirstName)
scala> pojos foreach getNames

scala>// creating protobuf store client. note the value serializer.
PersonPBO.Person is a protobuf Message class.
scala> var protoStore = new VoldemortClient[String,PersonPBO.Person]
Established connection to proto-store via tcp://localhost:6666
protoStore: voldemort.VoldemortClient[int,test.proto.PersonPBO.Person] =
store name : proto-store
bootstrap url : tcp://localhost:6666
key serializer: StringSerializer
val serializer: ProtoBufSerializer

scala>// converts a person to protobuf
scala> val toProto = (p: Person) =>
PersonPBO.Person newBuilder() setId(p getId)
setFirstName(p getFirstName) setLastName(p getLastName) build
toProto: (test.pojo.Person) => test.proto.PersonPBO.Person =

scala>// loads the protobuf object to the protoStore
scala> val copyToProto = (p: PersonPBO.Person) =>
protoStore put (p.getId.toString, p)
copyToProto: (test.proto.PersonPBO.Person) => Unit =

scala>// downloads the pojo object from the pojoStore
scala> val get = (id: Int) => pojoStore get (id.toString)
get: (Int) => test.pojo.Person =

scala>// download pojos from pojoStore, create protos and
upload them to protoStore
scala> List(1, 2, 3) map get map toProto foreach copyToProto

scala>// closure that downloads and prints objects from protoStore
scala> val getProto = (id: Int) => println(protoStore get (id.toString))
getProto: (Int) => Unit =

scala>// the printout is the protobufs the next line found on the protoStore
scala> List(1, 2, 3) foreach getProto
firstName: "Harry"
lastName: "Potter"
id: 1

firstName: "Rubeus"
lastName: "Hagrid"
id: 2

firstName: "Ron"
lastName: "Weasley"
id: 3

The example above is basic, it can be enhanced much more.

Creative Commons License This work by Eishay Smith is licensed under a Creative Commons Attribution 3.0 Unported License.