Java Code Examples for org.apache.hadoop.hive.ql.session.SessionState#start()

The following examples show how to use org.apache.hadoop.hive.ql.session.SessionState#start() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SentryConfigTool.java    From incubator-sentry with Apache License 2.0 7 votes vote down vote up
public void verifyLocalQuery(String queryStr) throws Exception {
  // setup Hive driver
  SessionState session = new SessionState(getHiveConf());
  SessionState.start(session);
  Driver driver = new Driver(session.getConf(), getUser());

  // compile the query
  CommandProcessorResponse compilerStatus = driver
      .compileAndRespond(queryStr);
  if (compilerStatus.getResponseCode() != 0) {
    String errMsg = compilerStatus.getErrorMessage();
    if (errMsg.contains(HiveAuthzConf.HIVE_SENTRY_PRIVILEGE_ERROR_MESSAGE)) {
      printMissingPerms(getHiveConf().get(
          HiveAuthzConf.HIVE_SENTRY_AUTH_ERRORS));
    }
    throw new SemanticException("Compilation error: "
        + compilerStatus.getErrorMessage());
  }
  driver.close();
  System.out
      .println("User " + getUser() + " has privileges to run the query");
}
 
Example 2
Source File: HiveHookIT.java    From atlas with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void setUp() throws Exception {
    //  initialize 'driverWithNoHook' with HiveServer2 hook and HiveMetastore hook disabled
    HiveConf conf = new HiveConf();
    conf.set("hive.exec.post.hooks", "");
    conf.set("hive.metastore.event.listeners", "");

    SessionState ss = new SessionState(conf);
    ss = SessionState.start(ss);
    SessionState.setCurrentSessionState(ss);

    // Initialize 'driverWithNoHook'  with HS2 hook disabled and HMS hook disabled.
    driverWithNoHook = new Driver(conf);

    super.setUp();
}
 
Example 3
Source File: ITSqlStdBasedAuthorization.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private static void generateHiveTestData() throws Exception {
  final SessionState ss = new SessionState(hiveConf);
  SessionState.start(ss);
  final Driver driver = new Driver(hiveConf);

  executeQuery(driver, "CREATE DATABASE " + db_general);
  createTbl(driver, db_general, g_student_user0, studentDef, studentData);
  createTbl(driver, db_general, g_voter_role0, voterDef, voterData);
  createTbl(driver, db_general, g_student_user2, studentDef, studentData);

  executeQuery(driver, "SET ROLE admin");
  executeQuery(driver, "CREATE ROLE " + test_role0);
  executeQuery(driver, "GRANT ROLE " + test_role0 + " TO USER " + org1Users[1]);
  executeQuery(driver, "GRANT ROLE " + test_role0 + " TO USER " + org1Users[2]);

  executeQuery(driver, String.format("GRANT SELECT ON %s.%s TO USER %s", db_general, g_student_user0, org1Users[0]));
  executeQuery(driver, String.format("GRANT SELECT ON %s.%s TO ROLE %s", db_general, g_voter_role0, test_role0));
  executeQuery(driver, String.format("GRANT SELECT ON %s.%s TO USER %s", db_general, g_student_user2, org1Users[2]));
}
 
Example 4
Source File: ITSqlStdBasedAuthorization.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private static void generateHiveTestData() throws Exception {
  final SessionState ss = new SessionState(hiveConf);
  SessionState.start(ss);
  final Driver driver = new Driver(hiveConf);

  executeQuery(driver, "CREATE DATABASE " + db_general);
  createTbl(driver, db_general, g_student_user0, studentDef, studentData);
  createTbl(driver, db_general, g_voter_role0, voterDef, voterData);
  createTbl(driver, db_general, g_student_user2, studentDef, studentData);

  executeQuery(driver, "SET ROLE admin");
  executeQuery(driver, "CREATE ROLE " + test_role0);
  executeQuery(driver, "GRANT ROLE " + test_role0 + " TO USER " + org1Users[1]);
  executeQuery(driver, "GRANT ROLE " + test_role0 + " TO USER " + org1Users[2]);

  executeQuery(driver, String.format("GRANT SELECT ON %s.%s TO USER %s", db_general, g_student_user0, org1Users[0]));
  executeQuery(driver, String.format("GRANT SELECT ON %s.%s TO ROLE %s", db_general, g_voter_role0, test_role0));
  executeQuery(driver, String.format("GRANT SELECT ON %s.%s TO USER %s", db_general, g_student_user2, org1Users[2]));
}
 
Example 5
Source File: TestSentryHiveAuthorizationTaskFactory.java    From incubator-sentry with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws Exception {
  conf = new HiveConf();
  baseDir = Files.createTempDir();
  baseDir.setWritable(true, false);
  conf.setVar(HiveConf.ConfVars.SCRATCHDIR, baseDir.getAbsolutePath());
  SessionState.start(conf);
  conf.setVar(ConfVars.HIVE_AUTHORIZATION_TASK_FACTORY,
      SentryHiveAuthorizationTaskFactoryImpl.class.getName());

  db = Mockito.mock(Hive.class);
  table = new Table(DB, TABLE);
  partition = new Partition(table);
  context = new Context(conf);
  parseDriver = new ParseDriver();
  analyzer = new DDLSemanticAnalyzer(conf, db);
  SessionState.start(conf);
  Mockito.when(db.getTable(TABLE, false)).thenReturn(table);
  Mockito.when(db.getPartition(table, new HashMap<String, String>(), false))
  .thenReturn(partition);

  HadoopDefaultAuthenticator auth = new HadoopDefaultAuthenticator();
  auth.setConf(conf);
  currentUser = auth.getUserName();

}
 
Example 6
Source File: HiveTestDataGenerator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private void runDDL(String query, HiveConf conf) throws IOException {
  final SessionState ss = new SessionState(conf);
  try {
    SessionState.start(ss);
    final Driver hiveDriver = getHiveDriver(conf);
    executeQuery(hiveDriver, query);
  } finally {
    ss.close();
  }
}
 
Example 7
Source File: HiveTestDataGenerator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private void runDDL(String query, HiveConf conf) throws IOException {
  final SessionState ss = new SessionState(conf);
  try {
    SessionState.start(ss);
    final Driver hiveDriver = getHiveDriver(conf);
    executeQuery(hiveDriver, query);
  } finally {
    ss.close();
  }
}
 
Example 8
Source File: HiveExec.java    From streamx with Apache License 2.0 5 votes vote down vote up
/**
 * HiveExec constructor
 * @param config HDFS Connector configuration
 */
public HiveExec(HdfsSinkConnectorConfig config) {
  hiveConf = new HiveConf();
  String hiveConfDir = config.getString(HdfsSinkConnectorConfig.HIVE_CONF_DIR_CONFIG);
  hiveConf.addResource(new Path(hiveConfDir, "hive-site.xml"));
  SessionState.start(new CliSessionState(hiveConf));
  cliDriver = new CliDriver();
}
 
Example 9
Source File: EmbeddedMetastoreService.java    From beam with Apache License 2.0 5 votes vote down vote up
public EmbeddedMetastoreService(String baseDirPath) throws IOException {
  FileUtils.forceDeleteOnExit(new File(baseDirPath));

  String hiveDirPath = makePathASafeFileName(baseDirPath + "/hive");
  String testDataDirPath =
      makePathASafeFileName(
          hiveDirPath
              + "/data/"
              + EmbeddedMetastoreService.class.getCanonicalName()
              + System.currentTimeMillis());
  String testWarehouseDirPath = makePathASafeFileName(testDataDirPath + "/warehouse");

  hiveConf = new HiveConf(getClass());
  hiveConf.setVar(HiveConf.ConfVars.PREEXECHOOKS, "");
  hiveConf.setVar(HiveConf.ConfVars.POSTEXECHOOKS, "");
  hiveConf.setBoolVar(HiveConf.ConfVars.HIVE_SUPPORT_CONCURRENCY, false);
  hiveConf.setVar(HiveConf.ConfVars.METASTOREWAREHOUSE, testWarehouseDirPath);
  hiveConf.setBoolVar(HiveConf.ConfVars.HIVEOPTIMIZEMETADATAQUERIES, true);
  hiveConf.setVar(
      HiveConf.ConfVars.HIVE_AUTHORIZATION_MANAGER,
      "org.apache.hadoop.hive.ql.security.authorization.plugin.sqlstd."
          + "SQLStdHiveAuthorizerFactory");
  hiveConf.set("test.tmp.dir", hiveDirPath);

  System.setProperty("derby.stream.error.file", "/dev/null");
  driver = new Driver(hiveConf);
  sessionState = SessionState.start(new SessionState(hiveConf));
}
 
Example 10
Source File: HiveLiteralRewriterTest.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
@BeforeClass(enabled = false)
public void setup() {
    conf = new HiveConf();
    conf.addResource("/hive-site.xml");
    SessionState ss = new SessionState(conf, "testuser");
    SessionState.start(ss);
    conf.set("hive.lock.manager", "org.apache.hadoop.hive.ql.lockmgr.EmbeddedLockManager");
}
 
Example 11
Source File: TestURI.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setupTestURI() {
  conf = new HiveConf();
  baseDir = Files.createTempDir();
  baseDir.setWritable(true, false);
  conf.setVar(HiveConf.ConfVars.SCRATCHDIR, baseDir.getAbsolutePath());
  SessionState.start(conf);
}
 
Example 12
Source File: ITStorageBasedHiveAuthorization.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private static void generateTestHiveData() throws Exception {
  // Generate Hive test tables
  final SessionState ss = new SessionState(hiveConf);
  SessionState.start(ss);
  final Driver driver = new Driver(hiveConf);

  executeQuery(driver, "CREATE DATABASE " + db_general);

  createTable(driver,
      db_general, g_student_u0_700, studentDef, studentData, org1Users[0], org1Groups[0], (short) 0700);
  createTable(driver,
      db_general, g_student_u0g0_750, studentDef, studentData, org1Users[0], org1Groups[0], (short) 0750);
  createTable(driver,
      db_general, g_student_all_755, studentDef, studentData, org1Users[2], org1Groups[2], (short) 0755);
  createTable(driver,
      db_general, g_voter_u1_700, voterDef, voterData, org1Users[1], org1Groups[1], (short) 0700);
  createTable(driver,
      db_general, g_voter_u2g1_750, voterDef, voterData, org1Users[2], org1Groups[1], (short) 0750);
  createTable(driver,
      db_general, g_voter_all_755, voterDef, voterData, org1Users[1], org1Groups[1], (short) 0755);

  createPartitionedTable(driver,
      db_general, g_partitioned_student_u0_700, partitionStudentDef,
      "INSERT OVERWRITE TABLE %s.%s PARTITION(age) SELECT rownum, name, gpa, studentnum, age FROM %s.%s",
      g_student_all_755, org1Users[0], org1Groups[0], (short) 0700);

  changeDBPermissions(db_general, (short) 0755, org1Users[0], org1Groups[0]);

  executeQuery(driver, "CREATE DATABASE " + db_u1g1_only);

  createTable(driver,
      db_u1g1_only, u1g1_student_all_755, studentDef, studentData, org1Users[1], org1Groups[1], (short) 0755);
  createTable(driver,
      db_u1g1_only, u1g1_student_u1_700, studentDef, studentData, org1Users[1], org1Groups[1], (short) 0700);
  createTable(driver,
      db_u1g1_only, u1g1_voter_all_755, voterDef, voterData, org1Users[1], org1Groups[1], (short) 0755);
  createTable(driver,
      db_u1g1_only, u1g1_voter_u1_700, voterDef, voterData, org1Users[1], org1Groups[1], (short) 0700);

  changeDBPermissions(db_u1g1_only, (short) 0750, org1Users[1], org1Groups[1]);

  executeQuery(driver, "CREATE DATABASE " + db_u0_only);

  createTable(driver, db_u0_only, u0_student_all_755, studentDef, studentData, org1Users[0], org1Groups[0], (short) 0755);
  createTable(driver, db_u0_only, u0_voter_all_755, voterDef, voterData, org1Users[0], org1Groups[0], (short) 0755);

  changeDBPermissions(db_u0_only, (short) 0700, org1Users[0], org1Groups[0]);
}
 
Example 13
Source File: ITStorageBasedHiveAuthorization.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private static void generateTestHiveData() throws Exception {
  // Generate Hive test tables
  final SessionState ss = new SessionState(hiveConf);
  SessionState.start(ss);
  final Driver driver = new Driver(hiveConf);

  executeQuery(driver, "CREATE DATABASE " + db_general);

  createTable(driver,
      db_general, g_student_u0_700, studentDef, studentData, org1Users[0], org1Groups[0], (short) 0700);
  createTable(driver,
      db_general, g_student_u0g0_750, studentDef, studentData, org1Users[0], org1Groups[0], (short) 0750);
  createTable(driver,
      db_general, g_student_all_755, studentDef, studentData, org1Users[2], org1Groups[2], (short) 0755);
  createTable(driver,
      db_general, g_voter_u1_700, voterDef, voterData, org1Users[1], org1Groups[1], (short) 0700);
  createTable(driver,
      db_general, g_voter_u2g1_750, voterDef, voterData, org1Users[2], org1Groups[1], (short) 0750);
  createTable(driver,
      db_general, g_voter_all_755, voterDef, voterData, org1Users[1], org1Groups[1], (short) 0755);

  createPartitionedTable(driver,
      db_general, g_partitioned_student_u0_700, partitionStudentDef,
      "INSERT OVERWRITE TABLE %s.%s PARTITION(age) SELECT rownum, name, gpa, studentnum, age FROM %s.%s",
      g_student_all_755, org1Users[0], org1Groups[0], (short) 0700);

  changeDBPermissions(db_general, (short) 0755, org1Users[0], org1Groups[0]);

  executeQuery(driver, "CREATE DATABASE " + db_u1g1_only);

  createTable(driver,
      db_u1g1_only, u1g1_student_all_755, studentDef, studentData, org1Users[1], org1Groups[1], (short) 0755);
  createTable(driver,
      db_u1g1_only, u1g1_student_u1_700, studentDef, studentData, org1Users[1], org1Groups[1], (short) 0700);
  createTable(driver,
      db_u1g1_only, u1g1_voter_all_755, voterDef, voterData, org1Users[1], org1Groups[1], (short) 0755);
  createTable(driver,
      db_u1g1_only, u1g1_voter_u1_700, voterDef, voterData, org1Users[1], org1Groups[1], (short) 0700);

  changeDBPermissions(db_u1g1_only, (short) 0750, org1Users[1], org1Groups[1]);

  executeQuery(driver, "CREATE DATABASE " + db_u0_only);

  createTable(driver, db_u0_only, u0_student_all_755, studentDef, studentData, org1Users[0], org1Groups[0], (short) 0755);
  createTable(driver, db_u0_only, u0_voter_all_755, voterDef, voterData, org1Users[0], org1Groups[0], (short) 0755);

  changeDBPermissions(db_u0_only, (short) 0700, org1Users[0], org1Groups[0]);
}