将不同的 XML 文件动态加载到 Hive 表
Dynamically load different XML files to Hive tables
我有 folder/stream 个不同的复杂 XML 文件(每个大小约为 1GB)。我知道如何将 XML 文件数据加载到 Hive table(或任何 Hadoop 数据库)。
但我想知道两件事:
- 我可以将每个 xml 文件数据加载到配置单元 动态 ,即不明确编写 create table 命令(因为我得到不同的 XML 文件作为流),有什么方法可以自动执行此操作。
"Stream of different complex xml Files --> Load to Hive tables (with out manually writing Create table command) --> Use the data which is loaded into Hive tables"
- 我如何编写 java 代码来将 xml 数据加载到 Hive table,而不是编写命令行脚本来创建配置单元 table。
关于你的第一个问题,AFAIK,这是不可能的。 Hive 旨在管理存储在 内 Hive table 秒内的数据(它并不总是存储在 table 内,但元数据被添加到 tables,指向真实数据,Hive外部就是这种情况tables).
我认为您唯一可以尝试的是为 XML 文件中的所有数据创建一个大的 table,已经存储的和未来的;诀窍是将所有 XML 文件放在一个公共 HDFS 文件夹下,该文件夹用作 create table
命令的位置。
关于你的第二个问题,请参考这段代码:
public final class HiveBasicClient {
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
private static Connection con;
private static Connection getConnection(String hiveServer, String hivePort, String hadoopUser, String hadoopPassword) {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
return null;
}
try {
return DriverManager.getConnection("jdbc:hive://" + hiveServer + ":" + hivePort + "/default?user=" + hadoopUser + "&password=" + hadoopPassword);
} catch (SQLException e) {
return null;
}
}
private static res doQuery(String query) {
try {
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery(query);
res.close();
stmt.close();
return res;
} catch (SQLException ex) {
System.exit(0);
}
}
public static void main(String[] args) {
String hiveServer = args[0];
String hivePort = args[1];
String hadoopUser = args[2];
String hadoopPassword = args[3];
con = getConnection(hiveServer, hivePort, hadoopUser, hadoopPassword);
doQuery("create external table <table_name> (<list_of_columns>) row format serde '<your_xml_serde>' location `<your_xml_files_location>');
}
}
希望对您有所帮助。
我有 folder/stream 个不同的复杂 XML 文件(每个大小约为 1GB)。我知道如何将 XML 文件数据加载到 Hive table(或任何 Hadoop 数据库)。
但我想知道两件事:
- 我可以将每个 xml 文件数据加载到配置单元 动态 ,即不明确编写 create table 命令(因为我得到不同的 XML 文件作为流),有什么方法可以自动执行此操作。
"Stream of different complex xml Files --> Load to Hive tables (with out manually writing Create table command) --> Use the data which is loaded into Hive tables"
- 我如何编写 java 代码来将 xml 数据加载到 Hive table,而不是编写命令行脚本来创建配置单元 table。
关于你的第一个问题,AFAIK,这是不可能的。 Hive 旨在管理存储在 内 Hive table 秒内的数据(它并不总是存储在 table 内,但元数据被添加到 tables,指向真实数据,Hive外部就是这种情况tables).
我认为您唯一可以尝试的是为 XML 文件中的所有数据创建一个大的 table,已经存储的和未来的;诀窍是将所有 XML 文件放在一个公共 HDFS 文件夹下,该文件夹用作 create table
命令的位置。
关于你的第二个问题,请参考这段代码:
public final class HiveBasicClient {
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
private static Connection con;
private static Connection getConnection(String hiveServer, String hivePort, String hadoopUser, String hadoopPassword) {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
return null;
}
try {
return DriverManager.getConnection("jdbc:hive://" + hiveServer + ":" + hivePort + "/default?user=" + hadoopUser + "&password=" + hadoopPassword);
} catch (SQLException e) {
return null;
}
}
private static res doQuery(String query) {
try {
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery(query);
res.close();
stmt.close();
return res;
} catch (SQLException ex) {
System.exit(0);
}
}
public static void main(String[] args) {
String hiveServer = args[0];
String hivePort = args[1];
String hadoopUser = args[2];
String hadoopPassword = args[3];
con = getConnection(hiveServer, hivePort, hadoopUser, hadoopPassword);
doQuery("create external table <table_name> (<list_of_columns>) row format serde '<your_xml_serde>' location `<your_xml_files_location>');
}
}
希望对您有所帮助。