2016-10-17 6 views
1

HDP-2.5.0.0 mit Ambari 2.4.0.1Alter (leer) Tabellenpartition COLUMN

Es gibt mehrere SQL Server und Oracle Datenbank-Schema hinzufügen, die auf HDFS/Hive importiert müssen.

Der derzeitige Ansatz funktioniert gut:

  1. Sqoop Import von RDBMS HDFS in avro Format
  2. Erstellung eines Hive externen Tabelle oben auf die Avro-Dateien nämlich. dataaggregate_avro_compressed
  3. Erstellen Sie die finale Tabelle aus Schritt 2. AUTOMATE dieser Schritt
  4. einfügen Daten aus der Tabelle in Schritt 2 an den Final Table

Nun wird der Schritt 3. Tabelle muss ORC + COMPRESSED + PARTITIONED und möglicherweise MANAGED sein. Manuell kann getan werden, folgende:

CREATE TABLE `dataaggregate_orc_empty`(......)PARTITIONED BY (`datedimensionid` bigint) ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde' STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat' TBLPROPERTIES ('orc.compress'='ZLIB'); 

Aber es ist automatische Erstellung ist eine Herausforderung, ich bin mit den folgenden Ansätze zu kämpfen:

  • CTAS leere Tabelle

    CREATE TABELLE dataaggregate_orc_empty LIKE dataaggregate_avro_compressed ROW-FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde' GESPEICHERT ALS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc. OrcInputFormat 'OUTPUTFORMAT' org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat 'TBLPROPERTIES (' orc.compress '=' ZLIB ');

Nun, diese Tabelle enthält die Partitionsspalte datedimensionid, die aus der Tabelle entfernt werden muss, sondern eine ‚Tabelle Drop Spalte ändern‘ wird nicht unterstützt

  • CTAS mit REGEX column spec. :

    set hive.support.quoted.identifiers = none; CREATE TABLE datenaggregate_orc_empty AS SELECT (datedimensionid)?+.+ VON dataaggregate_avro_compressed limit 0;

Diese die Tabelle ohne die Partitionsspalte datedimensionid schafft aber jetzt wie diese leere Tabelle zu ändern, um die Trennwandkolonne aufzunehmen, ist dies, wo auch der erste Ansatz auf die Wand trifft! Die documentation talks about adding partitions mit einer Spezifikation, aber ich habe keine zu diesem Zeitpunkt - Ich möchte nur, dass diese Tabelle die gleiche manuell erstellt werden (am Anfang des Beitrags gezeigt).

Wie soll ich fortfahren?

+0

'HiveMetaStoreClient' kann Ihre Anforderung helfen. Ich gab Beispiel Ansatz auf diese Weise modifizierten Partitionen. Sie können versuchen –

Antwort

1

Dies ist eine Möglichkeit zum Verbinden HiveMetaStoreClient und Sie können Methode alter partition verwenden.

In dieser Klasse zusammen mit Spalten können alle anderen Informationen wie Partitionen extrahiert werden. pls. Siehe Beispielclient und Methoden. enter image description here

import org.apache.hadoop.hive.conf.HiveConf; 

// test program 
public class Test { 
    public static void main(String[] args){ 

     HiveConf hiveConf = new HiveConf(); 
     hiveConf.setIntVar(HiveConf.ConfVars.METASTORETHRIFTCONNECTIONRETRIES, 3); 
     hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://host:port"); 

     HiveMetaStoreConnector hiveMetaStoreConnector = new HiveMetaStoreConnector(hiveConf); 
     if(hiveMetaStoreConnector != null){ 
      System.out.print(hiveMetaStoreConnector.getAllPartitionInfo("tablename")); 
     } 
    } 
} 


// define a class like this 

import com.google.common.base.Joiner; 
import com.google.common.collect.Lists; 
import org.apache.hadoop.hive.conf.HiveConf; 
import org.apache.hadoop.hive.metastore.HiveMetaStoreClient; 
import org.apache.hadoop.hive.metastore.api.FieldSchema; 
import org.apache.hadoop.hive.metastore.api.MetaException; 
import org.apache.hadoop.hive.metastore.api.Partition; 
import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants; 
import org.apache.hadoop.hive.ql.metadata.Hive; 
import org.apache.thrift.TException; 
import org.joda.time.DateTime; 
import org.joda.time.format.DateTimeFormatter; 

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

public class HiveMetaStoreConnector { 
    private HiveConf hiveConf; 
    HiveMetaStoreClient hiveMetaStoreClient; 

    public HiveMetaStoreConnector(String msAddr, String msPort){ 
     try { 
      hiveConf = new HiveConf(); 
      hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, msAddr+":"+ msPort); 
      hiveMetaStoreClient = new HiveMetaStoreClient(hiveConf); 
     } catch (MetaException e) { 
      e.printStackTrace(); 
      System.err.println("Constructor error"); 
      System.err.println(e.toString()); 
      System.exit(-100); 
     } 
    } 

    public HiveMetaStoreConnector(HiveConf hiveConf){ 
     try { 
      this.hiveConf = hiveConf; 
      hiveMetaStoreClient = new HiveMetaStoreClient(hiveConf); 
     } catch (MetaException e) { 
      e.printStackTrace(); 
      System.err.println("Constructor error"); 
      System.err.println(e.toString()); 
      System.exit(-100); 
     } 
    } 

    public String getAllPartitionInfo(String dbName){ 
     List<String> res = Lists.newArrayList(); 
     try { 
      List<String> tableList = hiveMetaStoreClient.getAllTables(dbName); 
      for(String tableName:tableList){ 
       res.addAll(getTablePartitionInformation(dbName,tableName)); 
      } 
     } catch (MetaException e) { 
      e.printStackTrace(); 
      System.out.println("getAllTableStatistic error"); 
      System.out.println(e.toString()); 
      System.exit(-100); 
     } 

     return Joiner.on("\n").join(res); 
    } 

    public List<String> getTablePartitionInformation(String dbName, String tableName){ 
     List<String> partitionsInfo = Lists.newArrayList(); 
     try { 
      List<String> partitionNames = hiveMetaStoreClient.listPartitionNames(dbName,tableName, (short) 10000); 
      List<Partition> partitions = hiveMetaStoreClient.listPartitions(dbName,tableName, (short) 10000); 
      for(Partition partition:partitions){ 
       StringBuffer sb = new StringBuffer(); 
       sb.append(tableName); 
       sb.append("\t"); 
       List<String> partitionValues = partition.getValues(); 
       if(partitionValues.size()<4){ 
        int size = partitionValues.size(); 
        for(int j=0; j<4-size;j++){ 
         partitionValues.add("null"); 
        } 
       } 
       sb.append(Joiner.on("\t").join(partitionValues)); 
       sb.append("\t"); 
       DateTime createDate = new DateTime((long)partition.getCreateTime()*1000); 
       sb.append(createDate.toString("yyyy-MM-dd HH:mm:ss")); 
       partitionsInfo.add(sb.toString()); 
      } 

     } catch (TException e) { 
      e.printStackTrace(); 
      return Arrays.asList(new String[]{"error for request on" + tableName}); 
     } 

     return partitionsInfo; 
    } 

    public String getAllTableStatistic(String dbName){ 
     List<String> res = Lists.newArrayList(); 
     try { 
      List<String> tableList = hiveMetaStoreClient.getAllTables(dbName); 
      for(String tableName:tableList){ 
       res.addAll(getTableColumnsInformation(dbName,tableName)); 
      } 
     } catch (MetaException e) { 
      e.printStackTrace(); 
      System.out.println("getAllTableStatistic error"); 
      System.out.println(e.toString()); 
      System.exit(-100); 
     } 

     return Joiner.on("\n").join(res); 
    } 

    public List<String> getTableColumnsInformation(String dbName, String tableName){ 
     try { 
      List<FieldSchema> fields = hiveMetaStoreClient.getFields(dbName, tableName); 
      List<String> infs = Lists.newArrayList(); 
      int cnt = 0; 
      for(FieldSchema fs : fields){ 
       StringBuffer sb = new StringBuffer(); 
       sb.append(tableName); 
       sb.append("\t"); 
       sb.append(cnt); 
       sb.append("\t"); 
       cnt++; 
       sb.append(fs.getName()); 
       sb.append("\t"); 
       sb.append(fs.getType()); 
       sb.append("\t"); 
       sb.append(fs.getComment()); 
       infs.add(sb.toString()); 
      } 

      return infs; 

     } catch (TException e) { 
      e.printStackTrace(); 
      System.out.println("getTableColumnsInformation error"); 
      System.out.println(e.toString()); 
      System.exit(-100); 
      return null; 
     } 
    } 
} 
+0

Aber die Methoden in der Klasse, z. add_partition (Partition new_part) scheint mein Problem nicht zu lösen - Hinzufügen einer Partitionsspalte zu einer leeren Tabelle, d. h. keine Daten oder Werte für die Partition. –

+0

hast du das probiert? AFAIK sollte es mit leeren Tabelle sowie –

+1

dieser Klasse HiveMetaStoreClient von anderen SQL-Engines verwendet werden, um Interoperabilität mit Bienenstock Metastore-Ebene haben –

Verwandte Themen