import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.Put;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table; public class HBaseTest2 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin; public static void main(String[] args) {
configuration=HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase"); /*
try {
connection=ConnectionFactory.createConnection(configuration);
admin=connection.getAdmin();
insertRow("Student","scofield","score","English","45");
insertRow("Student","scofield","score","Math","89");
insertRow("Student","scofield","score","Computer","100");
System.out.println("插入成功!---李运辰");
} catch (IOException e) {
e.printStackTrace();
}
*/
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try {
connection=ConnectionFactory.createConnection(configuration);
admin=connection.getAdmin();
getData("Student","scofield","score","English");
System.out.println("输出完成!---李运辰");
} catch (IOException e) {
e.printStackTrace();
} close();
} public static void insertRow(String tableName, String rowKey, String colFamily, String col, String val) {
try {
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();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public static void getData(String tableName, String rowKey, String colFamily, String col) {
try {
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);
table.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } private static void showCell(Result result) {
Cell[] cells=result.rawCells();
for(Cell cell:cells) {
System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
System.out.println("Timetamp:"+cell.getTimestamp()+" ");
System.out.println("column Family"+new String(CellUtil.cloneFamily(cell))+" ");
System.out.println("row Name:"+new String(CellUtil.cloneValue(cell))+" ");
System.out.println("value"+new String(CellUtil.cloneValue(cell))+" "); } }
public static void close() { try {
if (admin != null) {
admin.close();
}
if(null!=connection) {
connection.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }

Java_Habse_add的更多相关文章

随机推荐

  1. Appnium 环境搭建

    NodeJs 下载安装 npm install -g appium-doctor Java JDK jdk-8u241-windows-x64 添加环境变量:JAVA_HOME 在环境变量Path中添 ...

  2. asp.net mvc获取http body中的json

    今天遇到一个对方直接post 一共json对象字符串过来的场景.按照以前的旧方案方法可以直接在request.inputstream里面读取,然后反序列化即可.(当然估计ASP.NET MVC应该有更 ...

  3. Apache 安装概要

    1.apache下载参照百度 bin文件夹下命令行:  httpd -k install 2.安装完成后排错记录 服务无法启动,到bin目录下运行  httpd.exe  查看输出,然后百度一下输出即 ...

  4. Redis 要学的

    https://www.cnblogs.com/kismetv/p/8654978.html#t21 各个类型底层原理 慢查询 pipeline BitMaps 发布订阅 主从复制 psync psy ...

  5. 松软科技课堂:jQuery 语法

    jQuery 语法 jQuery 语法是为 HTML 元素的选取编制的,可以对元素执行某些操作. 基础语法是:$(selector).action() 美元符号定义 jQuery 选择符(select ...

  6. 【译】写个好的 CLI 程序

    写个好的 CLI 程序 Write a Good CLI Program 译文 原文链接:https://qiita.com/tigercosmos/items/678f39b1209e60843cc ...

  7. Apache Kafka(五)- Safe Kafka Producer

    Kafka Safe Producer 在应用Kafka的场景中,需要考虑到在异常发生时(如网络异常),被发送的消息有可能会出现丢失.乱序.以及重复消息. 对于这些情况,我们可以创建一个“safe p ...

  8. Winform 随机抽奖小程序

    效果图: 主要代码: Form1.cs using System; using System.Drawing; using System.IO; using System.Runtime.Intero ...

  9. 转 C#中哈希表(HashTable)的用法详解

    看了一遍有关哈希表的文字,作者总结的真是不错 .收藏起来 1.  哈希表(HashTable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提 ...

  10. 用python实现网络文件共享

    第一步:打开命令行 第二步:切换到要共享的文件夹目录 第三步:运行命令 python -m http.server 端口号 然后在浏览器输入显示的路径即可访问文件夹下的文件,点击即可下载,手机端亦可访 ...