Apache Phoenix基本操作-1
本篇我们将介绍phoenix的一些基本操作。
1. 如何使用Phoenix输出Hello World?
1.1 使用sqlline终端命令
sqlline.py SZB-L0023780:2181:/hbase114
0:jdbc:phoenix:SZB-L0023780:2181:/hbase114> create table test (mykey integernot null primary key, mycolumn varchar);
0:jdbc:phoenix:SZB-L0023780:2181:/hbase114> upsert into test values(1,'Hello');
0:jdbc:phoenix:SZB-L0023780:2181:/hbase114> upsert into test values(2,'World!');
0:jdbc:phoenix:SZB-L0023780:2181:/hbase114> select * from test;
+--------------+---------------------+
| MYKEY | MYCOLUMN |
+--------------+---------------------+
| 1 |Hello |
| 2 | World! |
+---------------+---------------------+
1.2 使用Java方式访问
创建test.java文件,内容如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class test2 {
public static void main(String[] args) throws SQLException {
Statement stmt = null;
ResultSet rset = null;
Connection con = DriverManager.getConnection("jdbc:phoenix:SZB-L0023780:2181:/hbase114");
stmt= con.createStatement();
stmt.executeUpdate("create table test2 (mykey integer not null primary key, mycolumn varchar)");
stmt.executeUpdate("upsert into test2 values (1,'Hello')");
stmt.executeUpdate("upsert into test2 values (2,'World!')");
con.commit();
PreparedStatement statement = con.prepareStatement("select * from test2");
rset= statement.executeQuery();
while(rset.next()) {
System.out.println(rset.getString("mycolumn"));
}
statement.close();
con.close();
}
}
编译:
javac test2.java
执行编译好的程序:
java -cp"../phoenix-4.8.0-Hbase-1.1-client.jar:." test2
输出结果:
Hello
World!
2. 如何通过Phoenix批量加载数据
Phoenix提供了两种方法用来加载CSV数据到Phoenix 表中,一种是通过psql命令,单线程方式加载;另一种是基于MapReduce批量加载方式。
psql方式适合几十MB的数据量,而基于MapReduce的方式适合更大的数据量加载。
下面我们来演示一下通过这两种方式加载CSV格式的数据到Phoenix表中。
(1)样例数据data.csv
12345,John,Doe
67890,Mary,Poppins
(2)创建表SQL
CREATE TABLE example (
my_pk bigint not null,
m.first_name varchar(50),
m.last_name varchar(50)
CONSTRAINT pk PRIMARY KEY(my_pk)
);
(3)通过psql方式加载
bin/psql.py -t EXAMPLE SZB-L0023780:2181:/hbase114 data.csv
psql.py使用的示例如下:
Examples:
psql my_ddl.sql
psql localhost my_ddl.sql
psql localhost my_ddl.sql my_table.csv
psql -t MY_TABLE my_cluster:1825 my_table2012-Q3.csv
psql -t MY_TABLE -h COL1,COL2,COL3 my_cluster:1825 my_table2012-Q3.csv
psql -t MY_TABLE -h COL1,COL2,COL3 -d : my_cluster:1825 my_table2012-Q3.csv
下面将一些参数说明一下:
Parameter |
Description |
-t |
加载数据的表名,默认为CSV文件名称,大小写敏感 |
-h |
Overrides the column names to which the CSV data maps and is case sensitive. A special value of in-line indicating that the first line of the CSV file determines the column to which the data maps. |
-s |
Run in strict mode, throwing an error on CSV parsing errors |
-d |
Supply a custom delimiter or delimiters for CSV parsing |
-q |
Supply a custom phrase delimiter, defaults to double quote character |
-e |
Supply a custom escape character, default is a backslash |
-a |
Supply an array delimiter (explained in more detail below) |
(4)通过MapReduce来加载数据
对于分布式集群更高吞吐量数据加载,建议使用MapReduce加载方式。这种方式首先将数据写入HFile中,等HFile创建好之后就写入到hbase表中。
MapReduce加载器是使用Hadoop命令,然后借助Phoenix的Client的Jar实现的,如下:
hadoop jar phoenix-<version>-client.jar org.apache.phoenix.mapreduce.CsvBulkLoadTool--table EXAMPLE --input /data/example.csv
这里需要注意的是,输入的文件必须是HDFS上的文件,不是本地文件系统上的。
比如我在环境里面执行如下;
hadoop jar phoenix-4.8.0-HBase-1.1-client.jarorg.apache.phoenix.mapreduce.CsvBulkLoadTool --table EXAMPLE --input /okok/data.csv-z SZB-L0023780:2181:/hbase114
执行部分日志如下:
mapreduce.AbstractBulkLoadTool: LoadingHFiles from /tmp/94b60a06-86d8-49d7-a8d1-df5428971a33
mapreduce.AbstractBulkLoadTool: LoadingHFiles for EXAMPLE from /tmp/94b60a06-86d8-49d7-a8d1-df5428971a33/EXAMPLE
mapreduce.LoadIncrementalHFiles: Trying toloadhfile=hdfs://SZB-L0023776:8020/tmp/94b60a06-86d8-49d7-a8d1-df5428971a33/EXAMPLE/M/b456b2a2a5834b32aa8fb3463d3bfd76first=\x80\x00\x00\x00\x00\x0009 last=\x80\x00\x00\x00\x00\x01\x092
下面我们将MapReduce加载器常用的参数罗列一下:
Parameter |
Description |
-i,–input |
Input CSV path (mandatory) |
-t,–table |
Phoenix table name (mandatory) |
-a,–array-delimiter |
Array element delimiter (optional) |
-c,–import-columns |
Comma-separated list of columns to be imported |
-d,–delimiter |
Input delimiter, defaults to comma |
-g,–ignore-errors |
Ignore input errors |
-o,–output |
Output path for temporary HFiles (optional) |
-s,–schema |
Phoenix schema name (optional) |
-z,–zookeeper |
Zookeeper quorum to connect to (optional) |
-it,–index-table |
Index table name to load (optional) |
注:
psql.py这种方式典型的upsert效率为每秒20k-50k行(依赖每行的大小)。
使用方法如下:
使用psql创建表:
psql.py [zookeeper] ../examples/web_stat.sql
使用psql批量upsert CSV格式的数据:
psql.py [zookeeper] ../examples/web_stat.csv
Apache Phoenix基本操作-1的更多相关文章
- Apache Phoenix基本操作-2
1. 如何映射一个Phoenix的表到一个Hbase的表? 你可以通过Create table/create view DDL语句在一个已经存在的hbase表上创建一个Phoenix表或者视图.对于C ...
- [saiku] 使用 Apache Phoenix and HBase 结合 saiku 做大数据查询分析
saiku不仅可以对传统的RDBMS里面的数据做OLAP分析,还可以对Nosql数据库如Hbase做统计分析. 本文简单介绍下一个使用saiku去查询分析hbase数据的例子. 1.phoenix和h ...
- Apache Phoenix JDBC 驱动和Spring JDBCTemplate的集成
介绍:Phoenix查询引擎会将SQL查询转换为一个或多个HBase scan,并编排运行以生成标准的JDBC结果集. 直接使用HBase API.协同处理器与自己定义过滤器.对于简单查询来说,其性能 ...
- phoenix 报错:type org.apache.phoenix.schema.types.PhoenixArray is not supported
今天用phoenix报如下错误: 主要原因: hbase的表中某字段类型是array,phoenix目前不支持此类型 解决方法: 复制替换phoenix包的cursor文件 # Copyright 2 ...
- Mapreduce atop Apache Phoenix (ScanPlan 初探)
利用Mapreduce/hive查询Phoenix数据时如何划分partition? PhoenixInputFormat的源码一看便知: public List<InputSplit> ...
- org.apache.phoenix.exception.PhoenixIOException: SYSTEM:CATALOG
Error: SYSTEM:CATALOG (state=08000,code=101)org.apache.phoenix.exception.PhoenixIOException: SYSTEM: ...
- phoenix连接hbase数据库,创建二级索引报错:Error: org.apache.phoenix.exception.PhoenixIOException: Failed after attempts=36, exceptions: Tue Mar 06 10:32:02 CST 2018, null, java.net.SocketTimeoutException: callTimeou
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...
- apache phoenix 安装试用
备注: 本次安装是在hbase docker 镜像的基础上配置的,主要是为了方便学习,而hbase搭建有觉得 有点费事,用镜像简单. 1. hbase 镜像 docker pull har ...
- How to use DBVisualizer to connect to Hbase using Apache Phoenix
How to use DBVisualizer to connect to Hbase using Apache Phoenix Article DB Visualizer is a popular ...
随机推荐
- Mysql InnoDB表结构
索引组织表 在InnoDB存储引擎中,表都是根据主键顺序组织存放的,这种存储方式的表称为索引组织表(index organized table).在InnoDB存储引擎表中,每张表都有个主键(Prim ...
- LeetCode Problem 9:Palindrome Number回文数
描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- SharePoint解决方案及开发系列(2)-ECM
很多次跟客户做咨询及沟通的时候,客户都问SharePoint能做什么?是不是就是做文档管理?为什么要花那么多的钱没SharePoint?高大上? 我上家公司面试的时候,我的那个BOSS面试官有一个问题 ...
- 如何自己实现一套EasyNVR这样的无插件流媒体服务器
EasyNVR流媒体解决方案 EasyNVR能够通过简单的网络摄像机通道配置,将传统监控行业里面的高清网络摄像机IP Camera.NVR等具有RTSP协议输出的设备接入到EasyNVR,EasyNV ...
- 前台传递给后台的JSON字符串中的引号 “” 在JAVA后台被转义为 "
前台传递给后台的JSON字符串中的引号 "" 在JAVA后台被转义为 " 1.问题: 前台数据,JSON字符串带有引号 "" ,数据被传递到后台 ...
- 《挑战程序设计竞赛》2.1 广度优先搜索 AOJ0558 POJ3669 AOJ0121
AOJ0558 原文链接: AOJ0558 题意: 在H * W的地图上有N个奶酪工厂,分别生产硬度为1-N的奶酪.有一只吃货老鼠准备从老鼠洞出发吃遍每一个工厂的奶酪.老鼠有一个体力值,初始时为1,每 ...
- python系列四:Python3字符串
#!/usr/bin/python #Python3 字符串#可以截取字符串的一部分并与其他字段拼接var1 = 'Hello World!'print ("已更新字符串 : ", ...
- 使用jquery获取单选按钮radio的值
<input type="radio" name="gender" value="男" >男</input> < ...
- JS基础知识简介
使用js的三种方式 1.HTML标签内嵌js <button onclick="javascript:alert(真点啊)">有本事点我</button> ...
- 如何使用 stl 进行排列组合?
#include <iostream> #include <vector> #include <algorithm> //从 indexs 集合中选择 num 个元 ...