/
HiveQueryRunner.java
217 lines (193 loc) · 9.61 KB
/
HiveQueryRunner.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* 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 com.facebook.presto.hive;
import com.facebook.presto.Session;
import com.facebook.presto.hive.metastore.BridgingHiveMetastore;
import com.facebook.presto.hive.metastore.InMemoryHiveMetastore;
import com.facebook.presto.metadata.QualifiedObjectName;
import com.facebook.presto.testing.QueryRunner;
import com.facebook.presto.tests.DistributedQueryRunner;
import com.facebook.presto.tpch.TpchPlugin;
import com.facebook.presto.tpch.testing.SampledTpchPlugin;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.airlift.log.Logger;
import io.airlift.log.Logging;
import io.airlift.tpch.TpchTable;
import org.apache.hadoop.hive.metastore.api.Database;
import org.apache.hadoop.hive.metastore.api.PrincipalType;
import org.intellij.lang.annotations.Language;
import org.joda.time.DateTimeZone;
import java.io.File;
import java.util.Map;
import static com.facebook.presto.hive.util.Types.checkType;
import static com.facebook.presto.testing.TestingSession.testSessionBuilder;
import static com.facebook.presto.tests.QueryAssertions.copyTpchTables;
import static com.facebook.presto.tpch.TpchMetadata.TINY_SCHEMA_NAME;
import static io.airlift.units.Duration.nanosSince;
import static java.lang.String.format;
import static java.util.Locale.ENGLISH;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.testng.Assert.assertEquals;
public final class HiveQueryRunner
{
private static final Logger log = Logger.get(HiveQueryRunner.class);
private HiveQueryRunner()
{
}
public static final String HIVE_CATALOG = "hive";
public static final String HIVE_BUCKETED_CATALOG = "hive_bucketed";
public static final String TPCH_SCHEMA = "tpch";
private static final String TPCH_SAMPLED_SCHEMA = "tpch_sampled";
private static final String TPCH_BUCKETED_SCHEMA = "tpch_bucketed";
private static final DateTimeZone TIME_ZONE = DateTimeZone.forID("Asia/Kathmandu");
public static DistributedQueryRunner createQueryRunner(TpchTable<?>... tables)
throws Exception
{
return createQueryRunner(ImmutableList.copyOf(tables));
}
public static DistributedQueryRunner createQueryRunner(Iterable<TpchTable<?>> tables)
throws Exception
{
return createQueryRunner(tables, ImmutableMap.of());
}
public static DistributedQueryRunner createQueryRunner(Iterable<TpchTable<?>> tables, Map<String, String> extraProperties)
throws Exception
{
return createQueryRunner(tables, extraProperties, "sql-standard", ImmutableMap.of());
}
public static DistributedQueryRunner createQueryRunner(Iterable<TpchTable<?>> tables, Map<String, String> extraProperties, String security, Map<String, String> extraHiveProperties)
throws Exception
{
assertEquals(DateTimeZone.getDefault(), TIME_ZONE, "Timezone not configured correctly. Add -Duser.timezone=Asia/Katmandu to your JVM arguments");
DistributedQueryRunner queryRunner = new DistributedQueryRunner(createSession(), 4, extraProperties);
try {
queryRunner.installPlugin(new TpchPlugin());
queryRunner.createCatalog("tpch", "tpch");
queryRunner.installPlugin(new SampledTpchPlugin());
queryRunner.createCatalog("tpch_sampled", "tpch_sampled");
File baseDir = queryRunner.getCoordinator().getBaseDataDir().resolve("hive_data").toFile();
InMemoryHiveMetastore metastore = new InMemoryHiveMetastore(baseDir);
metastore.createDatabase(createDatabaseMetastoreObject(baseDir, TPCH_SCHEMA));
metastore.createDatabase(createDatabaseMetastoreObject(baseDir, TPCH_SAMPLED_SCHEMA));
metastore.createDatabase(createDatabaseMetastoreObject(baseDir, TPCH_BUCKETED_SCHEMA));
queryRunner.installPlugin(new HivePlugin(HIVE_CATALOG, new BridgingHiveMetastore(metastore)));
Map<String, String> hiveProperties = ImmutableMap.<String, String>builder()
.putAll(extraHiveProperties)
.put("hive.metastore.uri", "thrift://localhost:8080")
.put("hive.time-zone", TIME_ZONE.getID())
.put("hive.security", security)
.build();
Map<String, String> hiveBucketedProperties = ImmutableMap.<String, String>builder()
.putAll(hiveProperties)
.put("hive.max-initial-split-size", "10kB") // so that each bucket has multiple splits
.put("hive.max-split-size", "10kB") // so that each bucket has multiple splits
.put("hive.storage-format", "TEXTFILE") // so that there's no minimum split size for the file
.put("hive.compression-codec", "NONE") // so that the file is splittable
.build();
queryRunner.createCatalog(HIVE_CATALOG, HIVE_CATALOG, hiveProperties);
queryRunner.createCatalog(HIVE_BUCKETED_CATALOG, HIVE_CATALOG, hiveBucketedProperties);
copyTpchTables(queryRunner, "tpch", TINY_SCHEMA_NAME, createSession(), tables);
copyTpchTables(queryRunner, "tpch_sampled", TINY_SCHEMA_NAME, createSampledSession(), tables);
copyTpchTablesBucketed(queryRunner, "tpch", TINY_SCHEMA_NAME, createBucketedSession(), tables);
return queryRunner;
}
catch (Exception e) {
queryRunner.close();
throw e;
}
}
private static Database createDatabaseMetastoreObject(File baseDir, String name)
{
Database database = new Database(name, null, new File(baseDir, name).toURI().toString(), null);
database.setOwnerName("public");
database.setOwnerType(PrincipalType.ROLE);
return database;
}
public static Session createSession()
{
return testSessionBuilder()
.setCatalog(HIVE_CATALOG)
.setSchema(TPCH_SCHEMA)
.build();
}
public static Session createSampledSession()
{
return testSessionBuilder()
.setCatalog(HIVE_CATALOG)
.setSchema(TPCH_SAMPLED_SCHEMA)
.build();
}
public static Session createBucketedSession()
{
return testSessionBuilder()
.setCatalog(HIVE_BUCKETED_CATALOG)
.setSchema(TPCH_BUCKETED_SCHEMA)
.build();
}
public static void copyTpchTablesBucketed(
QueryRunner queryRunner,
String sourceCatalog,
String sourceSchema,
Session session,
Iterable<TpchTable<?>> tables)
throws Exception
{
log.info("Loading data from %s.%s...", sourceCatalog, sourceSchema);
long startTime = System.nanoTime();
for (TpchTable<?> table : tables) {
copyTableBucketed(queryRunner, new QualifiedObjectName(sourceCatalog, sourceSchema, table.getTableName().toLowerCase(ENGLISH)), session);
}
log.info("Loading from %s.%s complete in %s", sourceCatalog, sourceSchema, nanosSince(startTime).toString(SECONDS));
}
private static void copyTableBucketed(QueryRunner queryRunner, QualifiedObjectName table, Session session)
{
long start = System.nanoTime();
log.info("Running import for %s", table.getObjectName());
@Language("SQL") String sql;
switch (table.getObjectName()) {
case "part":
case "partsupp":
case "supplier":
case "nation":
case "region":
sql = format("CREATE TABLE %s AS SELECT * FROM %s", table.getObjectName(), table);
break;
case "lineitem":
sql = format("CREATE TABLE %s WITH (bucketed_by=array['orderkey'], bucket_count=11) AS SELECT * FROM %s", table.getObjectName(), table);
break;
case "customer":
sql = format("CREATE TABLE %s WITH (bucketed_by=array['custkey'], bucket_count=11) AS SELECT * FROM %s", table.getObjectName(), table);
break;
case "orders":
sql = format("CREATE TABLE %s WITH (bucketed_by=array['custkey'], bucket_count=11) AS SELECT * FROM %s", table.getObjectName(), table);
break;
default:
throw new UnsupportedOperationException();
}
long rows = checkType(queryRunner.execute(session, sql).getMaterializedRows().get(0).getField(0), Long.class, "rows");
log.info("Imported %s rows for %s in %s", rows, table.getObjectName(), nanosSince(start).convertToMostSuccinctTimeUnit());
}
public static void main(String[] args)
throws Exception
{
// You need to add "--user user" to your CLI for your queries to work
Logging.initialize();
DistributedQueryRunner queryRunner = createQueryRunner(TpchTable.getTables(), ImmutableMap.of("http-server.http.port", "8080"));
Thread.sleep(10);
Logger log = Logger.get(DistributedQueryRunner.class);
log.info("======== SERVER STARTED ========");
log.info("\n====\n%s\n====", queryRunner.getCoordinator().getBaseUrl());
}
}