「java编写hbase」java编写一个窗口和按钮
本篇文章给大家谈谈java编写hbase,以及java编写一个窗口和按钮对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、hbase怎么写事务呢,java里怎么写
- 2、如何使用Java API操作Hbase
- 3、java 操作hbase数据库读取数据时运行代码到new h1table就不动了,跟卡住了一样。会的大神们可以加
- 4、java写的hbase脚本怎么打包
- 5、北大青鸟java培训:Hbase知识点总结?
- 6、我自己写java代码调用hbase的api来读写hbase 跟 写mapreduce来读写hbase,哪个效率高?高在哪里?
hbase怎么写事务呢,java里怎么写
Java中为了控制事务的一致性,会使用插入回滚点、callback方法,保证数据不被篡改,示例如下:
public String delete(String id) {
String ID = id;
db = new getConnection();
Connection con = db.getConnection();
try {
con.setAutoCommit(false);
db.executeUpdate("delete from helloworld where ID=" + ID); //更新操作1
db.executeUpdate("delete from helloworld _book where ID=" + ID); //更新操作2
db.executeUpdate("delete from helloworld_user where ID=" + ID); //更新操作3
con.commit();//提交JDBC事务
con.setAutoCommit(true);
db.close();
return “success”;
}
catch (Exception e) {
con.rollBack();//回滚JDBC事务
e.printStackTrace();
db.close();
return “fail”;
}
}
如何使用Java API操作Hbase
写了个Hbase新的api的增删改查的工具类,以供参考,代码如下:
package com.dhgate.hbase.test;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;
/**
* 基于新的API
* Hbase0.96版本
* 写的工具类
*
* **/
public class HbaseCommons {
static Configuration conf=HBaseConfiguration.create();
static String tableName="";
public static void main(String[] args)throws Exception {
//String tableName="test";
//createTable(tableName, null);
}
/**
* 批量添加数据
* @param tableName 标名字
* @param rows rowkey行健的集合
* 本方法仅作示例,其他的内容需要看自己义务改变
*
* **/
public static void insertList(String tableName,String rows[])throws Exception{
HTable table=new HTable(conf, tableName);
ListPut list=new ArrayListPut();
for(String r:rows){
Put p=new Put(Bytes.toBytes(r));
//此处示例添加其他信息
//p.add(Bytes.toBytes("family"),Bytes.toBytes("column"), 1000, Bytes.toBytes("value"));
list.add(p);
}
table.put(list);//批量添加
table.close();//释放资源
}
/**
* 创建一个表
* @param tableName 表名字
* @param columnFamilys 列簇
*
* **/
public static void createTable(String tableName,String[] columnFamilys)throws Exception{
//admin 对象
HBaseAdmin admin=new HBaseAdmin(conf);
if(admin.tableExists(tableName)){
System.out.println("此表,已存在!");
}else{
//旧的写法
//HTableDescriptor tableDesc=new HTableDescriptor(tableName);
//新的api
HTableDescriptor tableDesc=new HTableDescriptor(TableName.valueOf(tableName));
for(String columnFamily:columnFamilys){
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
}
admin.createTable(tableDesc);
System.out.println("建表成功!");
}
admin.close();//关闭释放资源
}
/**
* 删除一个表
* @param tableName 删除的表名
* */
public static void deleteTable(String tableName)throws Exception{
HBaseAdmin admin=new HBaseAdmin(conf);
if(admin.tableExists(tableName)){
admin.disableTable(tableName);//禁用表
admin.deleteTable(tableName);//删除表
System.out.println("删除表成功!");
}else{
System.out.println("删除的表不存在!");
}
admin.close();
}
/**
* 插入一条数据
* @param tableName 表明
* @param columnFamily 列簇
* @param column 列
* @param value 值
*
* ***/
public static void insertOneRow(String tableName,String rowkey,String columnFamily,String column,String value)throws Exception{
HTable table=new HTable(conf, tableName);
Put put=new Put(Bytes.toBytes(rowkey));
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);//放入表
table.close();//释放资源
}
/**
* 删除一条数据
* @param tableName 表名
* @param row rowkey行键
*
* */
public static void deleteOneRow(String tableName,String row)throws Exception{
HTable table=new HTable(conf, tableName);
Delete delete=new Delete(Bytes.toBytes(row));
table.delete(delete);
table.close();
}
/**
* 删除多条数据
* @param tableName 表名
* @param rows 行健集合
*
* **/
public static void deleteList(String tableName,String rows[])throws Exception{
HTable table=new HTable(conf, tableName);
ListDelete list=new ArrayListDelete();
for(String row:rows){
Delete del=new Delete(Bytes.toBytes(row));
list.add(del);
}
table.delete(list);
table.close();//释放资源
}
/**
* 获取一条数据,根据rowkey
* @param tableName 表名
* @param row 行健
*
* **/
public static void getOneRow(String tableName,String row)throws Exception{
HTable table=new HTable(conf, tableName);
Get get=new Get(Bytes.toBytes(row));
Result result=table.get(get);
printRecoder(result);//打印记录
table.close();//释放资源
}
/**
* 查看某个表下的所有数据
*
* @param tableName 表名
* */
public static void showAll(String tableName)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
printRecoder(r);//打印记录
}
table.close();//释放资源
}
/**
* 查看某个表下的所有数据
*
* @param tableName 表名
* @param rowKey 行健
* */
public static void ScanPrefixByRowKey(String tableName,String rowKey)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
scan.setFilter(new PrefixFilter(Bytes.toBytes(rowKey)));
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
printRecoder(r);//打印记录
}
table.close();//释放资源
}
/**
* 查看某个表下的所有数据
*
* @param tableName 表名
* @param rowKey 行健扫描
* @param limit 限制返回数据量
* */
public static void ScanPrefixByRowKeyAndLimit(String tableName,String rowKey,long limit)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
scan.setFilter(new PrefixFilter(Bytes.toBytes(rowKey)));
scan.setFilter(new PageFilter(limit));
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
printRecoder(r);//打印记录
}
table.close();//释放资源
}
/**
* 根据rowkey扫描一段范围
* @param tableName 表名
* @param startRow 开始的行健
* @param stopRow 结束的行健
* **/
public void scanByStartAndStopRow(String tableName,String startRow,String stopRow)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
scan.setStartRow(Bytes.toBytes(startRow));
scan.setStopRow(Bytes.toBytes(stopRow));
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
printRecoder(r);
}
table.close();//释放资源
}
/**
* 扫描整个表里面具体的某个字段的值
* @param tableName 表名
* @param columnFalimy 列簇
* @param column 列
* **/
public static void getValueDetail(String tableName,String columnFalimy,String column)throws Exception{
HTable table=new HTable(conf, tableName);
Scan scan=new Scan();
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
System.out.println("值: " +new String(r.getValue(Bytes.toBytes(columnFalimy), Bytes.toBytes(column))));
}
table.close();//释放资源
}
/**
* 打印一条记录的详情
*
* */
public static void printRecoder(Result result)throws Exception{
for(Cell cell:result.rawCells()){
System.out.print("行健: "+new String(CellUtil.cloneRow(cell)));
System.out.print("列簇: "+new String(CellUtil.cloneFamily(cell)));
System.out.print(" 列: "+new String(CellUtil.cloneQualifier(cell)));
System.out.print(" 值: "+new String(CellUtil.cloneValue(cell)));
System.out.println("时间戳: "+cell.getTimestamp());
}
}
}
java 操作hbase数据库读取数据时运行代码到new h1table就不动了,跟卡住了一样。会的大神们可以加
首先你应该看Master进程是否已经成功启动,检查下master的60010监控界面。这日志报的是连接拒绝 ,或者关闭防火墙 极有可能是你PC机网络无法连接到虚拟机里边,你可以从本机telnet下虚拟机上master的端口,看下能连上不6646
java写的hbase脚本怎么打包
先导入hbase的相关jar包。 再根据api进行操作。 package com.util;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfigura
北大青鸟java培训:Hbase知识点总结?
hbase概念: 非结构化的分布式的面向列存储非关系型的开源的数据库,根据谷歌的三大论文之一的bigtable 高宽厚表 作用: 为了解决大规模数据集合多重数据种类带来的挑战,尤其是大数据应用难题。
能干什么: 存储大量结果集数据,低延迟的随机查询。
sql: 结构化查询语言 nosql: 非关系型数据库,列存储和文档存储(查询低延迟),hbase是nosql的一个种类,其特点是列式存储。
非关系型数据库--列存储(hbase) 非关系型数据库--文档存储(MongoDB) 非关系型数据库--内存式存储(redis) 非关系型数据库--图形模型(graph) hive和hbase区别? Hive的定位是数据仓库,虽然也有增删改查,但其删改查对应的是整张表而不是单行数据,查询的延迟较高。
其本质是更加方便的使用mr的威力来进行离线分析的一个数据分析工具。
HBase的定位是hadoop的数据库,电脑培训发现是一个典型的Nosql,所以HBase是用来在大量数据中进行低延迟的随机查询的。
hbase运行方式: standalonedistrubited 单节点和伪分布式? 单节点:单独的进程运行在同一台机器上 hbase应用场景: 存储海量数据低延迟查询数据 hbase表由多行组成 hbase行一行在hbase中由行健和一个或多个列的值组成,按行健字母顺序排序的存储。
我自己写java代码调用hbase的api来读写hbase 跟 写mapreduce来读写hbase,哪个效率高?高在哪里?
如果你自己用“调”api,来读写hbase的话,我觉得具体考虑的话是任务能否最终实现的问题了,毕竟mapreduce所做的工作很多,它自己的master,zookeeper,hbase的master之间的通信,计算任务的reduce和mapping,细节太多,考虑到mapreduce通常处理的数据量,即便不考虑fault tolerant 都不一定能有效协调各个任务,更何况怎么可能不考虑?...所以,自己用java来实现的话,也许是个不错的学习过程,但是基本出不了东西,也就没有实用的可能...
关于java编写hbase和java编写一个窗口和按钮的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-24,除非注明,否则均为
原创文章,转载请注明出处。