forked from elastic/elasticsearch
/
PigSaveTest.java
181 lines (161 loc) · 8.44 KB
/
PigSaveTest.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.elasticsearch.hadoop.integration.pig;
import java.util.Date;
import org.elasticsearch.hadoop.cfg.ConfigurationOptions;
import org.elasticsearch.hadoop.integration.Provisioner;
import org.elasticsearch.hadoop.rest.RestClient;
import org.elasticsearch.hadoop.util.TestSettings;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
/**
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class PigSaveTest {
static PigWrapper pig;
@BeforeClass
public static void startup() throws Exception {
pig = new PigWrapper();
pig.start();
// initialize Pig in local mode
RestClient client = new RestClient(new TestSettings());
try {
client.deleteIndex("radio");
} catch (Exception ex) {
// ignore
}
}
@AfterClass
public static void shutdown() {
pig.stop();
}
@Test
public void testTuple() throws Exception {
String script =
"REGISTER "+ Provisioner.ESHADOOP_TESTING_JAR + ";" +
// "A = LOAD 'src/test/resources/artists.dat' USING PigStorage() AS (id:long, name:chararray, links:tuple(url:chararray, picture: chararray));" +
"A = LOAD 'src/test/resources/artists.dat' USING PigStorage() AS (id:long, name:chararray, url:chararray, picture: chararray);" +
//"ILLUSTRATE A;" +
"B = FOREACH A GENERATE name, TOTUPLE(url, picture) AS links;" +
//"ILLUSTRATE B;" +
"STORE B INTO 'pig/tupleartists' USING org.elasticsearch.hadoop.pig.ESStorage();";
//"es_total = LOAD 'radio/artists/_count?q=me*' USING org.elasticsearch.hadoop.pig.ESStorage();" +
//"DUMP es_total;" +
//"bartists = FILTER B BY name MATCHES 'me.*';" +
//"allb = GROUP bartists ALL;"+
//"total = FOREACH allb GENERATE 'total' as foo, COUNT_STAR($1) as total;"+
//"ILLUSTRATE allb;"+
//"STORE total INTO '/tmp/total';"+
//"DUMP total;";
pig.executeScript(script);
}
@Test
public void testBag() throws Exception {
String script =
"REGISTER "+ Provisioner.ESHADOOP_TESTING_JAR + ";" +
//"A = LOAD 'src/test/resources/artists.dat' USING PigStorage() AS (id:long, name, links:bag{t:(url:chararray, picture: chararray)});" +
"A = LOAD 'src/test/resources/artists.dat' USING PigStorage() AS (id:long, name:chararray, url:chararray, picture: chararray);" +
"B = FOREACH A GENERATE name, TOBAG(url, picture) AS links;" +
"ILLUSTRATE B;" +
"STORE B INTO 'pig/bagartists' USING org.elasticsearch.hadoop.pig.ESStorage();";
pig.executeScript(script);
}
@Test
public void testTimestamp() throws Exception {
long millis = new Date().getTime();
String script =
"REGISTER "+ Provisioner.ESHADOOP_TESTING_JAR + ";" +
//"A = LOAD 'src/test/resources/artists.dat' USING PigStorage() AS (id:long, name, links:bag{t:(url:chararray, picture: chararray)});" +
"A = LOAD 'src/test/resources/artists.dat' USING PigStorage() AS (id:long, name:chararray, url:chararray, picture: chararray);" +
"B = FOREACH A GENERATE name, ToDate(" + millis + "l), url;" +
"ILLUSTRATE B;" +
"STORE B INTO 'pig/timestamp' USING org.elasticsearch.hadoop.pig.ESStorage();";
pig.executeScript(script);
}
@Test
public void testFieldAlias() throws Exception {
long millis = new Date().getTime();
String script =
"REGISTER "+ Provisioner.ESHADOOP_TESTING_JAR + ";" +
//"A = LOAD 'src/test/resources/artists.dat' USING PigStorage() AS (id:long, name, links:bag{t:(url:chararray, picture: chararray)});" +
"A = LOAD 'src/test/resources/artists.dat' USING PigStorage() AS (Id:long, name:chararray, url:chararray, picture: chararray);" +
"B = FOREACH A GENERATE name, ToDate(" + millis + "l) AS timestamp, url, picture;" +
"ILLUSTRATE B;" +
"STORE B INTO 'pig/fieldalias' USING org.elasticsearch.hadoop.pig.ESStorage('es.mapping.names=nAme:@name, timestamp:@timestamp, uRL:url, picturE:picture');";
pig.executeScript(script);
}
@Test
public void testEmptyComplexStructures() throws Exception {
String script =
"REGISTER "+ Provisioner.ESHADOOP_TESTING_JAR + ";" +
"A = LOAD 'src/test/resources/artists.dat' USING PigStorage() AS (id:long, name:chararray, url:chararray, picture: chararray);" +
"AL = LIMIT A 10;" +
"B = FOREACH AL GENERATE (), [], {};" +
"STORE B INTO 'pig/emptyconst' USING org.elasticsearch.hadoop.pig.ESStorage();";
pig.executeScript(script);
}
@Test
public void testCreateWithId() throws Exception {
String script =
"REGISTER "+ Provisioner.ESHADOOP_TESTING_JAR + ";" +
"A = LOAD 'src/test/resources/artists.dat' USING PigStorage() AS (id:long, name:chararray, url:chararray, picture: chararray);" +
"B = FOREACH A GENERATE id, name, TOBAG(url, picture) AS links;" +
"STORE B INTO 'pig/createwithid' USING org.elasticsearch.hadoop.pig.ESStorage('"
+ ConfigurationOptions.ES_WRITE_OPERATION + "=create','"
+ ConfigurationOptions.ES_MAPPING_ID + "=id');";
pig.executeScript(script);
}
@Test(expected = IllegalStateException.class)
public void testCreateWithIdShouldFailOnDuplicate() throws Exception {
testCreateWithId();
}
@Test(expected = Exception.class)
public void testUpdateWithoutId() throws Exception {
String script =
"REGISTER "+ Provisioner.ESHADOOP_TESTING_JAR + ";" +
"A = LOAD 'src/test/resources/artists.dat' USING PigStorage() AS (id:long, name:chararray, url:chararray, picture: chararray);" +
"B = FOREACH A GENERATE id, name, TOBAG(url, picture) AS links;" +
"STORE B INTO 'pig/updatewoid' USING org.elasticsearch.hadoop.pig.ESStorage('"
+ ConfigurationOptions.ES_WRITE_OPERATION + "=update');";
pig.executeScript(script);
}
@Test
public void testUpdateWithId() throws Exception {
String script =
"REGISTER "+ Provisioner.ESHADOOP_TESTING_JAR + ";" +
"A = LOAD 'src/test/resources/artists.dat' USING PigStorage() AS (id:long, name:chararray, url:chararray, picture: chararray);" +
"B = FOREACH A GENERATE id, name, TOBAG(url, picture) AS links;" +
"STORE B INTO 'pig/update' USING org.elasticsearch.hadoop.pig.ESStorage('"
+ ConfigurationOptions.ES_WRITE_OPERATION + "=update','"
+ ConfigurationOptions.ES_MAPPING_ID + "=id');";
pig.executeScript(script);
}
@Test(expected = IllegalStateException.class)
public void testUpdateWithoutUpsert() throws Exception {
String script =
"REGISTER "+ Provisioner.ESHADOOP_TESTING_JAR + ";" +
"A = LOAD 'src/test/resources/artists.dat' USING PigStorage() AS (id:long, name:chararray, url:chararray, picture: chararray);" +
"B = FOREACH A GENERATE id, name, TOBAG(url, picture) AS links;" +
"STORE B INTO 'pig/updatewoupsert' USING org.elasticsearch.hadoop.pig.ESStorage('"
+ ConfigurationOptions.ES_WRITE_OPERATION + "=update','"
+ ConfigurationOptions.ES_MAPPING_ID + "=id','"
+ ConfigurationOptions.ES_UPSERT_DOC + "=false');";
pig.executeScript(script);
}
}