-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataFrameStartCluster_3.java
160 lines (132 loc) · 5.99 KB
/
dataFrameStartCluster_3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package org.apache.asterix.lang.common.dataFrame;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.lang.ProcessBuilder;
import java.lang.Process;
import java.io.InputStreamReader;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.File;
import org.json.*;
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
/**
* Created by MingdaLi on 4/5/16.
*/
public class dataFrame {
private String dataverse;
private String dataset;
public dataFrame(){
this.dataverse=null;
this.dataset=null;
}
public void buildDataverse(){
dataFrame AsterixDB=new dataFrame();
String result=null;
AsterixDB.buildAsterixDBInstance();
String urlQuery="http://localhost:19002/ddl?ddl=drop%20dataverse%20company%20if%20exists;create%20dataverse%20company;use%20dataverse%20company;create%20type%20Emp%20as%20open%20{id%20:%20int32,name%20:%20string};create%20dataset%20Employee(Emp)%20primary%20key%20id;";
result=AsterixDB.httpQueryAsterix(urlQuery);
String urlQuery2="http://localhost:19002/update?statements=use%20dataverse%20company;insert%20into%20dataset%20Employee({%20%22id%22:123,%22name%22:%22John%20Doe%22});";
result=AsterixDB.httpQueryAsterix(urlQuery2);
}
public String show(String dataverseName, String datasetName){
String urlQuery="http://localhost:19002/query?query=use%20dataverse%20"+dataverseName+";for%20$l%20in%20dataset('"+datasetName+"')%20return%20$l;";
return this.httpQueryAsterix(urlQuery);
}
public void load(){
}
/*execut rest http to read from AsterixDB or write to AsterixDB*/
public String httpQueryAsterix(String AsterixQueryURL){
try {
URL restServiceURL = new URL(AsterixQueryURL);
HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setRequestProperty("Accept", "application/json");
if (httpConnection.getResponseCode() != 200) {
throw new RuntimeException("HTTP GET Request Failed with Error code : "
+ httpConnection.getResponseCode());
}
BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
(httpConnection.getInputStream())));
String output;
String result=null;
while ((output = responseBuffer.readLine()) != null) {
System.out.println(output);
result = result+output;
}
httpConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void buildAsterixDBInstance(){
File directory = new File("");
String a=directory.getAbsolutePath();
dataFrame df=new dataFrame();
String filename=df.writeintosh();
String command="chmod +x "+filename;
try {
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
}catch(Exception e){
e.printStackTrace();
}
try {
String shpath="./asterix-installer/target/asterix-installer-0.8.9-SNAPSHOT-binary-assembly/dataFrameStartAsterix.sh";
Process ps = Runtime.getRuntime().exec(shpath);
ps.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
String result = sb.toString();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
/*build a shell script to establish an asterixDB instance
*/
public String writeintosh(){
try {
String name = "dataFrameStartAsterix.sh";
boolean flag = false;
File directory = new File("");
String a=directory.getAbsolutePath();
String b="asterix-lang-common/src/main/java/org/apache/asterix/lang/common/dataFrame/dataFrameStartAsterix.sh";
String c=a+"/asterix-installer/target/asterix-installer-0.8.9-SNAPSHOT-binary-assembly/";
String d=c+"bin";
String path = directory.getAbsolutePath() + "/asterix-installer/target/asterix-installer-0.8.9-SNAPSHOT-binary-assembly/";
String filenameTemp = path + name;
File writename = new File(filenameTemp);
writename.createNewFile();
BufferedWriter out = new BufferedWriter(new FileWriter(writename));
String command04="export MANAGIX_HOME="+c+"\n"+"export PATH=$PATH:"+d+"\n";
out.write(command04);
out.write("./asterix-installer/target/asterix-installer-0.8.9-SNAPSHOT-binary-assembly/bin/managix configure\n");
out.write("./asterix-installer/target/asterix-installer-0.8.9-SNAPSHOT-binary-assembly/bin/managix validate\n");
out.write("./asterix-installer/target/asterix-installer-0.8.9-SNAPSHOT-binary-assembly/bin/managix stop -n my_asterix\n");
out.write("./asterix-installer/target/asterix-installer-0.8.9-SNAPSHOT-binary-assembly/bin/managix delete -n my_asterix\n");
out.write("./asterix-installer/target/asterix-installer-0.8.9-SNAPSHOT-binary-assembly/bin/managix create -n my_asterix -c "+c+"/clusters/local/local.xml");
out.flush();
out.close();
return filenameTemp;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
dataFrame df=new dataFrame();
df.buildDataverse();
String result=df.show("company","Employee");
}
}