Search in sources :

Example 46 with HiveMetaException

use of org.apache.hadoop.hive.metastore.HiveMetaException in project hive by apache.

the class TestSchemaToolCatalogOps method moveTableToNonExistentDb.

@Test
public void moveTableToNonExistentDb() throws TException, HiveMetaException {
    String tableName = "doomedToWander";
    new TableBuilder().setTableName(tableName).addCol("a", "int").create(client, conf);
    try {
        String argsMoveTable = String.format("-moveTable %s -fromCatalog %s -toCatalog %s -fromDatabase %s -toDatabase nosuch", tableName, DEFAULT_CATALOG_NAME, DEFAULT_CATALOG_NAME, DEFAULT_DATABASE_NAME);
        execute(new SchemaToolTaskMoveTable(), argsMoveTable);
        Assert.fail("Attempt to move table to non-existent table should have failed.");
    } catch (HiveMetaException e) {
    // good
    }
    // Make sure nothing really moved
    Set<String> tableNames = new HashSet<>(client.getAllTables(DEFAULT_CATALOG_NAME, DEFAULT_DATABASE_NAME));
    Assert.assertTrue(tableNames.contains(tableName.toLowerCase()));
}
Also used : HiveMetaException(org.apache.hadoop.hive.metastore.HiveMetaException) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 47 with HiveMetaException

use of org.apache.hadoop.hive.metastore.HiveMetaException in project hive by apache.

the class TestSchemaToolCatalogOps method execute.

private static void execute(SchemaToolTask task, String taskArgs) throws HiveMetaException {
    try {
        StrTokenizer tokenizer = new StrTokenizer(argsBase + taskArgs, ' ', '\"');
        SchemaToolCommandLine cl = new SchemaToolCommandLine(tokenizer.getTokenArray(), null);
        task.setCommandLineArguments(cl);
    } catch (Exception e) {
        throw new IllegalStateException("Could not parse comman line \n" + argsBase + taskArgs, e);
    }
    task.setHiveSchemaTool(schemaTool);
    task.execute();
}
Also used : StrTokenizer(org.apache.commons.lang3.text.StrTokenizer) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) HiveMetaException(org.apache.hadoop.hive.metastore.HiveMetaException) TException(org.apache.thrift.TException) IOException(java.io.IOException)

Example 48 with HiveMetaException

use of org.apache.hadoop.hive.metastore.HiveMetaException in project hive by apache.

the class TestSchemaToolCatalogOps method moveTableWithExistingTableOfSameNameAlreadyInTargetDatabase.

@Test
public void moveTableWithExistingTableOfSameNameAlreadyInTargetDatabase() throws TException, HiveMetaException {
    String toDbName = "clobberTableDb";
    String tableName = "clobberTableTable";
    Database toDb = new DatabaseBuilder().setName(toDbName).create(client, conf);
    new TableBuilder().setTableName(tableName).addCol("a", "int").create(client, conf);
    new TableBuilder().inDb(toDb).setTableName(tableName).addCol("b", "varchar(32)").create(client, conf);
    try {
        String argsMoveTable = String.format("-moveTable %s -fromCatalog %s -toCatalog %s -fromDatabase %s -toDatabase %s", tableName, DEFAULT_CATALOG_NAME, DEFAULT_CATALOG_NAME, DEFAULT_DATABASE_NAME, toDbName);
        execute(new SchemaToolTaskMoveTable(), argsMoveTable);
        Assert.fail("Attempt to move table should have failed.");
    } catch (HiveMetaException e) {
    // good
    }
    // Make sure nothing really moved
    Set<String> tableNames = new HashSet<>(client.getAllTables(DEFAULT_CATALOG_NAME, DEFAULT_DATABASE_NAME));
    Assert.assertTrue(tableNames.contains(tableName.toLowerCase()));
    // Make sure the table in the target database didn't get clobbered
    Table fetchedTable = client.getTable(DEFAULT_CATALOG_NAME, toDbName, tableName);
    Assert.assertEquals("b", fetchedTable.getSd().getCols().get(0).getName());
}
Also used : DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) Table(org.apache.hadoop.hive.metastore.api.Table) Database(org.apache.hadoop.hive.metastore.api.Database) HiveMetaException(org.apache.hadoop.hive.metastore.HiveMetaException) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 49 with HiveMetaException

use of org.apache.hadoop.hive.metastore.HiveMetaException in project hive by apache.

the class TestSchemaToolForMetastore method execute.

private void execute(SchemaToolTask task, String taskArgs) throws HiveMetaException {
    try {
        StrTokenizer tokenizer = new StrTokenizer(argsBase + taskArgs, ' ', '\"');
        SchemaToolCommandLine cl = new SchemaToolCommandLine(tokenizer.getTokenArray(), null);
        task.setCommandLineArguments(cl);
    } catch (Exception e) {
        throw new IllegalStateException("Could not parse comman line \n" + argsBase + taskArgs, e);
    }
    task.setHiveSchemaTool(schemaTool);
    task.execute();
}
Also used : StrTokenizer(org.apache.commons.lang3.text.StrTokenizer) HiveMetaException(org.apache.hadoop.hive.metastore.HiveMetaException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 50 with HiveMetaException

use of org.apache.hadoop.hive.metastore.HiveMetaException in project hive by apache.

the class MetastoreSchemaTool method init.

@VisibleForTesting
public void init(String metastoreHome, String[] args, OptionGroup additionalOptions, Configuration conf) throws HiveMetaException {
    try {
        cmdLine = new SchemaToolCommandLine(args, additionalOptions);
    } catch (ParseException e) {
        System.err.println("Failed to parse command line. ");
        throw new HiveMetaException(e);
    }
    if (metastoreHome == null || metastoreHome.isEmpty()) {
        throw new HiveMetaException("No Metastore home directory provided");
    }
    this.conf = conf;
    this.dbType = cmdLine.getDbType();
    this.metaDbType = cmdLine.getMetaDbType();
    NestedScriptParser parser = getDbCommandParser(dbType, metaDbType);
    this.needsQuotedIdentifier = parser.needsQuotedIdentifier();
    this.quoteCharacter = parser.getQuoteCharacter();
    this.metaStoreSchemaInfo = MetaStoreSchemaInfoFactory.get(conf, metastoreHome, dbType);
    // It is overridden by command line options if passed (-url and -driver)
    if (dbType.equalsIgnoreCase(HiveSchemaHelper.DB_HIVE)) {
        this.url = HiveSchemaHelper.EMBEDDED_HS2_URL;
        this.driver = HiveSchemaHelper.HIVE_JDBC_DRIVER;
    }
    if (cmdLine.hasOption("userName")) {
        setUserName(cmdLine.getOptionValue("userName"));
    } else {
        setUserName(getConf().get(MetastoreConf.ConfVars.CONNECTION_USER_NAME.getVarname()));
    }
    if (cmdLine.hasOption("passWord")) {
        setPassWord(cmdLine.getOptionValue("passWord"));
    } else {
        try {
            setPassWord(MetastoreConf.getPassword(getConf(), ConfVars.PWD));
        } catch (IOException err) {
            throw new HiveMetaException("Error getting metastore password", err);
        }
    }
    if (cmdLine.hasOption("url")) {
        setUrl(cmdLine.getOptionValue("url"));
    }
    if (cmdLine.hasOption("driver")) {
        setDriver(cmdLine.getOptionValue("driver"));
    }
    if (cmdLine.hasOption("dryRun")) {
        setDryRun(true);
    }
    if (cmdLine.hasOption("verbose")) {
        setVerbose(true);
    }
    if (cmdLine.hasOption("dbOpts")) {
        setDbOpts(cmdLine.getOptionValue("dbOpts"));
    }
    if (cmdLine.hasOption("validate") && cmdLine.hasOption("servers")) {
        setValidationServers(cmdLine.getOptionValue("servers"));
    }
    if (cmdLine.hasOption("hiveUser")) {
        setHiveUser(cmdLine.getOptionValue("hiveUser"));
    }
    if (cmdLine.hasOption("hivePassword")) {
        setHivePasswd(cmdLine.getOptionValue("hivePassword"));
    }
    if (cmdLine.hasOption("hiveDb")) {
        setHiveDb(cmdLine.getOptionValue("hiveDb"));
    }
}
Also used : HiveMetaException(org.apache.hadoop.hive.metastore.HiveMetaException) ParseException(org.apache.commons.cli.ParseException) IOException(java.io.IOException) NestedScriptParser(org.apache.hadoop.hive.metastore.tools.schematool.HiveSchemaHelper.NestedScriptParser) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

HiveMetaException (org.apache.hadoop.hive.metastore.HiveMetaException)62 SQLException (java.sql.SQLException)39 IOException (java.io.IOException)28 ResultSet (java.sql.ResultSet)25 Statement (java.sql.Statement)24 PreparedStatement (java.sql.PreparedStatement)18 Connection (java.sql.Connection)13 File (java.io.File)12 Test (org.junit.Test)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)5 ParseException (org.apache.commons.cli.ParseException)5 ArrayList (java.util.ArrayList)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 OutputStream (java.io.OutputStream)3 PrintStream (java.io.PrintStream)3 DatabaseMetaData (java.sql.DatabaseMetaData)3 HashSet (java.util.HashSet)3 Pair (org.apache.commons.lang3.tuple.Pair)3 MetastoreCheckinTest (org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)3