Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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.apache.hadoop.hive.ql.metadata;
 
 import java.util.List;
 import java.util.Map;
 
 
 import  org.apache.hadoop.fs.FileSystem;
 import  org.apache.hadoop.fs.Path;
 import  org.apache.hadoop.hive.metastore.api.AlreadyExistsException;
 import  org.apache.hadoop.hive.metastore.api.Database;
 import  org.apache.hadoop.hive.metastore.api.FieldSchema;
 import  org.apache.hadoop.hive.metastore.api.MetaException;
 import  org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
 import  org.apache.hadoop.mapred.TextInputFormat;
TestHiveMetaStoreChecker.
 
 public class TestHiveMetaStoreChecker extends TestCase {
 
   private Hive hive;
   private FileSystem fs;
   private HiveMetaStoreChecker checker = null;
 
   private final String dbName = "dbname";
   private final String tableName = "tablename";
 
   private final String partDateName = "partdate";
   private final String partCityName = "partcity";
 
   private List<FieldSchema> partCols;
   private List<Map<StringString>> parts;
 
   @Override
   protected void setUp() throws Exception {
     super.setUp();
      = Hive.get();
      = new HiveMetaStoreChecker();
 
      = new ArrayList<FieldSchema>();
     .add(new FieldSchema(.""));
     .add(new FieldSchema(.""));
 
      = new ArrayList<Map<StringString>>();
     Map<StringStringpart1 = new HashMap<StringString>();
     part1.put("2008-01-01");
     part1.put("london");
     .add(part1);
     Map<StringStringpart2 = new HashMap<StringString>();
     part2.put("2008-01-02");
     part2.put("stockholm");
     .add(part2);
 
     // cleanup
     .dropTable(truetrue);
     try {
       .dropDatabase();
     } catch (NoSuchObjectException e) {
       // ignore
     }
   }
 
   @Override
   protected void tearDown() throws Exception {
     super.tearDown();
     Hive.closeCurrent();
   }
 
   public void testTableCheck() throws HiveException, MetaException,
       IOExceptionTException, AlreadyExistsException {
 
     CheckResult result = new CheckResult();
     .checkMetastore(nullnullresult);
     // we haven't added anything so should return an all ok
    // check table only, should not exist in ms
    result = new CheckResult();
    .checkMetastore(nullresult);
    assertEquals(1, result.getTablesNotInMs().size());
    Database db = new Database();
    db.setName();
    .createDatabase(db);
    Table table = new Table();
    table.setDbName();
    table.setInputFormatClass(TextInputFormat.class);
    .createTable(table);
    // now we've got a table, check that it works
    // first check all (1) tables
    result = new CheckResult();
    .checkMetastore(nullnullresult);
    // then let's check the one we know about
    result = new CheckResult();
    .checkMetastore(nullresult);
    // remove the table folder
     = table.getPath().getFileSystem(.getConf());
    .delete(table.getPath(), true);
    // now this shouldn't find the path on the fs
    result = new CheckResult();
    .checkMetastore(nullresult);
    assertEquals(1, result.getTablesNotOnFs().size());
    // put it back and one additional table
    .mkdirs(table.getPath());
    Path fakeTable = table.getPath().getParent().suffix(
        Path.SEPARATOR + "faketable");
    .mkdirs(fakeTable);
    // find the extra table
    result = new CheckResult();
    .checkMetastore(nullnullresult);
    assertEquals(1, result.getTablesNotInMs().size());
    assertEquals(fakeTable.getName(), result.getTablesNotInMs().get(0));
    // create a new external table
    table.setProperty("EXTERNAL""TRUE");
    .createTable(table);
    // should return all ok
    result = new CheckResult();
    .checkMetastore(nullnullresult);
  }
  public void testPartitionsCheck() throws HiveException, MetaException,
      IOExceptionTException, AlreadyExistsException {
    Database db = new Database();
    db.setName();
    .createDatabase(db);
    Table table = new Table();
    table.setDbName();
    table.setInputFormatClass(TextInputFormat.class);
    table.setPartCols();
    .createTable(table);
    table = .getTable();
    for (Map<StringStringpartSpec : ) {
      .createPartition(tablepartSpec);
    }
    CheckResult result = new CheckResult();
    .checkMetastore(nullresult);
    // all is well
    List<Partitionpartitions = .getPartitions(table);
    assertEquals(2, partitions.size());
    Partition partToRemove = partitions.get(0);
    Path partToRemovePath = new Path(partToRemove.getDataLocation().toString());
     = partToRemovePath.getFileSystem(.getConf());
    .delete(partToRemovePathtrue);
    result = new CheckResult();
    .checkMetastore(nullresult);
    // missing one partition on fs
    assertEquals(1, result.getPartitionsNotOnFs().size());
    assertEquals(partToRemove.getName(), result.getPartitionsNotOnFs().get(0)
        .getPartitionName());
    assertEquals(partToRemove.getTable().getTableName(), result
        .getPartitionsNotOnFs().get(0).getTableName());
    List<Map<StringString>> partsCopy = new ArrayList<Map<StringString>>();
    partsCopy.add(partitions.get(1).getSpec());
    // check only the partition that exists, all should be well
    result = new CheckResult();
    .checkMetastore(partsCopyresult);
    // put the other one back
    .mkdirs(partToRemovePath);
    // add a partition dir on fs
    Path fakePart = new Path(table.getDataLocation().toString(),
        "fakepartition=fakevalue");
    .mkdirs(fakePart);
    .checkMetastore(nullresult);
    // one extra partition
    assertEquals(1, result.getPartitionsNotInMs().size());
    assertEquals(fakePart.getName(), result.getPartitionsNotInMs().get(0)
        .getPartitionName());
  }
New to GrepCode? Check out our FAQ X