hbase使用MapReduce操作1(基本增删改查)
操作代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes; import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List; public class Main {
public static Configuration conf; static {
//使用 HBaseConfiguration 的单例方法实例化
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "master,node1,node2");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.master", "master:60000");
} public static void dropTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
if (isTableExist(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println("表" + tableName + "删除成功!");
} else {
System.out.println("表" + tableName + "不存在!");
}
} public static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException {
//创建 HTable 对象
HTable hTable = new HTable(conf, tableName);
//向表中插入数据
Put put = new Put(Bytes.toBytes(rowKey));
//向 Put 对象中组装数据
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
hTable.put(put);
hTable.close();
System.out.println("插入数据成功");
} public static void deleteMultiRow(String tableName, String... rows) throws IOException {
HTable hTable = new HTable(conf, tableName);
List<Delete> deleteList = new ArrayList<Delete>();
for (String row : rows) {
Delete delete = new Delete(Bytes.toBytes(row));
deleteList.add(delete);
} hTable.delete(deleteList);
hTable.close();
} public static void getAllRows(String tableName) throws IOException {
HTable hTable = new HTable(conf, tableName);
//得到用于扫描 region 的对象
Scan scan = new Scan();
//使用 HTable 得到 resultcanner 实现类的对象
ResultScanner resultScanner = hTable.getScanner(scan);
for (Result result : resultScanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
//得到 rowkey
System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));
//得到列族
System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
} public static void getRow(String tableName, String rowKey) throws IOException {
HTable table = new HTable(conf, tableName);
Get get = new Get(Bytes.toBytes(rowKey));
//get.setMaxVersions();显示所有版本
//get.setTimeStamp();显示指定时间戳的版本
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println("行键:" + Bytes.toString(result.getRow()));
System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("时间戳:" + cell.getTimestamp());
}
} public static void getRowQualifier(String tableName, String rowKey, String family, String qualifier) throws IOException {
HTable table = new HTable(conf, tableName);
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println("行键:" + Bytes.toString(result.getRow()));
System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
} public void addAFamily(String tableName, String familyName, boolean isAdd) throws IOException {
TableName tablename = TableName.valueOf(tableName);
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(tablename);
HTableDescriptor hDescriptor = admin.getTableDescriptor(tablename);
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(familyName);
if (isAdd) {
hDescriptor.addFamily(hColumnDescriptor);
} else {
hDescriptor.removeFamily(Bytes.toBytes(familyName));
}
admin.modifyTable(tablename, hDescriptor);
admin.enableTable(tablename);
} public ArrayList<String> readFile() throws Exception {
ArrayList<String> users = new ArrayList<String>();
File file = new File("F:/Hadoop/data_format/test1.csv");
InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
BufferedReader br = new BufferedReader(isr);
String s = "";
//创建 HTable 对象
HTable hTable = new HTable(conf, "test_data");
while ((s = br.readLine()) != null) {
String[] strs = s.split(",");
//向表中插入数据
Put put = new Put(Bytes.toBytes("rowKey"));
//向 Put 对象中组装数据
put.add(Bytes.toBytes("columnFamily"), Bytes.toBytes("column"), Bytes.toBytes("value"));
hTable.put(put);
}
hTable.close();
System.out.println("插入数据成功");
return users;
} public static void main(String args[]) throws IOException {
//System.out.println(isTableExist("student"));
//createTable("student2",new String[]{"info1","info2"});
//dropTable("student2");
//addRowData("student","1003","info","name","zhangy");
//deleteMultiRow("student",new String[]{"1002","1003"});
//getAllRows("student");
//getRow("student","1002");
getRowQualifier("student", "1001", "info", "name");
} public static boolean isTableExist(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
//在 HBase 中管理、访问表需要先创建 HBaseAdmin 对象
Connection connection = ConnectionFactory.createConnection(conf); //HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
HBaseAdmin admin = new HBaseAdmin(conf);
return admin.tableExists(tableName);
} public static void createTable(String tableName, String... columnFamily) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
HBaseAdmin admin;
admin = new HBaseAdmin(conf);
//判断表是否存在
if (isTableExist(tableName)) {
System.out.println("表" + tableName + "已存在");
System.exit(0);
} else {
//创建表属性对象,表名需要转字节
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
//创建多个列族
for (String cf : columnFamily) {
descriptor.addFamily(new HColumnDescriptor(cf));
}
//根据对表的配置,创建表
admin.createTable(descriptor);
System.out.println("表" + tableName + "创建成功!");
}
}
}
hbase使用MapReduce操作1(基本增删改查)的更多相关文章
- C#操作Excel数据增删改查(转)
C#操作Excel数据增删改查. 首先创建ExcelDB.xlsx文件,并添加两张工作表. 工作表1: UserInfo表,字段:UserId.UserName.Age.Address.CreateT ...
- C#操作Excel数据增删改查示例
Excel数据增删改查我们可以使用c#进行操作,首先创建ExcelDB.xlsx文件,并添加两张工作表,接下按照下面的操作步骤即可 C#操作Excel数据增删改查. 首先创建ExcelDB.xlsx文 ...
- sqlite数据库操作详细介绍 增删改查,游标
sqlite数据库操作详细介绍 增删改查,游标 本文来源于www.ifyao.com禁止转载!www.ifyao.com Source code package com.example ...
- java操作数据库:增删改查
不多bb了直接上. 工具:myeclipse 2016,mysql 5.7 目的:java操作数据库增删改查商品信息 test数据库的goods表 gid主键,自增 1.实体类Goods:封装数据库数 ...
- python操作三大主流数据库(8)python操作mongodb数据库②python使用pymongo操作mongodb的增删改查
python操作mongodb数据库②python使用pymongo操作mongodb的增删改查 文档http://api.mongodb.com/python/current/api/index.h ...
- python操作mysql数据库增删改查的dbutils实例
python操作mysql数据库增删改查的dbutils实例 # 数据库配置文件 # cat gconf.py #encoding=utf-8 import json # json里面的字典不能用单引 ...
- Asp.Net操作MySql数据库增删改查
Asp.Net操作MySql数据库增删改查,话不多说直接步入正题.git源码地址:https://git.oschina.net/gxiaopan/NetMySql.git 1.安装MySQL数据库 ...
- SpringBoot操作MongoDB实现增删改查
本篇博客主讲如何使用SpringBoot操作MongoDB. SpringBoot操作MongoDB实现增删改查 (1)pom.xml引入依赖 <dependency> <group ...
- Java数据库连接——JDBC基础知识(操作数据库:增删改查)
一.JDBC简介 JDBC是连接java应用程序和数据库之间的桥梁. 什么是JDBC? Java语言访问数据库的一种规范,是一套API. JDBC (Java Database Connectivit ...
- android使用xfire webservice框架远程对sqlserver操作(包括增删改查)的实例!!已在真机上试验通过
前两天,公司有一个利用android远程操作sqlserver的项目,对此我是毫无头绪的,但也挺感兴趣的,于是开始上网搜索方法,网上有挺多方法了,发现使用webservice的挺多的,不过我对这些技术 ...
随机推荐
- 如何写一个自定义的js文件
自定义一个Utils.js文件,在其中写js代码即可.如: (function(w){ function Utils(){} Utils.prototype.getChilds = function( ...
- Fiddler 捕获 nodejs 模拟的http请求
1.设置Fiddler Tools->Options-> Connections Allow remote computers to connect: 2.nodejs 请求有多种 2.1 ...
- Dubbo简单理解
Dubbo 致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,Dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有Dubbo这样 ...
- Ubuntu中解决机箱前置耳机没声音
Ubuntu中解决机箱前置耳机没声音 安装pavucontrol软件: sudo apt-get install pavucontrol 然后直接运行pavucontrol打开软件: 将输出设备设置为 ...
- np.identity()
二.np.identity()这个函数和之前的区别在于,这个只能创建方阵,也就是N=M 函数的原型:np.identity(n,dtype=None) 参数:n,int型表示的是输出的矩阵的行数和列数 ...
- 两台Linux之间传文件
安装sudo apt-get install openssh-client openssh-server 使用scp命令: scp john@~/hallo.h /usr/include 将左边移动到 ...
- 安装完CentOS可以不做的事
添加用户到sudo. 打开/etc/sudoers 找到root ALL=(ALL) ALL这一行,在后面再加上一行就可以了(不用引号): username ALL=(ALL) ALL 注意,都用ta ...
- 动态输出的javascript中alert文本的换行问题
这个简单<%out.println("<script>alert('姓名:xx\\n性别:女\\n爱好:吃\\n')</script>");%> ...
- MongoDB的数据类型(四)
JSON JSON是一种简单的数据表示方式,它易于理解.易于解析.易于记忆.但从另一方面来说,因为只有null.布尔.数字.字符串.数组和对象这几种数据类型,所以JSON有一定局限性.例如,JSON没 ...
- jQuery中animate()对Firefox无效的解决办法
在使用 animate()做返回顶部的动画时,会出现对Firefox无效的情况,如: $('body').animate({scrollTop:'0'},500); 它对Chrome,IE,Opera ...