Hive-仅在第一个表为空时创建表

时间:2015-07-06 06:10:56

标签: hive hiveql

我需要在import java.awt.BorderLayout; import java.awt.Component; import java.awt.EventQueue; import java.awt.Point; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; public class Test { public static void main(String[] args) { new Test(); } public Test() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { public TestPane() { setLayout(new BorderLayout()); DefaultTableModel model = new DefaultTableModel(0, 10); for (int row = 0; row < 10; row++) { Object[] data = new Object[10]; for (int col = 0; col < 10; col++) { data[col] = row + "x" + col; } model.addRow(data); } JTable table = new JTable(model) { @Override public Point getToolTipLocation(MouseEvent event) { return new Point(10, 10); } }; table.setDefaultRenderer(Object.class, new TestCellRenderer()); add(new JScrollPane(table)); } public class TestCellRenderer extends DefaultTableCellRenderer { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { setToolTipText("Banana @ " + value.toString()); return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); } } } } 中编写查询,我需要在hivetableA TableB,如果匹配则处理完成。但是如果我们没有得到任何匹配的记录,那么我们需要在col1上执行不同的连接标准。

任何人都可以帮我这个吗?提前谢谢。

我需要这样的东西:

col2
  • 我想检查第一个表是否为空,如果它不为空,则只创建第二个表。
  • 我不知道如何检查第一张表中是否有数据
create table Data1 AS
select *
from table1
join table2
  on table1.a = table2.a;

----------示例第二次加入-----------------

create table Data2 AS
select *
from table1
join table2
  on table1.b = table2.b;

因此它应该回答3,2,它使用第二个连接,因为我们没有第一个匹配的记录。

----------首次加入

with tableA as (
select 1 as a,  22 as b 
union
select 2 as a,  23 as b 
union
select 3 as a,  2 as b 
)
,
tableB as (
select 111 as a,  2 as b 
union
select 222 as a,  1 as b 
)
select x.* from 
tableA x join tableB y
on x.b=y.b

它应该给出以下结果:

with tableA as (
select 1 as a,  22 as b 
union
select 2 as a,  23 as b 
union
select 3 as a,  2 as b 
)
,
tableB as (
select 1 as a,  2 as b 
union
select 2 as a,  1 as b 
)
select x.* from 
tableA x join tableB y
on x.a=y.a

当第一次加入有效时,我们不应该得到任何与第二次加入条件匹配的记录。

*****抱歉我使用过SQL服务器的示例数据,而不是hive

2 个答案:

答案 0 :(得分:0)

正如你所指出的,Hive在join子句中不支持parseState,但是现在我想到了这个并不是你想要的。要完全复制业务逻辑,可以使用以下代码,它为每个条件使用左连接,然后使用case语句

or

答案 1 :(得分:0)

感谢您的帮助。我解决了我的问题:

首先,我在两个不同的表中执行连接,然后使用Except运算符获取所需的结果。它正在工作!

  DROP TABLE IF EXISTS TEST3
            CREATE TABLE TEST3 AS 
            SELECT * from
            (
            SELECT * FROM TEST1
            UNION ALL
            SELECT * FROM TEST2 a
            WHERE NOT EXISTS
            (SELECT 1 FROM TEST3 b WHERE a.UNQKEY=b.UNQKEY)
            )a