java程序访问hbase(java访问http)

本文目录一览:

如何使用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

求助帖,hbase新手,windows中的java怎么连接linux中的hbase

一、新建本地java工程

file-new-java project

二、添加jar包和配置文件

1、添加JAR包

右击Propertie在弹出的快捷菜单中选择Java Build Path对话框,在该对话框中单击Libraries选项卡,在该选项卡下单击

Add External JARs按钮,定位到$HBASE/lib目录下,并选取如下JAR包。

hadoop-core-1.0.0.jar

commons-loggings-version.jar

commons-cli-version.jar

commons-lang-version.jar

commons-configuration-version.jar

hbase-0.94.1.jar

zookeeper-3.4.3.jar

slf4j-api-1.5.8.jar

slf4j-log4j12-1.5.8.jar

log4j-1.2.16.jar

protobuf-java-2.4.1.jar

2、添加hbase-site.xml配置文件

在工程根目录下创建conf文件夹,将$HBASE_HOME/conf/目录中的hbase-site.xml文件复制到该文件夹中。通过右键

选择Propertie-Java Build Path-Libraries-Add Class Folder。

3、windows下开发HBase应用程序,HBase部署在linux环境中,在运行调试时可能会出现无法找到主机,类似异常信息如下:java.net.UnknownHostException: unknown host: master

解决办法如下:在C:\WINDOWS\system32\drivers\etc\hosts文件中添加如下信息

192.168.2.34 master

如何使用Java API读写Hbase

一般情况下,我们使用Linux的shell命令,就可以非常轻松的操作Hbase,例如一些建表,建列簇,插值,显示所有表,统计数量等等,但有时为了提高灵活性,我们也需要使用编程语言来操作Hbase,当然Hbase通过Thrift接口提供了对大多数主流编程语言的支持,例如C++,PHP,Python,Ruby等等,那么本篇,散仙给出的例子是基于Java原生的API操作Hbase,相比其他的一些编程语言,使用Java操作Hbase,会更加高效一些,因为Hbase本身就是使用Java语言编写的。转载

下面,散仙给出源码,以供参考:

package com.hbase;

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.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.util.Bytes;

/**

* @author 三劫散仙

*

* **/

public class Test {

static Configuration conf=null;

static{

conf=HBaseConfiguration.create();//hbase的配置信息

conf.set(“hbase.zookeeper.quorum”, “10.2.143.5”); //zookeeper的地址

}

public static void main(String[] args)throws Exception {

Test t=new Test();

//t.createTable(“temp”, new String[]{“name”,”age”});

//t.insertRow(“temp”, “2”, “age”, “myage”, “100”);

// t.getOneDataByRowKey(“temp”, “2”);

t.showAll(“temp”);

}

/***

* 创建一张表

* 并指定列簇

* */

public void createTable(String tableName,String cols[])throws Exception{

HBaseAdmin admin=new HBaseAdmin(conf);//客户端管理工具类

if(admin.tableExists(tableName)){

System.out.println(“此表已经存在…….”);

}else{

HTableDescriptor table=new HTableDescriptor(tableName);

for(String c:cols){

HColumnDescriptor col=new HColumnDescriptor(c);//列簇名

table.addFamily(col);//添加到此表中

}

admin.createTable(table);//创建一个表

admin.close();

System.out.println(“创建表成功!”);

}

}

/**

* 添加数据,

* 建议使用批量添加

* @param tableName 表名

* @param row 行号

* @param columnFamily 列簇

* @param column 列

* @param value 具体的值

*

* **/

public void insertRow(String tableName, String row,

String columnFamily, String column, String value) throws Exception {

HTable table = new HTable(conf, tableName);

Put put = new Put(Bytes.toBytes(row));

// 参数出分别:列族、列、值

put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),

Bytes.toBytes(value));

table.put(put);

table.close();//关闭

System.out.println(“插入一条数据成功!”);

}

/**

* 删除一条数据

* @param tableName 表名

* @param row rowkey

* **/

public void deleteByRow(String tableName,String rowkey)throws Exception{

HTable h=new HTable(conf, tableName);

Delete d=new Delete(Bytes.toBytes(rowkey));

h.delete(d);//删除一条数据

h.close();

}

/**

* 删除多条数据

* @param tableName 表名

* @param row rowkey

* **/

public void deleteByRow(String tableName,String rowkey[])throws Exception{

HTable h=new HTable(conf, tableName);

ListDelete list=new ArrayListDelete();

for(String k:rowkey){

Delete d=new Delete(Bytes.toBytes(k));

list.add(d);

}

h.delete(list);//删除

h.close();//释放资源

}

/**

* 得到一条数据

*

* @param tableName 表名

* @param rowkey 行号

* ***/

public void getOneDataByRowKey(String tableName,String rowkey)throws Exception{

HTable h=new HTable(conf, tableName);

Get g=new Get(Bytes.toBytes(rowkey));

Result r=h.get(g);

for(KeyValue k:r.raw()){

System.out.println(“行号: “+Bytes.toStringBinary(k.getRow()));

System.out.println(“时间戳: “+k.getTimestamp());

System.out.println(“列簇: “+Bytes.toStringBinary(k.getFamily()));

System.out.println(“列: “+Bytes.toStringBinary(k.getQualifier()));

//if(Bytes.toStringBinary(k.getQualifier()).equals(“myage”)){

// System.out.println(“值: “+Bytes.toInt(k.getValue()));

//}else{

String ss= Bytes.toString(k.getValue());

System.out.println(“值: “+ss);

//}

}

h.close();

}

/**

* 扫描所有数据或特定数据

* @param tableName

* **/

public void showAll(String tableName)throws Exception{

HTable h=new HTable(conf, tableName);

Scan scan=new Scan();

//扫描特定区间

//Scan scan=new Scan(Bytes.toBytes(“开始行号”),Bytes.toBytes(“结束行号”));

ResultScanner scanner=h.getScanner(scan);

for(Result r:scanner){

System.out.println(“==================================”);

for(KeyValue k:r.raw()){

System.out.println(“行号: “+Bytes.toStringBinary(k.getRow()));

System.out.println(“时间戳: “+k.getTimestamp());

System.out.println(“列簇: “+Bytes.toStringBinary(k.getFamily()));

System.out.println(“列: “+Bytes.toStringBinary(k.getQualifier()));

//if(Bytes.toStringBinary(k.getQualifier()).equals(“myage”)){

// System.out.println(“值: “+Bytes.toInt(k.getValue()));

//}else{

String ss= Bytes.toString(k.getValue());

System.out.println(“值: “+ss);

//}

}

}

h.close();

}

}

显示所有数据的打印输出如下:

==================================

行号: 1

时间戳: 1385597699287

列簇: name

列: myname

值: 秦东亮

==================================

行号: 2

时间戳: 1385598393306

列簇: age

列: myage

值: 100

行号: 2

时间戳: 1385597723900

列簇: name

列: myname

值: 三劫散仙

由此,可以看出Hbase的对外的API提供接口,是非常简单易用的

hbase java端调用

这是缺少必要的类org/apache/hadoop/thirdparty/guava/common/primitives/UnsignedBytes

你可以到jarsearch上搜索含有这个类的jar包,然后把它放到classpath下就行了

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/306633.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2025-01-02 12:01
下一篇 2025-01-02 12:01

相关推荐

  • java client.getacsresponse 编译报错解决方法

    java client.getacsresponse 编译报错是Java编程过程中常见的错误,常见的原因是代码的语法错误、类库依赖问题和编译环境的配置问题。下面将从多个方面进行分析…

    编程 2025-04-29
  • Java JsonPath 效率优化指南

    本篇文章将深入探讨Java JsonPath的效率问题,并提供一些优化方案。 一、JsonPath 简介 JsonPath是一个可用于从JSON数据中获取信息的库。它提供了一种DS…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • Java Bean加载过程

    Java Bean加载过程涉及到类加载器、反射机制和Java虚拟机的执行过程。在本文中,将从这三个方面详细阐述Java Bean加载的过程。 一、类加载器 类加载器是Java虚拟机…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Java腾讯云音视频对接

    本文旨在从多个方面详细阐述Java腾讯云音视频对接,提供完整的代码示例。 一、腾讯云音视频介绍 腾讯云音视频服务(Cloud Tencent Real-Time Communica…

    编程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介绍

    本文将详细介绍Java Milvus SearchParam withoutFields的相关知识和用法。 一、什么是Java Milvus SearchParam without…

    编程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java语言中的一个版本,于2014年3月18日发布。本文将从多个方面对Java 8中某一周的周一进行详细的阐述。 一、数组处理 Java 8新特性之一是Stream…

    编程 2025-04-29
  • Python程序文件的拓展

    Python是一门功能丰富、易于学习、可读性高的编程语言。Python程序文件通常以.py为文件拓展名,被广泛应用于各种领域,包括Web开发、机器学习、科学计算等。为了更好地发挥P…

    编程 2025-04-29
  • Java判断字符串是否存在多个

    本文将从以下几个方面详细阐述如何使用Java判断一个字符串中是否存在多个指定字符: 一、字符串遍历 字符串是Java编程中非常重要的一种数据类型。要判断字符串中是否存在多个指定字符…

    编程 2025-04-29

发表回复

登录后才能评论