「hbasejava示例」hbase join
今天给各位分享hbasejava示例的知识,其中也会对hbase join进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、hbase java
- 2、hbase怎么写事务呢,java里怎么写
- 3、java怎样给hbase插入数据
- 4、hbase的timestamp怎么换算 java
- 5、如何使用Java API操作Hbase
- 6、hbase java端调用
hbase java
hbase java是什么,让我们一起了解一下?
HBase是一个分布式的、面向列的开源数据库,具有高可靠性、高性能、面向列、可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群。
如何使用JAVA语言操作Hbase、整合Hbase?
可分为五步骤:
步骤1:新创建一个Java Project 。
步骤2:导入JAR包,在工程根目录下新建一个“lib”文件夹,将官方文档中的lib目录下的jar全部导入。
步骤3:修改开发机的hosts文件,在文件莫为增加一行虚拟机IP的映射信息。
步骤4:修改虚拟机的配置文件,修改虚拟机的设备名称,名称需要与之前两个配置文件的映射名称一致。
步骤5:实现查询、新建、删除等。
案例代码展示如下:
package hbase; import java.io.IOException; 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.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.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; 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.client.Table; import org.apache.hadoop.hbase.exceptions.DeserializationException; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.util.Bytes; import org.junit.Before; import org.junit.Test; public class HBaseDemo { // 与HBase数据库的连接对象 Connection connection; // 数据库元数据操作对象 Admin admin; @Before public void setUp() throws Exception { // 取得一个数据库连接的配置参数对象 Configuration conf = HBaseConfiguration.create(); // 设置连接参数:HBase数据库所在的主机IP conf.set("hbase.zookeeper.quorum", "192.168.137.13"); // 设置连接参数:HBase数据库使用的端口 conf.set("hbase.zookeeper.property.clientPort", "2181"); // 取得一个数据库连接对象 connection = ConnectionFactory.createConnection(conf); // 取得一个数据库元数据操作对象 admin = connection.getAdmin(); } /** * 创建表 */ public void createTable() throws IOException{ System.out.println("---------------创建表 START-----------------"); // 数据表表名 String tableNameString = "t_book"; // 新建一个数据表表名对象 TableName tableName = TableName.valueOf(tableNameString); // 如果需要新建的表已经存在 if(admin.tableExists(tableName)){ System.out.println("表已经存在!"); } // 如果需要新建的表不存在 else{ // 数据表描述对象 HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName); // 列族描述对象 HColumnDescriptor family= new HColumnDescriptor("base");; // 在数据表中新建一个列族 hTableDescriptor.addFamily(family); // 新建数据表 admin.createTable(hTableDescriptor); } System.out.println("---------------创建表 END-----------------"); } /** * 查询整表数据 */ @Test public void queryTable() throws IOException{ System.out.println("---------------查询整表数据 START-----------------"); // 取得数据表对象 Table table = connection.getTable(TableName.valueOf("t_book")); // 取得表中所有数据 ResultScanner scanner = table.getScanner(new Scan()); // 循环输出表中的数据 for (Result result : scanner) { byte[] row = result.getRow(); System.out.println("row key is:" + new String(row)); List listCells = result.listCells(); for (Cell cell : listCells) { byte[] familyArray = cell.getFamilyArray(); byte[] qualifierArray = cell.getQualifierArray(); byte[] valueArray = cell.getValueArray(); System.out.println("row value is:" + new String(familyArray) + new String(qualifierArray) + new String(valueArray)); } } System.out.println("---------------查询整表数据 END-----------------"); } /** * 按行键查询表数据 */ @Test public void queryTableByRowKey() throws IOException{ System.out.println("---------------按行键查询表数据 START-----------------"); // 取得数据表对象 Table table = connection.getTable(TableName.valueOf("t_book")); // 新建一个查询对象作为查询条件 Get get = new Get("row8".getBytes()); // 按行键查询数据 Result result = table.get(get); byte[] row = result.getRow(); System.out.println("row key is:" + new String(row)); List listCells = result.listCells(); for (Cell cell : listCells) { byte[] familyArray = cell.getFamilyArray(); byte[] qualifierArray = cell.getQualifierArray(); byte[] valueArray = cell.getValueArray(); System.out.println("row value is:" + new String(familyArray) + new String(qualifierArray) + new String(valueArray)); } System.out.println("---------------按行键查询表数据 END-----------------"); } /** * 按条件查询表数据 */ @Test public void queryTableByCondition() throws IOException{ System.out.println("---------------按条件查询表数据 START-----------------"); // 取得数据表对象 Table table = connection.getTable(TableName.valueOf("t_book")); // 创建一个查询过滤器 Filter filter = new SingleColumnValueFilter(Bytes.toBytes("base"), Bytes.toBytes("name"), CompareOp.EQUAL, Bytes.toBytes("bookName6")); // 创建一个数据表扫描器 Scan scan = new Scan(); // 将查询过滤器加入到数据表扫描器对象 scan.setFilter(filter); // 执行查询操作,并取得查询结果 ResultScanner scanner = table.getScanner(scan); // 循环输出查询结果 for (Result result : scanner) { byte[] row = result.getRow(); System.out.println("row key is:" + new String(row)); List listCells = result.listCells(); for (Cell cell : listCells) { byte[] familyArray = cell.getFamilyArray(); byte[] qualifierArray = cell.getQualifierArray(); byte[] valueArray = cell.getValueArray(); System.out.println("row value is:" + new String(familyArray) + new String(qualifierArray) + new String(valueArray)); } } System.out.println("---------------按条件查询表数据 END-----------------"); } /** * 清空表 */ @Test public void truncateTable() throws IOException{ System.out.println("---------------清空表 START-----------------"); // 取得目标数据表的表名对象 TableName tableName = TableName.valueOf("t_book"); // 设置表状态为无效 admin.disableTable(tableName); // 清空指定表的数据 admin.truncateTable(tableName, true); System.out.println("---------------清空表 End-----------------"); } /** * 删除表 */ @Test public void deleteTable() throws IOException{ System.out.println("---------------删除表 START-----------------"); // 设置表状态为无效 admin.disableTable(TableName.valueOf("t_book")); // 删除指定的数据表 admin.deleteTable(TableName.valueOf("t_book")); System.out.println("---------------删除表 End-----------------"); } /** * 删除行 */ @Test public void deleteByRowKey() throws IOException{ System.out.println("---------------删除行 START-----------------"); // 取得待操作的数据表对象 Table table = connection.getTable(TableName.valueOf("t_book")); // 创建删除条件对象 Delete delete = new Delete(Bytes.toBytes("row2")); // 执行删除操作 table.delete(delete); System.out.println("---------------删除行 End-----------------"); } /** * 删除行(按条件) */ @Test public void deleteByCondition() throws IOException, DeserializationException{ System.out.println("---------------删除行(按条件) START-----------------"); // 步骤1:调用queryTableByCondition()方法取得需要删除的数据列表 // 步骤2:循环步骤1的查询结果,对每个结果调用deleteByRowKey()方法 System.out.println("---------------删除行(按条件) End-----------------"); } /** * 新建列族 */ @Test public void addColumnFamily() throws IOException{ System.out.println("---------------新建列族 START-----------------"); // 取得目标数据表的表名对象 TableName tableName = TableName.valueOf("t_book"); // 创建列族对象 HColumnDescriptor columnDescriptor = new HColumnDescriptor("more"); // 将新创建的列族添加到指定的数据表 admin.addColumn(tableName, columnDescriptor); System.out.println("---------------新建列族 END-----------------"); } /** * 删除列族 */ @Test public void deleteColumnFamily() throws IOException{ System.out.println("---------------删除列族 START-----------------"); // 取得目标数据表的表名对象 TableName tableName = TableName.valueOf("t_book"); // 删除指定数据表中的指定列族 admin.deleteColumn(tableName, "more".getBytes()); System.out.println("---------------删除列族 END-----------------"); } /** * 插入数据 */ @Test public void insert() throws IOException{ System.out.println("---------------插入数据 START-----------------"); // 取得一个数据表对象 Table table = connection.getTable(TableName.valueOf("t_book")); // 需要插入数据库的数据集合 List putList = new ArrayList (); Put put; // 生成数据集合 for(int i = 0; i
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怎样给hbase插入数据
hbase 是动态列的,直接加就可以了,不用事先定义的啊
例如:(代码没有调试过,具体可看hbase的例子)
Table table = connection.getTable(TableName.valueOf(表名));
Put put = new Put(Bytes.toBytes(主键字符串));
put.addColumn(FieldFamily, Bytes.toBytes(字段名1), Bytes.toBytes(插入的值1));
put.addColumn(FieldFamily, Bytes.toBytes(字段名2), Bytes.toBytes(插入的值2));
.........
table.put(put);
hbase的timestamp怎么换算 java
hbase shell中timestamp转为可读格式
将hbase shell的timestamp转为可读。下面的示例将-ROOT-表的列info:serverstartcode的timestamp和value转成可读格式。
hbase(main):001:0 scan '-ROOT-'
ROW COLUMN+CELL
.META.,,1 column=info:regioninfo, timestamp=1340249081981, value={NAME = '.META.,,
1', STARTKEY = '', ENDKEY = '', ENCODED = 1028785192,}
.META.,,1 column=info:server, timestamp=1341304672637, value=Hadoop46:60020
.META.,,1 column=info:serverstartcode, timestamp=1341304672637, value=1341301228326
.META.,,1 column=info:v, timestamp=1340249081981, value=\x00\x00
1 row(s) in 1.3230 seconds
hbase(main):002:0 import java.util.Date
= Java::JavaUtil::Date
hbase(main):003:0 Date.new(1341304672637).toString()
= "Tue Jul 03 16:37:52 CST 2012"
hbase(main):004:0 Date.new(1341301228326).toString()
= "Tue Jul 03 15:40:28 CST 2012"
在shell中,如果有可读日期,能否转成long类型呢?
hbase(main):005:0 import java.text.SimpleDateFormat
= Java::JavaText::SimpleDateFormat
hbase(main):006:0 import java.text.ParsePosition
= Java::JavaText::ParsePosition
hbase(main):015:0 SimpleDateFormat.new("yy/MM/dd").parse("12/07/03",ParsePosition.new(0)).getTime()
= 1341244800000
如何使用Java API操作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.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
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.HTablePool;
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.client.Table;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseUtil {
public static Configuration configuration;
static {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.zookeeper.quorum", "192.168.1.103");
configuration.set("hbase.master", "192.168.1.103:600000");
}
public static void main(String[] args) throws IOException {
createTable("abc");
insertData("abc");
QueryAll("abc");
}
/**
* 创建表
* @param tableName
*/
public static void createTable(String tableName) {
System.out.println("start create table ......");
try {
HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
if (hBaseAdmin.tableExists(tableName)) {// 如果存在要创建的表,那么先删除,再创建
hBaseAdmin.disableTable(tableName);
hBaseAdmin.deleteTable(tableName);
System.out.println(tableName + " is exist,detele....");
}
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
tableDescriptor.addFamily(new HColumnDescriptor("column1"));
tableDescriptor.addFamily(new HColumnDescriptor("column2"));
tableDescriptor.addFamily(new HColumnDescriptor("column3"));
hBaseAdmin.createTable(tableDescriptor);
hBaseAdmin.close();
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end create table ......");
}
/**
* 插入数据
* @param tableName
* @throws IOException
*/
public static void insertData(String tableName) throws IOException {
System.out.println("start insert data ......");
Connection connection = ConnectionFactory.createConnection(configuration);
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put("112233bbbcccc".getBytes());// 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值
put.add("column1".getBytes(), null, "aaa".getBytes());// 本行数据的第一列
put.add("column2".getBytes(), null, "bbb".getBytes());// 本行数据的第三列
put.add("column3".getBytes(), null, "ccc".getBytes());// 本行数据的第三列
try {
table.put(put);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end insert data ......");
}
/**
* 删除一张表
* @param tableName
*/
public static void dropTable(String tableName) {
try {
HBaseAdmin admin = new HBaseAdmin(configuration);
admin.disableTable(tableName);
admin.deleteTable(tableName);
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 根据 rowkey删除一条记录
* @param tablename
* @param rowkey
*/
public static void deleteRow(String tablename, String rowkey) {
try {
HTable table = new HTable(configuration, tablename);
List list = new ArrayList();
Delete d1 = new Delete(rowkey.getBytes());
list.add(d1);
table.delete(list);
System.out.println("删除行成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 组合条件删除
* @param tablename
* @param rowkey
*/
public static void deleteByCondition(String tablename, String rowkey) {
//目前还没有发现有效的API能够实现 根据非rowkey的条件删除 这个功能能,还有清空表全部数据的API操作
}
/**
* 查询所有数据
* @param tableName
* @throws IOException
*/
public static void QueryAll(String tableName) throws IOException {
Connection connection = ConnectionFactory.createConnection(configuration);
Table table = connection.getTable(TableName.valueOf(tableName));
try {
ResultScanner rs = table.getScanner(new Scan());
for (Result r : rs) {
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+ "====值:" + new String(keyValue.getValue()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 单条件查询,根据rowkey查询唯一一条记录
* @param tableName
*/
public static void QueryByCondition1(String tableName) {
HTablePool pool = new HTablePool(configuration, 1000);
HTable table = (HTable) pool.getTable(tableName);
try {
Get scan = new Get("abcdef".getBytes());// 根据rowkey查询
Result r = table.get(scan);
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+ "====值:" + new String(keyValue.getValue()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 单条件按查询,查询多条记录
* @param tableName
*/
public static void QueryByCondition2(String tableName) {
try {
HTablePool pool = new HTablePool(configuration, 1000);
HTable table = (HTable) pool.getTable(tableName);
Filter filter = new SingleColumnValueFilter(Bytes
.toBytes("column1"), null, CompareOp.EQUAL, Bytes
.toBytes("aaa")); // 当列column1的值为aaa时进行查询
Scan s = new Scan();
s.setFilter(filter);
ResultScanner rs = table.getScanner(s);
for (Result r : rs) {
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+ "====值:" + new String(keyValue.getValue()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 组合条件查询
* @param tableName
*/
public static void QueryByCondition3(String tableName) {
try {
HTablePool pool = new HTablePool(configuration, 1000);
HTable table = (HTable) pool.getTable(tableName);
ListFilter filters = new ArrayListFilter();
Filter filter1 = new SingleColumnValueFilter(Bytes
.toBytes("column1"), null, CompareOp.EQUAL, Bytes
.toBytes("aaa"));
filters.add(filter1);
Filter filter2 = new SingleColumnValueFilter(Bytes
.toBytes("column2"), null, CompareOp.EQUAL, Bytes
.toBytes("bbb"));
filters.add(filter2);
Filter filter3 = new SingleColumnValueFilter(Bytes
.toBytes("column3"), null, CompareOp.EQUAL, Bytes
.toBytes("ccc"));
filters.add(filter3);
FilterList filterList1 = new FilterList(filters);
Scan scan = new Scan();
scan.setFilter(filterList1);
ResultScanner rs = table.getScanner(scan);
for (Result r : rs) {
System.out.println("获得到rowkey:" + new String(r.getRow()));
for (KeyValue keyValue : r.raw()) {
System.out.println("列:" + new String(keyValue.getFamily())
+ "====值:" + new String(keyValue.getValue()));
}
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
hbase java端调用
这是缺少必要的类org/apache/hadoop/thirdparty/guava/common/primitives/UnsignedBytes
你可以到jarsearch上搜索含有这个类的jar包,然后把它放到classpath下就行了
hbasejava示例的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于hbase join、hbasejava示例的信息别忘了在本站进行查找喔。