package shangke;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException; import shangke.ExampleForHbase; import javax.swing.*; public class test extends JFrame
{
//控件
JLabel jb1 = new JLabel();
JTextField platName = new JTextField(15);
JLabel jb2 = new JLabel();
JTextField platNumber = new JTextField(15);
JLabel jb3=new JLabel();
JTextField date = new JTextField(15);
JLabel jb4=new JLabel();
JTextField number = new JTextField(15);
JLabel jb5=new JLabel();
JTextField tec = new JTextField(15);
//定义
FlowLayout flow = new FlowLayout();
JTextArea jt1 = new JTextArea();
JButton button = new JButton();
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
public test()
{
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
} private void jbInit()throws Exception
{
setSize(new Dimension(260,200));
getContentPane().setLayout(flow);
jb1.setText("平台名称");
platName.setText("");
jb2.setText("平台编号");
platNumber.setText("");
jb3.setText("批准年月");
date.setText("");
jb4.setText("批准文号");
number.setText("");
jb5.setText("技术领域");
tec.setText("");
jt1.setText("");
button.setText("插入");
button1.setText("删除");
button2.setText("修改");
button3.setText("查询");
button.addActionListener(new test_jButton1_actionAdapter(this));
button1.addActionListener(new test_jButton2_actionAdapter(this));
button2.addActionListener(new test_jButton3_actionAdapter(this));
button3.addActionListener(new test_jButton4_actionAdapter(this));
this.getContentPane().add(jb1);
this.getContentPane().add(platName);
this.getContentPane().add(jb2);
this.getContentPane().add(platNumber);
this.getContentPane().add(jb3);
this.getContentPane().add(date);
this.getContentPane().add(jb4);
this.getContentPane().add(number);
this.getContentPane().add(jb5);
this.getContentPane().add(tec);
this.getContentPane().add(button);
this.getContentPane().add(button1);
this.getContentPane().add(button2);
this.getContentPane().add(button3); } public static void main(String[] args)
{
test test=new test();
test.setVisible(true);
}
} class test_jButton1_actionAdapter implements ActionListener
{
private test adaptee;
test_jButton1_actionAdapter(test adaptee)
{
this.adaptee=adaptee;
}
public void actionPerformed(ActionEvent e)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e2)
{
e2.printStackTrace();
}
String platName=adaptee.platName.getText();
String platNumber=adaptee.platNumber.getText();
//adaptee.jt1.setText("您的用户名是:"+username+"您的输入密码是:"+String.valueOf(pwd));
String date=adaptee.date.getText();
String number=adaptee.number.getText();
String tec=adaptee.tec.getText();
try
{
ExampleForHbase.insertRow("Test", "1", "platName", "", platName);
ExampleForHbase.insertRow("Test", "1", "platNumber", "", platNumber);
ExampleForHbase.insertRow("Test", "1", "date", "", date);
ExampleForHbase.insertRow("Test", "1", "number", "", number);
ExampleForHbase.insertRow("Test", "1", "tec", "", tec);
JOptionPane.showMessageDialog(adaptee,"插入成功");
}
catch (IOException e)
{
e.printStackTrace();
}
} });
}
}
//delete
class test_jButton2_actionAdapter implements ActionListener
{
private test adaptee;
test_jButton2_actionAdapter(test adaptee)
{
this.adaptee=adaptee;
}
public void actionPerformed(ActionEvent e)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e2)
{
e2.printStackTrace();
}
String str=JOptionPane.showInputDialog("请输入要删除的平台编号");
try
{
ExampleForHbase.deleteRow("Test", "1", "", "");
JOptionPane.showMessageDialog(adaptee,"删除成功");
}
catch (IOException e)
{
e.printStackTrace();
}
} });
}
}
//update
class test_jButton3_actionAdapter implements ActionListener
{
private test adaptee;
test_jButton3_actionAdapter(test adaptee)
{
this.adaptee=adaptee;
}
public void actionPerformed(ActionEvent e)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e2)
{
e2.printStackTrace();
}
String str=JOptionPane.showInputDialog("请输入要修改的平台编号");
try
{
if(ExampleForHbase.getData("Test", "1", "platName", "")!=null)
{
ExampleForHbase.deleteRow("Test", "1", "", "");
String str1=JOptionPane.showInputDialog("请输入要修改的平台编号");
String str2=JOptionPane.showInputDialog("请输入要修改的平台名称");
String str3=JOptionPane.showInputDialog("请输入要修改的批准年月");
String str4=JOptionPane.showInputDialog("请输入要修改的批准文号");
String str5=JOptionPane.showInputDialog("请输入要修改的技术领域");
ExampleForHbase.insertRow("Test", "1", "platName", "", str1);
ExampleForHbase.insertRow("Test", "1", "platNumber", "", str2);
ExampleForHbase.insertRow("Test", "1", "date", "", str3);
ExampleForHbase.insertRow("Test", "1", "number", "", str4);
ExampleForHbase.insertRow("Test", "1", "tec", "", str5);
JOptionPane.showMessageDialog(adaptee,"修改成功");
}
else
{
JOptionPane.showMessageDialog(adaptee,"所要修改的不存在");
}
}
catch (IOException e)
{
e.printStackTrace();
}
} });
}
}
//select
class test_jButton4_actionAdapter implements ActionListener
{
private test adaptee;
test_jButton4_actionAdapter(test adaptee)
{
this.adaptee=adaptee;
}
public void actionPerformed(ActionEvent e)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e2)
{
e2.printStackTrace();
}
try
{
String str=JOptionPane.showInputDialog("请输入要查询的平台编号"); String platName=ExampleForHbase.getData("Test", "1", "platName", "");
String platNumber=ExampleForHbase.getData("Test", "1", "platNumber", "");
String date=ExampleForHbase.getData("Test", "1", "date", "");
String number=ExampleForHbase.getData("Test", "1", "number", "");
String tec=ExampleForHbase.getData("Test", "1", "tec", "");
if(platName==null)
{
JOptionPane.showMessageDialog(adaptee,"查询为空");
}
else
{
JOptionPane.showMessageDialog(adaptee,"平台名称:"+platName+"\n平台编号:"+platNumber+"\n批准年月:"+date+"\n批准文号:"+number+"\n技术领域:"+tec);
}
}
catch (IOException e)
{
e.printStackTrace();
}
} });
}
}
package shangke;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException; public class ExampleForHbase{
public static Configuration configuration;
public static Connection connection;
public static Admin admin; //主函数中的语句请逐句执行,只需删除其前的//即可,如:执行insertRow时请将其他语句注释
public static void main(String[] args)throws IOException{
//创建一个表,表名为Score,列族为sname,course
//createTable("Test",new String[]{"platName","platNumber","date","number","tec"}); //在Score表中插入一条数据,其行键为95001,sname为Mary(因为sname列族下没有子列所以第四个参数为空)
//等价命令:put 'Score','95001','sname','Mary'
//insertRow("Score", "95001", "sname", "", "Mary");
//在Score表中插入一条数据,其行键为95001,course:Math为88(course为列族,Math为course下的子列)
//等价命令:put 'Score','95001','score:Math','88'
//insertRow("Score", "95001", "course", "Math", "88");
//在Score表中插入一条数据,其行键为95001,course:English为85(course为列族,English为course下的子列)
//等价命令:put 'Score','95001','score:English','85'
//insertRow("Score", "95001", "course", "English", "85"); //1、删除Score表中指定列数据,其行键为95001,列族为course,列为Math
//执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码取消注释注释,将删除制定列族的代码注释
//等价命令:delete 'Score','95001','score:Math'
//deleteRow("Score", "95001", "course", "Math"); //2、删除Score表中指定列族数据,其行键为95001,列族为course(95001的Math和English的值都会被删除)
//执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码注释,将删除制定列族的代码取消注释
//等价命令:delete 'Score','95001','score'
//deleteRow("Score", "95001", "course", ""); //3、删除Score表中指定行数据,其行键为95001
//执行这句代码前请deleteRow方法的定义中,将删除指定列数据的代码注释,以及将删除制定列族的代码注释
//等价命令:deleteall 'Score','95001'
//deleteRow("Score", "95001", "", ""); //查询Score表中,行键为95001,列族为course,列为Math的值
//getData("Score", "95001", "course", "Math");
//查询Score表中,行键为95001,列族为sname的值(因为sname列族下没有子列所以第四个参数为空)
getData("Test", "1", "tec", ""); //删除Score表
//deleteTable("Score");
} //建立连接
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs://192.168.180.128:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (IOException e){
e.printStackTrace();
}
}
//关闭连接
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
} /**
* 建表。HBase的表中会有一个系统默认的属性作为主键,主键无需自行创建,默认为put命令操作中表名后第一个数据,因此此处无需创建id列
* @param myTableName 表名
* @param colFamily 列族名
* @throws IOException
*/
public static void createTable(String myTableName,String[] colFamily) throws IOException { init();
TableName tableName = TableName.valueOf(myTableName); if(admin.tableExists(tableName)){
System.out.println("talbe is exists!");
}else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for(String str:colFamily){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println("create table success");
}
close();
}
/**
* 删除指定表
* @param tableName 表名
* @throws IOException
*/
public static void deleteTable(String tableName) throws IOException {
init();
TableName tn = TableName.valueOf(tableName);
if (admin.tableExists(tn)) {
admin.disableTable(tn);
admin.deleteTable(tn);
}
close();
} /**
* 查看已有表
* @throws IOException
*/
public static void listTables() throws IOException {
init();
HTableDescriptor hTableDescriptors[] = admin.listTables();
for(HTableDescriptor hTableDescriptor :hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
close();
}
/**
* 向某一行的某一列插入数据
* @param tableName 表名
* @param rowKey 行键
* @param colFamily 列族名
* @param col 列名(如果其列族下没有子列,此参数可为空)
* @param val 值
* @throws IOException
*/
public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
table.put(put);
table.close();
close();
} /**
* 删除数据
* @param tableName 表名
* @param rowKey 行键
* @param colFamily 列族名
* @param col 列名
* @throws IOException
*/
public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(rowKey.getBytes());
//删除指定列族的所有数据
//delete.addFamily(colFamily.getBytes());
//删除指定列的数据
//delete.addColumn(colFamily.getBytes(), col.getBytes()); table.delete(delete);
table.close();
close();
}
/**
* 根据行键rowkey查找数据
* @param tableName 表名
* @param rowKey 行键
* @param colFamily 列族名
* @param col 列名
* @throws IOException
*/
public static String getData(String tableName,String rowKey,String colFamily,String col)throws IOException{
String str = null;
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
get.addColumn(colFamily.getBytes(),col.getBytes());
Result result = table.get(get);
showCell(result);
Cell[] cells = result.rawCells();
for(Cell cell:cells)
{
str=new String(CellUtil.cloneValue(cell),"UTF-8");
}
table.close();
close();
return str;
}
/**
* 格式化输出
* @param result
*/
public static void showCell(Result result)
{
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println(new String(CellUtil.cloneValue(cell))+" ");
}
}
}

简单窗口与hbase数据库相连的更多相关文章

  1. HBase数据库集群配置

    0,HBase简介 HBase是Apache Hadoop中的一个子项目,是一个HBase是一个开源的.分布式的.多版本的.面向列的.非关系(NoSQL)的.可伸缩性分布式数据存储模型,Hbase依托 ...

  2. pyqt5之简单窗口的创建

    在学完tkinter后,发现tkinter在布局方面特别的不方便(Tkinter资料:http://effbot.org/tkinterbook/tkinter-index.htm),因此学习pyqt ...

  3. HBase数据库相关基本知识

    HBase数据库相关知识 1. HBase相关概念模型 l  表(table),与关系型数据库一样就是有行和列的表 l  行(row),在表里数据按行存储.行由行键(rowkey)唯一标识,没有数据类 ...

  4. HBase数据库集群配置【转】

    https://www.cnblogs.com/ejiyuan/p/5591613.html HBase简介 HBase是Apache Hadoop中的一个子项目,是一个HBase是一个开源的.分布式 ...

  5. 一文让您全面了解清楚HBase数据库的所有知识点,值得收藏!

    一.HBase基本概念:列式数据库 在Hadoop生态体系结构中,HBase位于HDFS(Hadoop分布式文件系统)的上一层,不依赖于MapReduce,那么如果没有HBase这种Nosql数据库会 ...

  6. 【转载】HBase 数据库检索性能优化策略

    转自:http://www.ibm.com/developerworks/cn/java/j-lo-HBase/index.html 高性能 HBase 数据库 本文首先介绍了 HBase 数据库基本 ...

  7. HBase 数据库检索性能优化策略--转

    https://www.ibm.com/developerworks/cn/java/j-lo-HBase/index.html HBase 数据表介绍 HBase 数据库是一个基于分布式的.面向列的 ...

  8. php框架中的phalcon框架的安装,及初步认识,从表单提交简单的数据到数据库中

    php框架中的phalcon框架的安装,及初步认识,从表单提交简单的数据到数据库中 1.phalcon框架的安装: phalcon框架在windows本地安装可以利用wamp软件,安装之后可以查看对应 ...

  9. HBase数据库增删改查常用命令操作

    最近测试用到了Hbase数据库,新建一个学生表,对表进行增删改查操作,把常用命令贴出来分享给大家~ 官方API:https://hbase.apache.org/book.html#quickstar ...

随机推荐

  1. Linux基础实操一

    开启Linux操作系统,要求以root用户登录GNOME图形界面,语言支持选择为汉语 使用快捷键切换到虚拟终端2,使用普通用户身份登录,查看系统提示符 使用命令退出虚拟终端2上登录的用户 使用快捷键切 ...

  2. tensorflow(1) 基础: 神经网络基本框架

    1.tensorflow 的计算得到的是计算图graph import tensorflow as tf a=tf.constant([1.0,2.0]) b=tf.constant([3.0,4.0 ...

  3. beego获取用户请求参数的方法

    我们经常需要获取用户传递的数据,包括 Get.POST 等方式的请求,beego 里面会自动解析这些数据,你可以通过如下方式获取数据: GetString(key string) string Get ...

  4. C++ Primer 笔记——固有的不可移植的特性

    1.为了支持底层编程,C++定义了一些固有的不可移植的特性,所谓不可移植特性是指因机器而异的特性. 2.一个位域中含有一定数量的二进制位,位域在内存中的布局是机器相关的.位域的类型必须是整型或枚举类型 ...

  5. cnetos 7 mariadb 集群报错分析解答

    1.故障1:通过查看/var/log/message 发现报错 2017-04-14 14:44:10 139845276428544 [ERROR] WSREP: It may not be saf ...

  6. 20165323 结对编程之四则运算week2-整体总结

    一.需求 实现一个命令行程序,要求: 1.自动生成小学四则运算题目(加.减.乘.除) 2.支持整数 3.支持多运算符(比如生成包含100个运算符的题目) 4.支持真分数 5.能判断错误,在输入错误结果 ...

  7. 微信jssdk分享功能开发

    先理解下分享: 在app端 ,经常能看见 分享按钮的功能,(分享给朋友,分享到朋友圈,分享到QQ空间等等): https://open.weixin.qq.com/(微信开发平台),这需要到开放平台注 ...

  8. 学习笔记: 特性Attribute详解,应用封装

    /// /// 特性:中括号声明 /// /// 错觉:每一个特性都可以带来对应的功能 /// /// 实际上特性添加后,编译会在元素内部产生IL,但是我们是没办法直接使用的, /// 而且在meta ...

  9. U32592 摘果实

    链接:https://www.luogu.org/problemnew/show/U32592 题解: 60-70分 二分+网络流

  10. [转]JAVA实现SFTP实例

    http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888384.html 最近写的一个JAVA实现SFTP的实例: /** Created ...