Introduction
Three months in the past, I wrote a couple of new command, twitter2stata, that imports information from Twitter’s REST API into Stata. Right now, I’ll present you the instruments we used to develop this command. Penning this command from scratch solely in Mata or ado-code would have taken a number of months. Fortuitously, we will considerably pace up our growth utilizing an current Java library (Twitter4J) and Stata’s Java plugins. On this submit, I’ll focus on the essential steps of methods to leverage a Java library and the Stata Java API.
Java is the most well-liked programming language on the planet, so there are various libraries to help your growth. A fast Google search ought to let you know if a Java library exists for what you are attempting to do; that is how we discovered the library Twitter4J. For the remainder of this weblog entry, a primary understanding of programming in Java is useful, however not needed.
Setup
The most typical approach to compile Java code is to make use of an built-in growth setting (IDE). For this text, I’ll use Java IDE Eclipse, model Oxygen. Even you probably have a unique model of Eclipse, the steps needs to be comparable. You’ll be able to obtain the newest model of Eclipse from the beneath URL if you need to comply with this instance.
https://eclipse.org/downloads/
After downloading and putting in Eclipse, you’ll need to obtain the Java library twitter4j to work together with Twitter’s API.
Create venture and bundle
To begin, we should create a Mission in Eclipse by opening Eclipse, going to the File menu, and deciding on New->Java Mission. It is best to now see the New Java Mission dialog.
I named my venture test_twitter, however you may identify your something you want. Click on the End button and it is best to now see your venture identify within the Bundle Explorer pane on the left-hand aspect of Eclipse.
Add libraries to bundle
Now that we have now our venture, we have to add the twitter4J Java library and the Stata SFI library to it in order that we will work together with Twitter’s API and Stata. To do that, unzip the file you downloaded from twitter4j.org. After you unzip the file, there needs to be a listing, lib, that accommodates the libraries we’d like. Bear in mind this location.
So as to add the twitter4J JAR information to our venture, right-click on the test_twitter bundle identify within the Bundle Explorer and choose Properties.
Within the Properties dialog, choose Java Construct Path and click on on the Libraries tab. Then, click on on the Add Exterior JARs… button to convey up the Jar Choice dialog. Browse to the lib listing the place you unzipped the twitter4J JAR information, choose all JAR information within the listing, and click on the Open button.
Subsequent, we have to add the Stata Perform Interface JAR file. The file sfi-api.jar is situated in your Stata set up listing. Once more, click on on the Add Exterior JARs… button to convey up the Jar Choice dialog. Find your Stata set up listing after which click on on the utilities/jar listing. For instance, on my laptop, Stata is put in in
C:Program Information (x86)Stata15utilitiesjar
Discover the file sfi-api.jar, choose it, after which click on the Open button. Final, within the Properties dialog, press the Apply and shut button.
Create bundle and sophistication
Now that the libraries have been added to our venture, let’s create a bundle by including a category to the venture. So as to add a category to the venture, go to File->New->Class and it is best to see:
While you add a category to a venture, you could additionally identify the bundle the place the category is saved. A Java bundle is only a identify house. When naming your bundle the Java commonplace is to make use of a URL as a part of your bundle identify house, which in my case is stata.com. You’ll be able to identify the bundle something, however try to identify it one thing distinctive to keep away from Java bundle identify collisions afterward. To maintain issues easy, I named my bundle com.stata.kcrow. I named my class StTwitter. Click on End to create your bundle and sophistication.
Hi there world
To check that we have now every part arrange appropriately, let’s write a easy helloWorld (member) perform in our StTwitter class to check the Stata SFI. You’ll be able to copy and paste the beneath code into Eclipse code editor after which save the venture.
bundle com.stata.kcrow;
import com.stata.sfi.* ;
public class StTwitter {
public static int helloWorld(String args[]) {
SFIToolkit.error("Hi there World!");
return(0);
}
}
Observe that the tactic to be referred to as should be carried out with a particular Java signature within the following kind:
static int java_method_name(String[] args)
To make use of this perform in Stata, we should export our code to a JAR file for Stata to learn. To do that, once more right-click on the test_twitter bundle identify within the Bundle Explorer and choose Export. Within the Export dialog, choose Java->JAR file after which click on Subsequent.
Within the Jar Export dialog, we have to export the file to a listing the place Stata can discover it. To do that, save the JAR file alongside Stata’s adopath. When you don’t know what Stata’s adopath is ready to in your laptop, open Stata and kind adopath within the Stata Command window. For instance,
. adopath [1] (BASE) "C:Program Information (x86)Stata15adobase/" [2] (SITE) "C:Program Information (x86)Stata15adosite/" [3] "." [4] (PERSONAL) "c:adopersonal/" [5] (PLUS) "c:adoplus/" [6] (OLDPLACE) "c:ado/"
We must always save the file within the PERSONAL listing on this case. Additionally, make sure that the test field Export generated class information and assets is checked. Click on the End button.
Calling the category from Stata
javacall is the Stata command we use to load our plugin. The syntax is
javacall class methodology [varlist] [if] [in], classpath(classpath) [args(arg_list)]
For instance, to name our helloWorld methodology from Stata, we sort:
. javacall com.stata.kcrow.StTwitter helloWorld, jars(test_twitter.jar) Hi there World!
within the Stata Command window. You too can wrap the javacall command in an ado-file to make it simpler to name from Stata. For instance, it can save you the file twitter_test.ado, which accommodates the beneath code, alongside your ado-path.
program outline twitter_test
javacall com.stata.kcrow.StTwitter helloWorld, jars(test_twitter.jar)
finish
Now, to name our StTwitter class, we will sort twitter_test in Stata.
. twitter_test Hi there World!
In my subsequent submit, I’ll deal with Twitter API authentication and looking tweets utilizing twitter4j.





