前端技术
HTML
CSS
Javascript
前端框架和UI库
VUE
ReactJS
AngularJS
JQuery
NodeJS
JSON
Element-UI
Bootstrap
Material UI
服务端和客户端
Java
Python
PHP
Golang
Scala
Kotlin
Groovy
Ruby
Lua
.net
c#
c++
后端WEB和工程框架
SpringBoot
SpringCloud
Struts2
MyBatis
Hibernate
Tornado
Beego
Go-Spring
Go Gin
Go Iris
Dubbo
HessianRPC
Maven
Gradle
数据库
MySQL
Oracle
Mongo
中间件与web容器
Redis
MemCache
Etcd
Cassandra
Kafka
RabbitMQ
RocketMQ
ActiveMQ
Nacos
Consul
Tomcat
Nginx
Netty
大数据技术
Hive
Impala
ClickHouse
DorisDB
Greenplum
PostgreSQL
HBase
Kylin
Hadoop
Apache Pig
ZooKeeper
SeaTunnel
Sqoop
Datax
Flink
Spark
Mahout
数据搜索与日志
ElasticSearch
Apache Lucene
Apache Solr
Kibana
Logstash
数据可视化与OLAP
Apache Atlas
Superset
Saiku
Tesseract
系统与容器
Linux
Shell
Docker
Kubernetes
[数据库表唯一标识符生成方法]的搜索结果
这里是文章列表。热门标签的颜色随机变换,标签颜色没有特殊含义。
点击某个标签可搜索标签相关的文章。
点击某个标签可搜索标签相关的文章。
Beego
...就比如说,有时候需要生成独一无二的UUID,或者来个自增ID啥的。今天,我们就来聊聊在Beego中如何解决这些问题。 一、UUID生成 在分布式系统中,我们常常需要生成全局唯一的标识符,也就是我们常说的UUID。UUID是一个128位的数字,可以用来表示一个特定的对象。在Go语言中,我们可以使用标准库中的math/rand包和time包来生成UUID。 go import ( "crypto/rand" "encoding/hex" "math/big" "time" ) func NewUUID() string { var b [16]byte _, err := rand.Read(b[:]) if err != nil { panic(err) } now := time.Now().UnixNano() b[6] = byte((now >> 40) & 0xf) b[7] = byte(now >> 32) b[8] = byte(now >> 24) b[9] = byte(now >> 16) b[10] = byte(now >> 8) b[11] = byte(now) return hex.EncodeToString(b[:]) } 二、自增ID生成 自增ID是一种常见的数据库主键生成方式,它通过不断增加一个整数值来保证数据的唯一性。在Beego这个框架里头,如果你想实现自动增长ID的功能,完全可以这样做:先定义一个模型,然后在这个模型里头添加一个类型为uint的ID字段,这就搞定了自增ID的需求。就像是给每一条记录分配一个独一无二的数字身份证一样,每次新增记录时,这个ID会自动加一,省去了手动指定ID的麻烦。 go type User struct { ID uint orm:"column(id);auto" Name string Email string Phone string Address string } 以上代码中,我们在User模型中定义了一个名为ID的字段,并设置了它的类型为uint和auto。这样,每次插入一条新的用户记录时,ID字段都会自动递增。 三、UUID和自增ID的选择 在实际开发中,我们常常需要根据具体的需求来选择生成哪种类型的ID。如果我们正在捣鼓一个分布式系统,那么选用UUID绝对是个更酷的选择。为啥呢?因为它可以在全球这个大舞台上保证每个ID都是独一无二的,就像每个人都有自己的指纹一样独特。假如我们正在捣鼓一个单机应用,那么选择自增ID可能是个更省心省力的办法。为啥呢?因为它生成的速度贼快,而且出岔子的概率也低得多,这样一来,我们就不用在这方面费太多心思啦! 四、总结 总的来说,生成UUID或自增ID是我们在开发Web应用时经常会遇到的问题。在Beego中,我们可以通过简单的代码就能实现这两种ID的生成。不过呢,具体要用哪种类型的ID,咱们还得根据实际需求来掂量决定。无论我们挑哪一个,只要能把数据的唯一性和安全性稳稳地守住,那就都是个没毛病的选择。
2023-11-17 22:27:26
589
翡翠梦境-t
Hadoop
一、引言 在大数据处理领域中,Hadoop是一个非常重要的工具。这个东西提供了一种超赞的分布式计算模式,能够帮我们轻轻松松地应对和处理那些海量数据,让管理起来不再头疼。不过呢,就像其他那些软件兄弟一样,Hadoop这家伙有时候也会闹点小情绪,其中一个常见的问题就是数据写入会重复发生。 在本文中,我们将深入探讨什么是数据写入重复,为什么会在Hadoop中发生,并提供几种解决这个问题的方法。这将包括详细的代码示例和解释。 二、什么是数据写入重复? 数据写入重复是指在一个数据库或其他存储系统中,同一个数据项被多次写入的情况。这可能会导致许多问题,例如: 1. 数据一致性问题 如果一个数据项被多次写入,那么它的最终状态可能并不明确。 2. 空间浪费 重复的数据会占用额外的空间,尤其是在大数据环境中,这可能会成为一个严重的问题。 3. 性能影响 当数据库或其他存储系统尝试处理大量重复的数据时,其性能可能会受到影响。 三、为什么会在Hadoop中发生数据写入重复? 在Hadoop中,数据写入重复通常发生在MapReduce任务中。这是因为MapReduce是个超级厉害的并行处理工具,它能够同时派出多个“小分队”去处理不同的数据块,就像是大家一起动手,各自负责一块儿,效率贼高。有时候,这些家伙可能会干出同样的活儿,然后把结果一股脑地塞进同一个文件里。 此外,数据写入重复也可能是由于其他原因引起的,例如错误的数据输入、网络故障等。 四、如何避免和解决数据写入重复? 以下是一些可以用来避免和解决数据写入重复的方法: 1. 使用ID生成器 当写入数据时,可以使用一个唯一的ID来标识每个数据项。这样就可以确保每个数据项只被写入一次。 python import uuid 生成唯一ID id = str(uuid.uuid4()) 2. 使用事务 在某些情况下,可以使用数据库事务来确保数据的一致性。这可以通过设置数据库的隔离级别来实现。 sql START TRANSACTION; INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2'); COMMIT; 3. 使用MapReduce的输出去重特性 Hadoop提供了MapReduce的输出去重特性,可以在Map阶段就去除重复的数据,然后再进行Reduce操作。 java public static class MyMapper extends Mapper { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] words = value.toString().split(" "); for (String word : words) { word = word.toLowerCase(); if (!word.isEmpty()) { context.write(new Text(word), one); } } } } 以上就是关于Hadoop中的数据写入重复的一些介绍和解决方案。希望对你有所帮助。
2023-05-18 08:48:57
506
秋水共长天一色-t
转载文章
在数据库管理系统中,自增主键的管理与维护是一项常见且关键的任务。MySQL作为广泛使用的开源关系型数据库,其AUTO_INCREMENT特性为表的主键提供了自动递增的功能,但在特定场景下,如遇到唯一键冲突时可能导致自增ID不连续的问题。近期,针对这一问题,有数据库专家和开发者们展开了深入探讨。 实际上,MySQL官方社区以及相关技术博客对此类问题已有多种解决方案提出。例如,除了文中提及的在每次插入操作后动态调整AUTO_INCREMENT值的方法外,还有一种观点是通过重构数据库设计,将自增ID与业务逻辑解耦,采用UUID或其他全局唯一标识符替代自增主键,以减少对连续性的依赖。同时,随着MySQL 8.0版本的发布,新增了序列(SEQUENCE)对象,提供了一种更为灵活的方式来生成唯一的序列号,可用于解决自增主键不连续的问题。 此外,在数据库优化方面,对于高并发环境下的插入操作,如何确保自增主键的连续性和唯一性变得更加复杂。一些大型互联网公司采用了分布式ID生成策略,如雪花算法(Snowflake),能够在分布式环境下实现高效且有序的ID生成,从而避免因单点故障或并发写入导致的自增主键断层。 值得注意的是,无论采取何种解决方案,都需要根据实际应用场景、数据量大小、并发访问量及性能需求等因素综合考虑。同时,理解并遵循数据库设计范式,合理规划表结构,也有助于从根本上减少此类问题的发生。总之,面对MySQL或其他数据库系统中的自增主键连续性挑战,持续关注最新的数据库技术和最佳实践,结合自身项目特点选择最优方案,才能确保系统的稳定、高效运行。
2023-08-26 08:19:54
92
转载
Python
...洁高效。此外,对于大数据处理或科学计算场景,NumPy库提供的ndarray对象在性能上远超Python原生列表,可以实现快速的矩阵运算和统计分析。 近期,一篇发布于“Real Python”网站的文章深入探讨了如何利用列表推导式(List Comprehensions)和生成器表达式(Generator Expressions)对列表进行复杂操作,如过滤、映射和压缩数据,从而提升代码可读性和运行效率。文章还介绍了functools模块中的reduce函数,用于对列表元素执行累积操作,如求乘积、求序列中最长连续子序列等。 另外,在实际编程实践中,掌握列表的排序、切片、连接、复制等基本操作同样至关重要。例如,使用sorted()函数或列表的sort()方法对列表进行排序;利用切片技术实现列表的部分提取或替换;通过extend()和+运算符完成列表合并等。这些操作不仅能丰富你对Python列表的理解,更能在日常开发任务中助你事半功倍。 总的来说,深入学习和熟练运用Python列表的各种特性与功能,不仅有助于数据分析和处理,更能提升代码编写质量,使程序更加简洁、高效。同时,关注Python社区的最新动态和最佳实践,将能持续拓展你的编程技能边界,紧跟时代发展步伐。
2023-10-05 18:16:18
359
算法侠
PostgreSQL
在数据库管理系统中,序列生成器是一个关键功能,尤其对于需要唯一标识符的应用场景,如交易流水号、用户ID等。PostgreSQL的序列生成器功能强大且灵活,但在实际应用中,开发者还应考虑其并发环境下的性能和安全性问题。 近期,PostgreSQL官方社区发布了一篇深度技术文章,针对高并发场景下如何优化序列生成器的使用进行了探讨。文中指出,在多线程或多进程环境下,虽然序列生成器能确保生成的数字唯一,但如果不采取适当的并发控制策略,可能会导致序列号之间的间隙增大或序列生成效率降低。为此,建议采用“缓存”策略(例如通过设置CACHE大小),预先生成一组序列号,从而减少对序列对象的争用,提高并发性能。 此外,对于分布式系统中的全局唯一序列号生成需求,PostgreSQL提供的逻辑复制功能可以与序列生成器结合,实现跨多个数据库节点的全局唯一序列号分配。但这一过程涉及更复杂的架构设计与配置,开发者需深入理解并合理运用。 综上所述,尽管PostgreSQL的序列生成器为开发者提供了便利,但在实际应用时还需根据具体业务场景进行针对性优化,并时刻关注社区发布的最新技术动态,以便更好地利用数据库特性,提升系统的稳定性和性能。
2023-04-25 22:21:14
77
半夏微凉-t
Redis
...够记录用户阅读状态的数据库。 二、设计思路 要实现这个功能,我们可以利用Redis这种键值对存储的数据库来存储用户的阅读状态。我们可以把每篇文章看作一个键,而用户的阅读状态则可以看作一个值。当有用户点开一篇文章瞧瞧的时候,我们就能通过查这个小标签的记录,轻松判断出这位用户是不是已经拜读过这篇文章啦。 三、具体实现 接下来我们将详细介绍如何使用Redis实现这个功能。首先,我们需要创建一个新的键值对存储表,并且为每个文章创建一个键。比如,假设有这么一个叫做“news”的文章列表,我们完全可以给列表里的每一篇文章都创建一个独特的标签,就像这样子:“news:article1”,“news:article2”等等,就像是给每篇文章起了个专属的小名儿一样。 然后,我们需要为用户创建一个键,用于存储他们的阅读状态。例如,我们可以为每个用户创建一个名为"user:uid:read_status"的键,其中"uid"是用户的唯一标识符。 当用户访问一篇文章时,我们可以通过查询"news:articleX"这个键的值来获取文章的阅读状态。如果这个键的值为空,则表示用户还未阅读过这篇文章。反之,如果这个键的值不为空,则表示用户已经阅读过这篇文章。 接下来,我们可以通过修改"news:articleX"这个键的值来更新文章的阅读状态。比如,当咱发现有用户已经阅读过某篇文章了,咱们就可以把这篇文章对应的键值标记为"true",就像在小本本上做个记号一样。换种说法,假如我们发现用户还没读过某篇文章呢,那咱们就可以干脆把这篇文章对应的键的值清空掉,让它变成空空如也。 四、代码示例 下面是一个使用Python实现的简单示例: python import redis 创建Redis客户端对象 r = redis.Redis(host='localhost', port=6379, db=0) 获取文章的阅读状态 def get_article_read_status(article_id): key = f'news:{article_id}:read_status' return r.get(key) is not None 更新文章的阅读状态 def set_article_read_status(article_id, read_status): key = f'news:{article_id}:read_status' if read_status: r.set(key, 'true') else: r.delete(key) 五、总结 通过上述介绍,我们可以看到,使用Redis作为阅读状态数据库是一种非常可行的方法。它可以方便地存储和管理用户的阅读状态,而且因为Redis的特性,它的性能非常高,可以很好地应对高并发的情况。 当然,这只是一个基本的设计方案,实际的应用可能还需要考虑更多的因素,例如安全性、稳定性、可扩展性等等。不管咋说,Redis这款数据库工具真心值得我给你安利一波。它可是能实实在在地帮我们简化开发过程,这样一来,咱就能把更多的心思和精力花在琢磨业务逻辑上,让工作更加高效流畅。
2023-06-24 14:53:48
332
岁月静好_t
Beego
...本原理是通过HTTP方法(GET, POST, PUT, DELETE)来对资源进行操作。这种设计风格使得API更易理解和使用。 三、Beego支持的特性 Beego不仅支持RESTful API的基本功能,还提供了一些额外的特性。比如,它有一个超级给力的路由机制,能妥妥地应对各种曲折复杂的URL路径;而且人家还特别贴心地支持数据库操作,让你轻轻松松就能把数据存到MySQL或者MongoDB这些数据库里去。 四、设计原则 以下是使用Beego开发RESTful API的一些设计原则: 1. 保持简单 RESTful API应该是简单的,易于理解和使用的。这意味着应该尽可能减少API的复杂性,并遵循RESTful API的设计原则。 2. 明确的状态 每一个HTTP请求都应该返回一个明确的状态。比如,假设你请求一个东西,如果这个请求一切顺利,就相当于你得到了一个“YES”,这时候,服务器会给你回个HTTP状态码200,表示“妥了,兄弟,你的请求我成功处理了”。而要是请求出岔子了,那就等于收到了一个“NO”,这时候,服务器可能会甩给你一个400或者500的HTTP状态码,意思是:“哎呀,老铁,你的请求有点问题,不是格式不对(400),就是服务器这边内部出了状况(500)。” 3. 使用标准的HTTP方法 HTTP定义了8种方法,包括GET, POST, PUT, DELETE, HEAD, OPTIONS, CONNECT和TRACE。应该始终使用这些方法,而不是自定义的方法。 4. 使用URI来表示资源 URI是统一资源标识符,它是唯一标识资源的方式。应该使用URI来表示资源,而不是使用ID或其他非唯一的标识符。 5. 使用HTTP头部信息 HTTP头部信息可以提供关于请求或响应的附加信息。应该尽可能使用HTTP头部信息来提高API的功能性。 6. 返回适当的格式 应该根据客户端的需求返回适当的数据格式,例如JSON或XML。 五、示例代码 以下是一个使用Beego创建RESTful API的简单示例: go package main import ( "github.com/astaxie/beego" ) type User struct { Id int json:"id" Name string json:"name" Email string json:"email" } func main() { beego.Router("/users/:id", &UserController{}) beego.Run() } type UserController struct{} func (u UserController) Get(ctx beego.Controller) { id := ctx.Params.Int(":id") user := &User{Id: id, Name: "John Doe", Email: "john.doe@example.com"} ctx.JSON(200, user) } 在这个示例中,我们首先导入了beego包,然后定义了一个User结构体。然后我们在main函数中设置了路由,当收到GET /users/:id请求时,调用UserController的Get方法。 在Get方法中,我们从URL参数中获取用户ID,然后创建一个新的User对象,并将其转换为JSON格式,最后返回给客户端。 这就是使用Beego创建RESTful API的一个简单示例。当然,这只是一个基础的例子,实际的API可能会更复杂。不过呢,只要你按照上面提到的设计原则来,就能轻轻松松地设计出既高效又超级好用的RESTful API,保证让你省心省力。
2023-08-12 16:38:17
511
风轻云淡-t
.net
...计模式,用于在关系型数据库系统与面向对象编程语言之间建立桥梁。在.NET开发中提及的EF Core就是一个ORM框架实例,它允许开发者以操作对象的方式来操作数据库,将数据库表映射为类,SQL查询转换为 LINQ 表达式或方法调用,从而极大地简化数据访问层的开发工作,并提高代码可读性和复用性。 参数化SQL , 参数化SQL是在执行SQL语句时,将变量或用户输入的数据作为参数传递给SQL命令的方式。这样可以有效防止SQL注入攻击,并确保SQL语句的正确编译和执行。例如,在文章中的SqlHelper类中,通过SqlCommand.Parameters.AddRange(parameters)方法来绑定参数,确保插入、更新或删除数据时SQL语句的安全性和准确性。 主键约束 , 主键约束是关系型数据库中的一种完整性约束,用于唯一标识数据库表中的每一条记录。在创建表结构时,通常会指定一个或多个字段为主键,这些字段的值必须在全表范围内保持唯一。当尝试插入已存在主键值的数据时,数据库会根据主键约束抛出异常,以保证数据的一致性和完整性。在文中提到的问题二中,如果尝试插入已存在的主键值,就会触发主键冲突异常。
2023-04-19 11:32:32
549
梦幻星空_
Java
...领域,签名是一种验证数据完整性和来源合法性的重要手段。在本文中,签名是指微信JS-SDK调用时,由服务器端根据特定算法和参数(包括access_token、nonceStr、timestamp以及url等)生成的一串唯一标识符。这串签名用于前端与微信服务器进行交互时的身份验证,确保数据在传输过程中未被篡改,只有正确的签名才能使微信JS-SDK的功能得以正常使用。 SHA-1 , SHA-1(Secure Hash Algorithm 1)是一种广泛使用的加密散列函数,它将任意长度的数据映射为固定长度(160位)的哈希值,也称为“数字指纹”。在文中,SHA-1作为生成微信JS-SDK签名的加密算法,通过对拼接好的字符串进行SHA-1计算,得到一个唯一的签名值,以确保数据的安全性及防止数据被恶意篡改。由于原文中提到Java代码片段使用了SHA-1算法来生成签名,因此在这个语境下,SHA-1的作用尤为关键。
2023-09-10 15:26:34
315
人生如戏_
Apache Lucene
...文档都有其独一无二的标识符——document id。当我们试图使用相同的document id创建并添加一个新的文档到索引时,DocumentAlreadyExistsException就会闪亮登场。这是因为Lucene这个家伙,为了确保索引数据的整齐划一、滴水不漏,坚决不让两个相同ID的文档同时存在于它的数据库里。就像是图书管理员坚决不让两本同书名、同作者的书籍混进同一个书架一样,它对索引数据的一致性和完整性要求可是相当严格的呢! java // 创建一个新的文档 Document doc = new Document(); doc.add(new StringField("id", "123", Field.Store.YES)); doc.add(new TextField("content", "This is a sample document.", Field.Store.YES)); // 尝试将文档添加到索引(假设索引中已有id为"123"的文档) IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig()); try { writer.addDocument(doc); } catch (DocumentAlreadyExistsException e) { System.out.println("Oops! A document with the same ID already exists."); // 这里是异常处理逻辑... } 3. 遇到DocumentAlreadyExistsException时的思考过程 首先,当此异常出现时,我们应当反思一下业务逻辑。是不是有用户不小心手滑了,或者咱们的系统设计上有个小bug,让一份文档被多次抓取进了索引里?要是真有这样的情况,那我们得在最上面的应用层好好瞅瞅,做点相应的检查和优化工作,确保同样的内容不会被反复提交上去。 其次,如果确实有更新文档的需求,而不是简单地添加新的文档,那么应该采用IndexWriter.updateDocument()方法替换原有的文档,而非addDocument(): java Term term = new Term("id", "123"); writer.updateDocument(term, updatedDoc); // 更新已存在的文档 最后,对于一些需要保证唯一性的场景,例如日志记录、订单编号等,可以考虑在索引建立阶段就设置IndexWriterConfig.setMergePolicy(NoDuplicatesMergePolicy.INSTANCE),从而避免因并发写入导致的重复文档问题。 4. 深入探讨与应对策略 在实践中,处理DocumentAlreadyExistsException不仅关乎对Lucene机制的理解,更需要结合具体应用场景来制定解决方案。比如,我们可以设想这样一种方案:定制一个独特的错误处理机制,这样一来,只要系统一检测到这个异常情况,就会自动启动文档内容合并流程,或者更贴心地告诉你,哎呀,这份文档已经存在了,需要你提供一个新的文档编号。 此外,对于高并发环境下的索引更新,除了利用Lucene提供的API外,还需要引入适当的并发控制策略,如乐观锁、分布式锁等,确保在多线程环境下,也能正确无误地处理文档添加与更新操作。 总结起来,DocumentAlreadyExistsException在Apache Lucene中扮演着守护者角色,提醒我们在构建高效、精准的全文搜索服务的同时,也要注意维护数据的一致性与完整性。如果咱们能全面摸清这个异常状况,并且妥善应对处理,那么咱们的应用程序就会变得更皮实耐造,这样一来,用户体验也绝对会蹭蹭地往上提升,变得超赞!
2023-01-30 18:34:51
458
昨夜星辰昨夜风
转载文章
...个系统目录表,记录了数据库中的所有表、索引、视图等对象的基本信息,如名称(relname)、OID(唯一标识符)等。在处理本文所述问题时,通过联合查询 pg_class 表和其他系统表,可以找到与被锁定表相关的后台进程信息。 pg_stat_activity 表 , pg_stat_activity 是 PostgreSQL 内置的一个系统视图,提供了关于数据库当前活动会话及其执行状态的信息,包括会话 ID(pid)、启动时间(backend_start)、应用程序名(application_name)、查询开始时间(query_start)、等待状态(waiting)、事务状态(state)以及当前执行的查询语句(query)等。在排查锁定问题时,通过查询 pg_stat_activity 表可了解哪些会话可能对问题表进行了锁定操作。
2023-09-22 09:08:45
126
转载
PostgreSQL
一、数据表索引过多导致查询性能下降 在我们日常的数据库开发过程中,我们都希望能够通过创建索引来提高查询效率。这是因为索引就像是数据库的一张超级导航图,能够迅速找到你要的数据藏在哪里,这样一来,就不用大海捞针似的把整个表格从头到尾扫一遍了。这可真是个大大的提速秘诀,让查询速度嗖嗖地提升起来!然而,有时候我们会遇到这么个情况:明明我们辛辛苦苦创建了一堆索引,本以为查询速度能嗖嗖提升,结果却不如人意,反而还冒出了一些小插曲,让人头疼不已。这就是因为我们的索引创建得太多了。 二、索引的创建原则 那么,我们应该怎样正确地创建索引呢?首先,我们需要明确一点,不是所有的字段都适合创建索引。一般来说,我们只需要在经常用于WHERE子句、JOIN子句或者ORDER BY子句的字段上创建索引。这么做的妙处在于,只有当需要用到这些字段的数据时,系统才会聪明地调用索引,这样一来,就能有效地避开那些没必要的花费,让整个过程更“轻盈”、更高效。 1. 使用explain命令分析SQL语句 为了更好地了解索引对于查询的影响,我们可以使用explain命令来分析SQL语句。这个命令能让我们像看漫画书一样,瞧瞧查询执行的“剧本”,一目了然地看到哪些字段正在被索引这位幕后英雄助力,又有哪些字段还在等待被发掘利用。这样我们就可以根据实际情况来决定是否需要创建索引。 sql EXPLAIN SELECT FROM users WHERE age > 20; 上面的SQL语句将会返回一个表格,其中包含了查询的执行计划。我们可以看到,age字段被使用到了索引,而name字段没有被使用到索引。 2. 观察SQL语句的执行情况 除了使用explain命令外,我们还可以直接观察SQL语句的执行情况,来判断是否需要创建索引。咱们可以翻翻数据库的日志文件,或者使使劲儿数据库监控工具这把“神器”,瞧瞧SQL语句执行花了多久、CPU被占用了多少、磁盘I/O的情况怎么样,这些信息都能一目了然。要是你发现某个SQL语句运行老半天还在转悠,或者CPU占用噌噌往上涨得离谱,那很可能就是因为你还没给它创建索引。 三、解决方法 知道了上述的原因后,我们就可以采取一些措施来解决这个问题了。首先,我们可以尽量减少索引的数量。这意味着我们需要更加精确地选择要创建索引的字段,避免无谓的开销。其次,咱们还可以时不时地给索引做个“大扫除”,重新构建一下,或者考虑用上一些特殊的索引技巧。比如,就像覆盖索引啦,唯一索引这些小玩意儿,都能让数据库更好地运转起来。最后,我们还可以琢磨一下采用数据库分区或者分片这招,让查询的压力能够分散开来,这样一来就不会把所有的“重活”都压在一块儿了。 四、总结 总的来说,索引是一个非常重要的概念,它能够极大地提高数据库的查询效率。然而,如果索引创建得过多,就会导致查询性能下降。因此,我们在创建索引时,一定要考虑到实际情况,避免盲目创建。同时呢,咱们也得不断给自己充电,学点新鲜的知识,掌握更多的技能才行。这样一来,面对各种难缠的问题,咱们就能更加游刃有余地解决它们了。只有这样,我们才能够成为一名真正的数据库专家。
2023-06-12 18:34:17
502
青山绿水-t
HBase
...践 1. 引言 在大数据时代,处理海量数据成为常态,而HBase作为一款高效、可伸缩的分布式列式数据库,在众多场景中扮演着关键角色。不过,在处理多线程或者分布式这些复杂场景时,为了不让多个任务同时改数据搞得一团糟,确保信息同步和准确无误,一个给力的分布式锁机制可是必不可少的!这篇文会拽着你的小手,一起蹦跶进HBase的大千世界。咱会通过实实在在的代码实例,再配上超级详细的解说,悄悄告诉你怎么巧妙玩转HBase,用它来实现那个高大上的分布式锁,保证让你看得明明白白、学得轻轻松松! 2. HBase基础理解 首先,让我们先对HBase有个基本的认识。HBase基于Google的Bigtable设计思想,利用Hadoop HDFS提供存储支持,并通过Zookeeper管理集群状态和服务协调。他们家这玩意儿,独门绝技就是RowKey的设计,再加上那牛哄哄的原子性操作,妥妥地帮咱们在分布式锁这块儿打开了新世界的大门。 3. 利用HBase实现分布式锁的基本思路 在HBase中,我们可以创建一个特定的表,用于表示锁的状态。每一行代表一把锁,RowKey可以是锁的名称或者需要锁定的资源标识。每个行只有一个列族(例如:"Lock"),并且这个列族下的唯一一个列(例如:"lock")的值并不重要,我们只需要关注它的存在与否来判断锁是否被占用。 4. 示例代码详解 下面是一个使用Java API实现HBase分布式锁的示例: java import org.apache.hadoop.hbase.TableName; 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.client.Table; public class HBaseDistributedLock { private final Connection connection; private final TableName lockTable = TableName.valueOf("distributed_locks"); public HBaseDistributedLock(Configuration conf) throws IOException { this.connection = ConnectionFactory.createConnection(conf); } // 尝试获取锁 public boolean tryLock(String lockName) throws IOException { Table table = connection.getTable(lockTable); Put put = new Put(Bytes.toBytes(lockName)); put.addColumn("Lock".getBytes(), "lock".getBytes(), System.currentTimeMillis(), null); try { table.put(put); // 如果这行已存在,则会抛出异常,表示锁已被占用 return true; // 无异常则表示成功获取锁 } catch (ConcurrentModificationException e) { return false; // 表示锁已被其他客户端占有 } finally { table.close(); } } // 释放锁 public void unlock(String lockName) throws IOException { Table table = connection.getTable(lockTable); Delete delete = new Delete(Bytes.toBytes(lockName)); table.delete(delete); table.close(); } } 5. 分析与讨论 上述代码展示了如何借助HBase实现分布式锁的核心逻辑。当你试着去拿锁的时候,就相当于你要在一张表里插一条新记录。如果发现这条记录竟然已经存在了(这就意味着这把锁已经被别的家伙抢先一步拿走了),系统就会毫不客气地抛出一个异常,然后告诉你“没戏,锁没拿到”,也就是返回个false。而在解锁时,只需删除对应的行即可。 然而,这种简单实现并未考虑超时、锁续期等问题,实际应用中还需要结合Zookeeper进行优化,如借助Zookeeper的临时有序节点特性实现更完善的分布式锁服务。 6. 结语 HBase的分布式锁实现是一种基于数据库事务特性的方法,它简洁且直接。不过呢,每种技术方案都有它能施展拳脚的地方,也有它的局限性。就好比选择分布式锁的实现方式,咱们得看实际情况,比如应用场景的具体需求、对性能的高标准严要求,还有团队掌握的技术工具箱。这就好比选工具干活,得看活儿是什么、要干得多精细,再看看咱手头有什么趁手的家伙事儿,综合考虑才能选对最合适的那个。明白了这个原理之后,咱们就可以动手实操起来,并且不断摸索、优化它,让这玩意儿更好地为我们设计的分布式系统架构服务,让它发挥更大的作用。
2023-11-04 13:27:56
437
晚秋落叶
转载文章
...了系统内部资源的一种唯一标识符。在文章中,作者通过查找并获取弹出框及其子控件的句柄,从而能够精确地与这些窗口组件进行交互。例如,通过找到文件名输入框的句柄,进而可以向该输入框发送消息以实现自动输入文件名。 SendMessage函数 , SendMessage是Windows API中一个用于向指定窗口发送消息的函数。在文中,作者利用win32gui模块封装的SendMessage方法,向特定窗口句柄发送WM_SETTEXT消息,以此实现在“文件另存为”对话框的文件名输入框中设置文本内容。具体而言,SendMessage(hwnd, message, wParam, lParam)这四个参数分别代表目标窗口句柄、要发送的消息类型、两个附加参数,其中在本文情境下message参数设为WM_SETTEXT,wParam和lParam通常设为None,而将待输入的文件名字符串作为实际消息内容传递。 窗口类名 , 在Windows操作系统中,每个窗口都有一个类名,它是创建窗口时定义的,用来区分不同类型的窗口。在文中,作者通过查找窗口的类名来识别特定的“文件另存为”弹出框和其他相关控件,比如ComboBox、Edit或Button等,以便精确操控这些窗口组件完成自动化任务。 模拟按键点击 , 模拟按键点击是指在程序中模拟用户的键盘或鼠标动作,使得程序可以如同真实用户一样与应用程序交互。在本文中,作者使用win32api模块提供的keybd_event函数模拟按下Enter键和Ctrl+V键等操作,以实现路径选择和回车确认的功能,还通过mouse_event函数模拟鼠标左键单击事件,来点击取消按钮,这些都是对用户交互行为的自动化模拟。
2023-12-17 22:46:11
253
转载
转载文章
...tice公司及其收购数据库技术公司–StormDB的产品。Postgres-XL是一个横向扩展的开源数据库集群,具有足够的灵活性来处理不同的数据库任务。 Postgres-XL功能特性 开放源代码:(源协议使用宽松的“Mozilla Public License”许可,允许将开源代码与闭源代码混在一起使用。) 完全的ACID支持 可横向扩展的关系型数据库(RDBMS) 支持OLAP应用,采用MPP(Massively Parallel Processing:大规模并行处理系统)架构模式 支持OLTP应用,读写性能可扩展 集群级别的ACID特性 多租户安全 也可被用作分布式Key-Value存储 事务处理与数据分析处理混合型数据库 支持丰富的SQL语句类型,比如:关联子查询 支持绝大部分PostgreSQL的SQL语句 分布式多版本并发控制(MVCC:Multi-version Concurrency Control) 支持JSON和XML格式 Postgres-XL缺少的功能 内建的高可用机制 使用外部机制实现高可能,如:Corosync/Pacemaker 有未来功能提升的空间 增加节点/重新分片数据(re-shard)的简便性 数据重分布(redistribution)期间会锁表 可采用预分片(pre-shard)方式解决,在同台物理服务器上建立多个数据节点,每个节点存储一个数据分片。数据重分布时,将一些数据节点迁出即可 某些外键、唯一性约束功能 Postgres-XL架构 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M9lFuEIP-1640133702200)(./assets/postgre-xl.jpg)] 基于开源项目Postgres-XC XL增加了MPP,允许数据节点间直接通讯,交换复杂跨节点关联查询相关数据信息,减少协调器负载。 多个协调器(Coordinator) 应用程序的数据库连入点 分析查询语句,生成执行计划 多个数据节点(DataNode) 实际的数据存储 数据自动打散分布到集群中各数据节点 本地执行查询 一个查询在所有相关节点上并行查询 全局事务管理器(GTM:Global Transaction Manager) 提供事务间一致性视图 部署GTM Proxy实例,以提高性能 Postgre-XL主要组件 GTM (Global Transaction Manager) - 全局事务管理器 GTM是Postgres-XL的一个关键组件,用于提供一致的事务管理和元组可见性控制。 GTM Standby GTM的备节点,在pgxc,pgxl中,GTM控制所有的全局事务分配,如果出现问题,就会导致整个集群不可用,为了增加可用性,增加该备用节点。当GTM出现问题时,GTM Standby可以升级为GTM,保证集群正常工作。 GTM-Proxy GTM需要与所有的Coordinators通信,为了降低压力,可以在每个Coordinator机器上部署一个GTM-Proxy。 Coordinator --协调器 协调器是应用程序到数据库的接口。它的作用类似于传统的PostgreSQL后台进程,但是协调器不存储任何实际数据。实际数据由数据节点存储。协调器接收SQL语句,根据需要获取全局事务Id和全局快照,确定涉及哪些数据节点,并要求它们执行(部分)语句。当向数据节点发出语句时,它与GXID和全局快照相关联,以便多版本并发控制(MVCC)属性扩展到集群范围。 Datanode --数据节点 用于实际存储数据。表可以分布在各个数据节点之间,也可以复制到所有数据节点。数据节点没有整个数据库的全局视图,它只负责本地存储的数据。接下来,协调器将检查传入语句,并制定子计划。然后,根据需要将这些数据连同GXID和全局快照一起传输到涉及的每个数据节点。数据节点可以在不同的会话中接收来自各个协调器的请求。但是,由于每个事务都是惟一标识的,并且与一致的(全局)快照相关联,所以每个数据节点都可以在其事务和快照上下文中正确执行。 Postgres-XL继承了PostgreSQL Postgres-XL是PostgreSQL的扩展并继承了其很多特性: 复杂查询 外键 触发器 视图 事务 MVCC(多版本控制) 此外,类似于PostgreSQL,用户可以通过多种方式扩展Postgres-XL,例如添加新的 数据类型 函数 操作 聚合函数 索引类型 过程语言 安装 环境说明 由于资源有限,gtm一台、另外两台身兼数职。 主机名 IP 角色 端口 nodename 数据目录 gtm 192.168.20.132 GTM 6666 gtm /nodes/gtm 协调器 5432 coord1 /nodes/coordinator xl1 192.168.20.133 数据节点 5433 node1 /nodes/pgdata gtm代理 6666 gtmpoxy01 /nodes/gtm_pxy1 协调器 5432 coord2 /nodes/coordinator xl2 192.168.20.134 数据节点 5433 node2 /nodes/pgdata gtm代理 6666 gtmpoxy02 /nodes/gtm_pxy2 要求 GNU make版本 3.8及以上版本 [root@pg ~] make --versionGNU Make 3.82Built for x86_64-redhat-linux-gnuCopyright (C) 2010 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law. 需安装GCC包 需安装tar包 用于解压缩文件 默认需要GNU Readline library 其作用是可以让psql命令行记住执行过的命令,并且可以通过键盘上下键切换命令。但是可以通过--without-readline禁用这个特性,或者可以指定--withlibedit-preferred选项来使用libedit 默认使用zlib压缩库 可通过--without-zlib选项来禁用 配置hosts 所有主机上都配置 [root@xl2 11] cat /etc/hosts127.0.0.1 localhost192.168.20.132 gtm192.168.20.133 xl1192.168.20.134 xl2 关闭防火墙、Selinux 所有主机都执行 关闭防火墙: [root@gtm ~] systemctl stop firewalld.service[root@gtm ~] systemctl disable firewalld.service selinux设置: [root@gtm ~]vim /etc/selinux/config 设置SELINUX=disabled,保存退出。 This file controls the state of SELinux on the system. SELINUX= can take one of these three values: enforcing - SELinux security policy is enforced. permissive - SELinux prints warnings instead of enforcing. disabled - No SELinux policy is loaded.SELINUX=disabled SELINUXTYPE= can take one of three two values: targeted - Targeted processes are protected, minimum - Modification of targeted policy. Only selected processes are protected. mls - Multi Level Security protection. 安装依赖包 所有主机上都执行 yum install -y flex bison readline-devel zlib-devel openjade docbook-style-dsssl gcc 创建用户 所有主机上都执行 [root@gtm ~] useradd postgres[root@gtm ~] passwd postgres[root@gtm ~] su - postgres[root@gtm ~] mkdir ~/.ssh[root@gtm ~] chmod 700 ~/.ssh 配置SSH免密登录 仅仅在gtm节点配置如下操作: [root@gtm ~] su - postgres[postgres@gtm ~] ssh-keygen -t rsa[postgres@gtm ~] cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys[postgres@gtm ~] chmod 600 ~/.ssh/authorized_keys 将刚生成的认证文件拷贝到xl1到xl2中,使得gtm节点可以免密码登录xl1~xl2的任意一个节点: [postgres@gtm ~] scp ~/.ssh/authorized_keys postgres@xl1:~/.ssh/[postgres@gtm ~] scp ~/.ssh/authorized_keys postgres@xl2:~/.ssh/ 对所有提示都不要输入,直接enter下一步。直到最后,因为第一次要求输入目标机器的用户密码,输入即可。 下载源码 下载地址:https://www.postgres-xl.org/download/ [root@slave ~] ll postgres-xl-10r1.1.tar.gz-rw-r--r-- 1 root root 28121666 May 30 05:21 postgres-xl-10r1.1.tar.gz 编译、安装Postgres-XL 所有节点都安装,编译需要一点时间,最好同时进行编译。 [root@slave ~] tar xvf postgres-xl-10r1.1.tar.gz[root@slave ~] ./configure --prefix=/home/postgres/pgxl/[root@slave ~] make[root@slave ~] make install[root@slave ~] cd contrib/ --安装必要的工具,在gtm节点上安装即可[root@slave ~] make[root@slave ~] make install 配置环境变量 所有节点都要配置 进入postgres用户,修改其环境变量,开始编辑 [root@gtm ~]su - postgres[postgres@gtm ~]vi .bashrc --不是.bash_profile 在打开的文件末尾,新增如下变量配置: export PGHOME=/home/postgres/pgxlexport LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATHexport PATH=$PGHOME/bin:$PATH 按住esc,然后输入:wq!保存退出。输入以下命令对更改重启生效。 [postgres@gtm ~] source .bashrc --不是.bash_profile 输入以下语句,如果输出变量结果,代表生效 [postgres@gtm ~] echo $PGHOME 应该输出/home/postgres/pgxl代表生效 配置集群 生成pgxc_ctl.conf配置文件 [postgres@gtm ~] pgxc_ctl prepare/bin/bashInstalling pgxc_ctl_bash script as /home/postgres/pgxl/pgxc_ctl/pgxc_ctl_bash.ERROR: File "/home/postgres/pgxl/pgxc_ctl/pgxc_ctl.conf" not found or not a regular file. No such file or directoryInstalling pgxc_ctl_bash script as /home/postgres/pgxl/pgxc_ctl/pgxc_ctl_bash.Reading configuration using /home/postgres/pgxl/pgxc_ctl/pgxc_ctl_bash --home /home/postgres/pgxl/pgxc_ctl --configuration /home/postgres/pgxl/pgxc_ctl/pgxc_ctl.confFinished reading configuration. PGXC_CTL START Current directory: /home/postgres/pgxl/pgxc_ctl 配置pgxc_ctl.conf 新建/home/postgres/pgxc_ctl/pgxc_ctl.conf文件,编辑如下: 对着模板文件一个一个修改,否则会造成初始化过程出现各种神奇问题。 pgxcInstallDir=$PGHOMEpgxlDATA=$PGHOME/data pgxcOwner=postgres---- GTM Master -----------------------------------------gtmName=gtmgtmMasterServer=gtmgtmMasterPort=6666gtmMasterDir=$pgxlDATA/nodes/gtmgtmSlave=y Specify y if you configure GTM Slave. Otherwise, GTM slave will not be configured and all the following variables will be reset.gtmSlaveName=gtmSlavegtmSlaveServer=gtm value none means GTM slave is not available. Give none if you don't configure GTM Slave.gtmSlavePort=20001 Not used if you don't configure GTM slave.gtmSlaveDir=$pgxlDATA/nodes/gtmSlave Not used if you don't configure GTM slave.---- GTM-Proxy Master -------gtmProxyDir=$pgxlDATA/nodes/gtm_proxygtmProxy=y gtmProxyNames=(gtm_pxy1 gtm_pxy2) gtmProxyServers=(xl1 xl2) gtmProxyPorts=(6666 6666) gtmProxyDirs=($gtmProxyDir $gtmProxyDir) ---- Coordinators ---------coordMasterDir=$pgxlDATA/nodes/coordcoordNames=(coord1 coord2) coordPorts=(5432 5432) poolerPorts=(6667 6667) coordPgHbaEntries=(0.0.0.0/0)coordMasterServers=(xl1 xl2) coordMasterDirs=($coordMasterDir $coordMasterDir)coordMaxWALsernder=0 没设置备份节点,设置为0coordMaxWALSenders=($coordMaxWALsernder $coordMaxWALsernder) 数量保持和coordMasterServers一致coordSlave=n---- Datanodes ----------datanodeMasterDir=$pgxlDATA/nodes/dn_masterprimaryDatanode=xl1 主数据节点datanodeNames=(node1 node2)datanodePorts=(5433 5433) datanodePoolerPorts=(6668 6668) datanodePgHbaEntries=(0.0.0.0/0)datanodeMasterServers=(xl1 xl2)datanodeMasterDirs=($datanodeMasterDir $datanodeMasterDir)datanodeMaxWalSender=4datanodeMaxWALSenders=($datanodeMaxWalSender $datanodeMaxWalSender) 集群初始化,启动,停止 初始化 pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf init all 输出结果: /bin/bashInstalling pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Installing pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Reading configuration using /home/postgres/pgxc_ctl/pgxc_ctl_bash --home /home/postgres/pgxc_ctl --configuration /home/postgres/pgxc_ctl/pgxc_ctl.conf/home/postgres/pgxc_ctl/pgxc_ctl.conf: line 189: $coordExtraConfig: ambiguous redirectFinished reading configuration. PGXC_CTL START Current directory: /home/postgres/pgxc_ctlStopping all the coordinator masters.Stopping coordinator master coord1.Stopping coordinator master coord2.pg_ctl: directory "/home/postgres/pgxc/nodes/coord/coord1" does not existpg_ctl: directory "/home/postgres/pgxc/nodes/coord/coord2" does not existDone.Stopping all the datanode masters.Stopping datanode master datanode1.Stopping datanode master datanode2.pg_ctl: PID file "/home/postgres/pgxc/nodes/datanode/datanode1/postmaster.pid" does not existIs server running?Done.Stop GTM masterwaiting for server to shut down.... doneserver stopped[postgres@gtm ~]$ echo $PGHOME/home/postgres/pgxl[postgres@gtm ~]$ ll /home/postgres/pgxl/pgxc/nodes/gtm/gtm.^C[postgres@gtm ~]$ pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf init all/bin/bashInstalling pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Installing pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Reading configuration using /home/postgres/pgxc_ctl/pgxc_ctl_bash --home /home/postgres/pgxc_ctl --configuration /home/postgres/pgxc_ctl/pgxc_ctl.conf/home/postgres/pgxc_ctl/pgxc_ctl.conf: line 189: $coordExtraConfig: ambiguous redirectFinished reading configuration. PGXC_CTL START Current directory: /home/postgres/pgxc_ctlInitialize GTM masterERROR: target directory (/home/postgres/pgxc/nodes/gtm) exists and not empty. Skip GTM initilializationDone.Start GTM masterserver startingInitialize all the coordinator masters.Initialize coordinator master coord1.ERROR: target coordinator master coord1 is running now. Skip initilialization.Initialize coordinator master coord2.The files belonging to this database system will be owned by user "postgres".This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /home/postgres/pgxc/nodes/coord/coord2 ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... creating cluster information ... oksyncing data to disk ... okfreezing database template0 ... okfreezing database template1 ... okfreezing database postgres ... okWARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success.Done.Starting coordinator master.Starting coordinator master coord1ERROR: target coordinator master coord1 is already running now. Skip initialization.Starting coordinator master coord22019-05-30 21:09:25.562 EDT [2148] LOG: listening on IPv4 address "0.0.0.0", port 54322019-05-30 21:09:25.562 EDT [2148] LOG: listening on IPv6 address "::", port 54322019-05-30 21:09:25.563 EDT [2148] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"2019-05-30 21:09:25.601 EDT [2149] LOG: database system was shut down at 2019-05-30 21:09:22 EDT2019-05-30 21:09:25.605 EDT [2148] LOG: database system is ready to accept connections2019-05-30 21:09:25.612 EDT [2156] LOG: cluster monitor startedDone.Initialize all the datanode masters.Initialize the datanode master datanode1.Initialize the datanode master datanode2.The files belonging to this database system will be owned by user "postgres".This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /home/postgres/pgxc/nodes/datanode/datanode1 ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... creating cluster information ... oksyncing data to disk ... okfreezing database template0 ... okfreezing database template1 ... okfreezing database postgres ... okWARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success.The files belonging to this database system will be owned by user "postgres".This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /home/postgres/pgxc/nodes/datanode/datanode2 ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... creating cluster information ... oksyncing data to disk ... okfreezing database template0 ... okfreezing database template1 ... okfreezing database postgres ... okWARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success.Done.Starting all the datanode masters.Starting datanode master datanode1.WARNING: datanode master datanode1 is running now. Skipping.Starting datanode master datanode2.2019-05-30 21:09:33.352 EDT [2404] LOG: listening on IPv4 address "0.0.0.0", port 154322019-05-30 21:09:33.352 EDT [2404] LOG: listening on IPv6 address "::", port 154322019-05-30 21:09:33.355 EDT [2404] LOG: listening on Unix socket "/tmp/.s.PGSQL.15432"2019-05-30 21:09:33.392 EDT [2404] LOG: redirecting log output to logging collector process2019-05-30 21:09:33.392 EDT [2404] HINT: Future log output will appear in directory "pg_log".Done.psql: FATAL: no pg_hba.conf entry for host "192.168.20.132", user "postgres", database "postgres"psql: FATAL: no pg_hba.conf entry for host "192.168.20.132", user "postgres", database "postgres"Done.psql: FATAL: no pg_hba.conf entry for host "192.168.20.132", user "postgres", database "postgres"psql: FATAL: no pg_hba.conf entry for host "192.168.20.132", user "postgres", database "postgres"Done.[postgres@gtm ~]$ pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf stop all/bin/bashInstalling pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Installing pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Reading configuration using /home/postgres/pgxc_ctl/pgxc_ctl_bash --home /home/postgres/pgxc_ctl --configuration /home/postgres/pgxc_ctl/pgxc_ctl.conf/home/postgres/pgxc_ctl/pgxc_ctl.conf: line 189: $coordExtraConfig: ambiguous redirectFinished reading configuration. PGXC_CTL START Current directory: /home/postgres/pgxc_ctlStopping all the coordinator masters.Stopping coordinator master coord1.Stopping coordinator master coord2.pg_ctl: directory "/home/postgres/pgxc/nodes/coord/coord1" does not existDone.Stopping all the datanode masters.Stopping datanode master datanode1.Stopping datanode master datanode2.pg_ctl: PID file "/home/postgres/pgxc/nodes/datanode/datanode1/postmaster.pid" does not existIs server running?Done.Stop GTM masterwaiting for server to shut down.... doneserver stopped[postgres@gtm ~]$ pgxc_ctl/bin/bashInstalling pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Installing pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Reading configuration using /home/postgres/pgxc_ctl/pgxc_ctl_bash --home /home/postgres/pgxc_ctl --configuration /home/postgres/pgxc_ctl/pgxc_ctl.conf/home/postgres/pgxc_ctl/pgxc_ctl.conf: line 189: $coordExtraConfig: ambiguous redirectFinished reading configuration. PGXC_CTL START Current directory: /home/postgres/pgxc_ctlPGXC monitor allNot running: gtm masterRunning: coordinator master coord1Not running: coordinator master coord2Running: datanode master datanode1Not running: datanode master datanode2PGXC stop coordinator master coord1Stopping coordinator master coord1.pg_ctl: directory "/home/postgres/pgxc/nodes/coord/coord1" does not existDone.PGXC stop datanode master datanode1Stopping datanode master datanode1.pg_ctl: PID file "/home/postgres/pgxc/nodes/datanode/datanode1/postmaster.pid" does not existIs server running?Done.PGXC monitor allNot running: gtm masterRunning: coordinator master coord1Not running: coordinator master coord2Running: datanode master datanode1Not running: datanode master datanode2PGXC monitor allNot running: gtm masterNot running: coordinator master coord1Not running: coordinator master coord2Not running: datanode master datanode1Not running: datanode master datanode2PGXC exit[postgres@gtm ~]$ pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf init all/bin/bashInstalling pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Installing pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Reading configuration using /home/postgres/pgxc_ctl/pgxc_ctl_bash --home /home/postgres/pgxc_ctl --configuration /home/postgres/pgxc_ctl/pgxc_ctl.conf/home/postgres/pgxc_ctl/pgxc_ctl.conf: line 189: $coordExtraConfig: ambiguous redirectFinished reading configuration. PGXC_CTL START Current directory: /home/postgres/pgxc_ctlInitialize GTM masterERROR: target directory (/home/postgres/pgxc/nodes/gtm) exists and not empty. Skip GTM initilializationDone.Start GTM masterserver startingInitialize all the coordinator masters.Initialize coordinator master coord1.Initialize coordinator master coord2.The files belonging to this database system will be owned by user "postgres".This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /home/postgres/pgxc/nodes/coord/coord1 ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... creating cluster information ... oksyncing data to disk ... okfreezing database template0 ... okfreezing database template1 ... okfreezing database postgres ... okWARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success.The files belonging to this database system will be owned by user "postgres".This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /home/postgres/pgxc/nodes/coord/coord2 ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... creating cluster information ... oksyncing data to disk ... okfreezing database template0 ... okfreezing database template1 ... okfreezing database postgres ... okWARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success.Done.Starting coordinator master.Starting coordinator master coord1Starting coordinator master coord22019-05-30 21:13:03.998 EDT [25137] LOG: listening on IPv4 address "0.0.0.0", port 54322019-05-30 21:13:03.998 EDT [25137] LOG: listening on IPv6 address "::", port 54322019-05-30 21:13:04.000 EDT [25137] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"2019-05-30 21:13:04.038 EDT [25138] LOG: database system was shut down at 2019-05-30 21:13:00 EDT2019-05-30 21:13:04.042 EDT [25137] LOG: database system is ready to accept connections2019-05-30 21:13:04.049 EDT [25145] LOG: cluster monitor started2019-05-30 21:13:04.020 EDT [2730] LOG: listening on IPv4 address "0.0.0.0", port 54322019-05-30 21:13:04.020 EDT [2730] LOG: listening on IPv6 address "::", port 54322019-05-30 21:13:04.021 EDT [2730] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"2019-05-30 21:13:04.057 EDT [2731] LOG: database system was shut down at 2019-05-30 21:13:00 EDT2019-05-30 21:13:04.061 EDT [2730] LOG: database system is ready to accept connections2019-05-30 21:13:04.062 EDT [2738] LOG: cluster monitor startedDone.Initialize all the datanode masters.Initialize the datanode master datanode1.Initialize the datanode master datanode2.The files belonging to this database system will be owned by user "postgres".This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /home/postgres/pgxc/nodes/datanode/datanode1 ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... creating cluster information ... oksyncing data to disk ... okfreezing database template0 ... okfreezing database template1 ... okfreezing database postgres ... okWARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success.The files belonging to this database system will be owned by user "postgres".This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /home/postgres/pgxc/nodes/datanode/datanode2 ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... creating cluster information ... oksyncing data to disk ... okfreezing database template0 ... okfreezing database template1 ... okfreezing database postgres ... okWARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success.Done.Starting all the datanode masters.Starting datanode master datanode1.Starting datanode master datanode2.2019-05-30 21:13:12.077 EDT [25392] LOG: listening on IPv4 address "0.0.0.0", port 154322019-05-30 21:13:12.077 EDT [25392] LOG: listening on IPv6 address "::", port 154322019-05-30 21:13:12.079 EDT [25392] LOG: listening on Unix socket "/tmp/.s.PGSQL.15432"2019-05-30 21:13:12.114 EDT [25392] LOG: redirecting log output to logging collector process2019-05-30 21:13:12.114 EDT [25392] HINT: Future log output will appear in directory "pg_log".2019-05-30 21:13:12.079 EDT [2985] LOG: listening on IPv4 address "0.0.0.0", port 154322019-05-30 21:13:12.079 EDT [2985] LOG: listening on IPv6 address "::", port 154322019-05-30 21:13:12.081 EDT [2985] LOG: listening on Unix socket "/tmp/.s.PGSQL.15432"2019-05-30 21:13:12.117 EDT [2985] LOG: redirecting log output to logging collector process2019-05-30 21:13:12.117 EDT [2985] HINT: Future log output will appear in directory "pg_log".Done.psql: FATAL: no pg_hba.conf entry for host "192.168.20.132", user "postgres", database "postgres"psql: FATAL: no pg_hba.conf entry for host "192.168.20.132", user "postgres", database "postgres"Done.psql: FATAL: no pg_hba.conf entry for host "192.168.20.132", user "postgres", database "postgres"psql: FATAL: no pg_hba.conf entry for host "192.168.20.132", user "postgres", database "postgres"Done. 启动 pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf start all 关闭 pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf stop all 查看集群状态 [postgres@gtm ~]$ pgxc_ctl monitor all/bin/bashInstalling pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Installing pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Reading configuration using /home/postgres/pgxc_ctl/pgxc_ctl_bash --home /home/postgres/pgxc_ctl --configuration /home/postgres/pgxc_ctl/pgxc_ctl.conf/home/postgres/pgxc_ctl/pgxc_ctl.conf: line 189: $coordExtraConfig: ambiguous redirectFinished reading configuration. PGXC_CTL START Current directory: /home/postgres/pgxc_ctlRunning: gtm masterRunning: coordinator master coord1Running: coordinator master coord2Running: datanode master datanode1Running: datanode master datanode2 配置集群信息 分别在数据节点、协调器节点上分别执行以下命令: 注:本节点只执行修改操作即可(alert node),其他节点执行创建命令(create node)。因为本节点已经包含本节点的信息。 create node coord1 with (type=coordinator,host=xl1, port=5432);create node coord2 with (type=coordinator,host=xl2, port=5432);alter node coord1 with (type=coordinator,host=xl1, port=5432);alter node coord2 with (type=coordinator,host=xl2, port=5432);create node datanode1 with (type=datanode, host=xl1,port=15432,primary=true,PREFERRED);create node datanode2 with (type=datanode, host=xl2,port=15432);alter node datanode1 with (type=datanode, host=xl1,port=15432,primary=true,PREFERRED);alter node datanode2 with (type=datanode, host=xl2,port=15432);select pgxc_pool_reload(); 分别登陆数据节点、协调器节点验证 postgres= select from pgxc_node;node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id-----------+-----------+-----------+-----------+----------------+------------------+-------------coord1 | C | 5432 | xl1 | f | f | 1885696643coord2 | C | 5432 | xl2 | f | f | -1197102633datanode2 | D | 15432 | xl2 | f | f | -905831925datanode1 | D | 15432 | xl1 | t | f | 888802358(4 rows) 测试 插入数据 在数据节点1,执行相关操作。 通过协调器端口登录PG [postgres@xl1 ~]$ psql -p 5432psql (PGXL 10r1.1, based on PG 10.6 (Postgres-XL 10r1.1))Type "help" for help.postgres= create database lei;CREATE DATABASEpostgres= \c lei;You are now connected to database "lei" as user "postgres".lei= create table test1(id int,name text);CREATE TABLElei= insert into test1(id,name) select generate_series(1,8),'测试';INSERT 0 8lei= select from test1;id | name----+------1 | 测试2 | 测试5 | 测试6 | 测试8 | 测试3 | 测试4 | 测试7 | 测试(8 rows) 注:默认创建的表为分布式表,也就是每个数据节点值存储表的部分数据。关于表类型具体说明,下面有说明。 通过15432端口登录数据节点,查看数据 有5条数据 [postgres@xl1 ~]$ psql -p 15432psql (PGXL 10r1.1, based on PG 10.6 (Postgres-XL 10r1.1))Type "help" for help.postgres= \c lei;You are now connected to database "lei" as user "postgres".lei= select from test1;id | name----+------1 | 测试2 | 测试5 | 测试6 | 测试8 | 测试(5 rows) 登录到节点2,查看数据 有3条数据 [postgres@xl2 ~]$ psql -p15432psql (PGXL 10r1.1, based on PG 10.6 (Postgres-XL 10r1.1))Type "help" for help.postgres= \c lei;You are now connected to database "lei" as user "postgres".lei= select from test1;id | name----+------3 | 测试4 | 测试7 | 测试(3 rows) 两个节点的数据加起来整个8条,没有问题。 至此Postgre-XL集群搭建完成。 创建数据库、表时可能会出现以下错误: ERROR: Failed to get pooled connections 是因为pg_hba.conf配置不对,所有节点加上host all all 192.168.20.0/0 trust并重启集群即可。 ERROR: No Datanode defined in cluster 首先确认是否创建了数据节点,也就是create node相关的命令。如果创建了则执行select pgxc_pool_reload();使其生效即可。 集群管理与应用 表类型说明 REPLICATION表:各个datanode节点中,表的数据完全相同,也就是说,插入数据时,会分别在每个datanode节点插入相同数据。读数据时,只需要读任意一个datanode节点上的数据。 建表语法: CREATE TABLE repltab (col1 int, col2 int) DISTRIBUTE BY REPLICATION; DISTRIBUTE :会将插入的数据,按照拆分规则,分配到不同的datanode节点中存储,也就是sharding技术。每个datanode节点只保存了部分数据,通过coordinate节点可以查询完整的数据视图。 CREATE TABLE disttab(col1 int, col2 int, col3 text) DISTRIBUTE BY HASH(col1); 模拟数据插入 任意登录一个coordinate节点进行建表操作 [postgres@gtm ~]$ psql -h xl1 -p 5432 -U postgrespostgres= INSERT INTO disttab SELECT generate_series(1,100), generate_series(101, 200), 'foo';INSERT 0 100postgres= INSERT INTO repltab SELECT generate_series(1,100), generate_series(101, 200);INSERT 0 100 查看数据分布结果: DISTRIBUTE表分布结果 postgres= SELECT xc_node_id, count() FROM disttab GROUP BY xc_node_id;xc_node_id | count ------------+-------1148549230 | 42-927910690 | 58(2 rows) REPLICATION表分布结果 postgres= SELECT count() FROM repltab;count -------100(1 row) 查看另一个datanode2中repltab表结果 [postgres@datanode2 pgxl9.5]$ psql -p 15432psql (PGXL 10r1.1, based on PG 10.6 (Postgres-XL 10r1.1))Type "help" for help.postgres= SELECT count() FROM repltab;count -------100(1 row) 结论:REPLICATION表中,datanode1,datanode2中表是全部数据,一模一样。而DISTRIBUTE表,数据散落近乎平均分配到了datanode1,datanode2节点中。 新增数据节点与数据重分布 在线新增节点、并重新分布数据。 新增datanode节点 在gtm集群管理节点上执行pgxc_ctl命令 [postgres@gtm ~]$ pgxc_ctl/bin/bashInstalling pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Installing pgxc_ctl_bash script as /home/postgres/pgxc_ctl/pgxc_ctl_bash.Reading configuration using /home/postgres/pgxc_ctl/pgxc_ctl_bash --home /home/postgres/pgxc_ctl --configuration /home/postgres/pgxc_ctl/pgxc_ctl.confFinished reading configuration. PGXC_CTL START Current directory: /home/postgres/pgxc_ctlPGXC 在服务器xl3上,新增一个master角色的datanode节点,名称是datanode3 端口号暂定5430,pool master暂定6669 ,指定好数据目录位置,从两个节点升级到3个节点,之后要写3个none none应该是datanodeSpecificExtraConfig或者datanodeSpecificExtraPgHba配置PGXC add datanode master datanode3 xl3 15432 6671 /home/postgres/pgxc/nodes/datanode/datanode3 none none none 等待新增完成后,查询集群节点状态: postgres= select from pgxc_node;node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id-----------+-----------+-----------+-----------+----------------+------------------+-------------datanode1 | D | 15432 | xl1 | t | f | 888802358datanode2 | D | 15432 | xl2 | f | f | -905831925datanode3 | D | 15432 | xl3 | f | f | -705831925coord1 | C | 5432 | xl1 | f | f | 1885696643coord2 | C | 5432 | xl2 | f | f | -1197102633(4 rows) 节点新增完毕 数据重新分布 由于新增节点后无法自动完成数据重新分布,需要手动操作。 DISTRIBUTE表分布在了node1,node2节点上,如下: postgres= SELECT xc_node_id, count() FROM disttab GROUP BY xc_node_id;xc_node_id | count ------------+-------1148549230 | 42-927910690 | 58(2 rows) 新增一个节点后,将sharding表数据重新分配到三个节点上,将repl表复制到新节点 重分布sharding表postgres= ALTER TABLE disttab ADD NODE (datanode3);ALTER TABLE 复制数据到新节点postgres= ALTER TABLE repltab ADD NODE (datanode3);ALTER TABLE 查看新的数据分布: postgres= SELECT xc_node_id, count() FROM disttab GROUP BY xc_node_id;xc_node_id | count ------------+--------700122826 | 36-927910690 | 321148549230 | 32(3 rows) 登录datanode3(新增的时候,放在了xl3服务器上,端口15432)节点查看数据: [postgres@gtm ~]$ psql -h xl3 -p 15432 -U postgrespsql (PGXL 10r1.1, based on PG 10.6 (Postgres-XL 10r1.1))Type "help" for help.postgres= select count() from repltab;count -------100(1 row) 很明显,通过 ALTER TABLE tt ADD NODE (dn)命令,可以将DISTRIBUTE表数据重新分布到新节点,重分布过程中会中断所有事务。可以将REPLICATION表数据复制到新节点。 从datanode节点中回收数据 postgres= ALTER TABLE disttab DELETE NODE (datanode3);ALTER TABLEpostgres= ALTER TABLE repltab DELETE NODE (datanode3);ALTER TABLE 删除数据节点 Postgresql-XL并没有检查将被删除的datanode节点是否有replicated/distributed表的数据,为了数据安全,在删除之前需要检查下被删除节点上的数据,有数据的话,要回收掉分配到其他节点,然后才能安全删除。删除数据节点分为四步骤: 1.查询要删除节点dn3的oid postgres= SELECT oid, FROM pgxc_node;oid | node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id -------+-----------+-----------+-----------+-----------+----------------+------------------+-------------11819 | coord1 | C | 5432 | datanode1 | f | f | 188569664316384 | coord2 | C | 5432 | datanode2 | f | f | -119710263316385 | node1 | D | 5433 | datanode1 | f | t | 114854923016386 | node2 | D | 5433 | datanode2 | f | f | -92791069016397 | dn3 | D | 5430 | datanode1 | f | f | -700122826(5 rows) 2.查询dn3对应的oid中是否有数据 testdb= SELECT FROM pgxc_class WHERE nodeoids::integer[] @> ARRAY[16397];pcrelid | pclocatortype | pcattnum | pchashalgorithm | pchashbuckets | nodeoids ---------+---------------+----------+-----------------+---------------+-------------------16388 | H | 1 | 1 | 4096 | 16397 16385 1638616394 | R | 0 | 0 | 0 | 16397 16385 16386(2 rows) 3.有数据的先回收数据 postgres= ALTER TABLE disttab DELETE NODE (dn3);ALTER TABLEpostgres= ALTER TABLE repltab DELETE NODE (dn3);ALTER TABLEpostgres= SELECT FROM pgxc_class WHERE nodeoids::integer[] @> ARRAY[16397];pcrelid | pclocatortype | pcattnum | pchashalgorithm | pchashbuckets | nodeoids ---------+---------------+----------+-----------------+---------------+----------(0 rows) 4.安全删除dn3 PGXC$ remove datanode master dn3 clean 故障节点FAILOVER 1.查看当前集群状态 [postgres@gtm ~]$ psql -h xl1 -p 5432psql (PGXL 10r1.1, based on PG 10.6 (Postgres-XL 10r1.1))Type "help" for help.postgres= SELECT oid, FROM pgxc_node;oid | node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id-------+-----------+-----------+-----------+-----------+----------------+------------------+-------------11739 | coord1 | C | 5432 | xl1 | f | f | 188569664316384 | coord2 | C | 5432 | xl2 | f | f | -119710263316387 | datanode2 | D | 15432 | xl2 | f | f | -90583192516388 | datanode1 | D | 15432 | xl1 | t | t | 888802358(4 rows) 2.模拟datanode1节点故障 直接关闭即可 PGXC stop -m immediate datanode master datanode1Stopping datanode master datanode1.Done. 3.测试查询 只要查询涉及到datanode1上的数据,那么该查询就会报错 postgres= SELECT xc_node_id, count() FROM disttab GROUP BY xc_node_id;WARNING: failed to receive file descriptors for connectionsERROR: Failed to get pooled connectionsHINT: This may happen because one or more nodes are currently unreachable, either because of node or network failure.Its also possible that the target node may have hit the connection limit or the pooler is configured with low connections.Please check if all nodes are running fine and also review max_connections and max_pool_size configuration parameterspostgres= SELECT xc_node_id, FROM disttab WHERE col1 = 3;xc_node_id | col1 | col2 | col3------------+------+------+-------905831925 | 3 | 103 | foo(1 row) 测试发现,查询范围如果涉及到故障的node1节点,会报错,而查询的数据范围不在node1上的话,仍然可以查询。 4.手动切换 要想切换,必须要提前配置slave节点。 PGXC$ failover datanode node1 切换完成后,查询集群 postgres= SELECT oid, FROM pgxc_node;oid | node_name | node_type | node_port | node_host | nodeis_primary | nodeis_preferred | node_id -------+-----------+-----------+-----------+-----------+----------------+------------------+-------------11819 | coord1 | C | 5432 | datanode1 | f | f | 188569664316384 | coord2 | C | 5432 | datanode2 | f | f | -119710263316386 | node2 | D | 15432 | datanode2 | f | f | -92791069016385 | node1 | D | 15433 | datanode2 | f | t | 1148549230(4 rows) 发现datanode1节点的ip和端口都已经替换为配置的slave了。 本篇文章为转载内容。原文链接:https://blog.csdn.net/qianglei6077/article/details/94379331。 该文由互联网用户投稿提供,文中观点代表作者本人意见,并不代表本站的立场。 作为信息平台,本站仅提供文章转载服务,并不拥有其所有权,也不对文章内容的真实性、准确性和合法性承担责任。 如发现本文存在侵权、违法、违规或事实不符的情况,请及时联系我们,我们将第一时间进行核实并删除相应内容。
2023-01-30 11:09:03
94
转载
MySQL
...SQL作为一种常用的数据库维护系统,更是常常用于游戏数据的保存和维护。那么,怎么使用MySQL来更正游戏角色虚拟货币呢?接下来将为大家介绍。 链接数据库 首先,我们需要链接MySQL数据库。在指令行中输入如下指令: mysql -h主机名 -u用户名 -p密码 数据库名 其中,主机名可省略,默认为本机;用户名和密码则是在MySQL安装时设置的。 搜索游戏角色标识符 更正游戏角色虚拟货币必须知道游戏角色的标识符。因此,我们需要搜索相应的游戏角色的标识符。使用如下指令: SELECT id FROM role WHERE name='游戏角色名'; 其中,游戏角色名为需要更正虚拟货币的游戏角色名。 更正虚拟货币 找到相应的游戏角色的标识符后,我们就可以更正其虚拟货币了。使用如下指令: UPDATE role 设定 gold=虚拟货币数目 WHERE id=游戏角色标识符; 其中,虚拟货币数目为想要更正的虚拟货币数目,游戏角色标识符为上一步搜索到的游戏角色标识符。 完成 至此,游戏角色虚拟货币的更正就完成了。可以通过如下指令来检查更正是否成功: SELECT gold FROM role WHERE id=游戏角色标识符; 其中,游戏角色标识符为更正虚拟货币时使用的标识符。 总之,以上就是使用MySQL更正游戏角色虚拟货币的方法。当然,具体使用时需要根据实际情况进行调整,但基本思路是相同的。
2023-04-20 08:05:28
61
软件工程师
CSS
...建工具自动为类名添加唯一标识符,有效防止全局命名冲突,并实现样式封装。 另外, styled-components 作为CSS-in-JS库中的代表,它将CSS直接内联到JavaScript组件中,不仅实现了样式与组件逻辑的高度耦合,还支持主题切换、动态样式生成等功能,进一步推动了CSS模块化的进程。同时,这种编写方式可以更好地适应现代化框架如React、Vue等的应用场景,使得CSS维护更加灵活和高效。 此外,最新的Web Components标准也在探索CSS Shadow DOM的潜力,旨在提供一种原生的模块化解决方案,让组件样式在DOM层级上实现完全隔离,确保组件的可复用性和独立性。 综上所述,CSS模块化正不断进化,开发人员应持续关注并学习这些新技术和实践,以适应前端开发领域的快速发展,提升项目的可维护性和扩展性。
2023-02-21 14:04:27
464
幽谷听泉_t
Groovy
...编程领域,映射是一种数据结构,它将唯一的键与对应的值相关联。在Groovy中,映射以键值对的形式存储数据,允许开发者通过键来快速查找和操作对应的值。这种数据结构类似于现实世界中的字典,其中键是查找项的标识符,值是与该键关联的数据。 元编程(Metaprogramming) , 元编程是指编写程序去操作、生成或者修改其他程序的行为或结构的一种编程范式。在Groovy语言中,元编程特性允许程序员在运行时动态修改类和对象的行为,例如添加属性、方法,甚至改变现有方法的行为,增强了代码的灵活性和可扩展性。 函数式编程(Functional Programming) , 函数式编程是一种强调程序执行过程中的计算视为数学函数应用,并且尽量避免改变状态和可变数据的编程范式。在Groovy语言中,虽然主要支持面向对象编程,但也引入了函数式编程的特性,比如支持高阶函数、闭包以及对集合的操作等,使得开发者能够以更简洁、易于理解的方式处理复杂逻辑。 字面量创建映射(Literal Map Creation) , 这是一种直接在代码中定义并初始化映射的语法方式。在Groovy中,通过 key: value, ... 的形式可以一次性声明多个键值对,从而创建并初始化一个映射,这种方式提高了代码的可读性和编写效率。 迭代器(Iterator) , 迭代器是编程设计模式中的一种通用接口,用于顺序访问集合(如列表、映射等)中的元素,而无需暴露其底层表示。在Groovy中,映射提供了keySet()、values()和entrySet()方法分别返回包含所有键、所有值和所有键值对的迭代器,使得开发者可以通过循环遍历并处理映射的所有内容。
2023-06-22 19:47:27
692
青山绿水-t
转载文章
...此外,欧盟GDPR等数据保护法规的实施也使得邓白氏这样的第三方认证机构承担起更大的责任,他们不仅要确保企业的唯一标识准确性,还必须遵循严格的隐私保护政策,防止信息滥用。这也从侧面反映了邓白氏编码在构建安全可信的数字经济环境中的基石地位。 深入探究,邓白氏编码体系的背后是庞大的全球商业数据库,通过大数据分析与人工智能技术,Dun & Bradstreet能够提供详尽的企业背景调查、风险评估报告等增值服务,帮助企业进行合作伙伴筛选、市场准入策略制定等决策,大大提升了商业运作效率及安全性。 因此,无论是iOS开发者还是其他行业的企业,理解和掌握邓白氏编码的申请及使用,不仅可以提升自身在数字化时代的竞争力,更是顺应全球化趋势、强化合规运营的重要一环。
2024-03-15 12:18:54
507
转载
转载文章
...式。 GUID(全局唯一标识符) , 全局唯一标识符是一种由算法生成的长度固定、格式确定、保证全球唯一的字符串型标识符。在文章中提到的“电源方案 GUID”,指的是操作系统内部用于区分不同电源计划的独特标识,例如。 “卓越性能”模式 , 这是Windows 10操作系统中的一项高级电源管理模式,专为高性能硬件配置和专业应用场景设计,如企业版和工作站版用户。该模式旨在优化系统资源调度,减少不必要的后台活动,从而最大化提升处理器、内存和存储设备等硬件组件的性能表现,尤其适用于处理大量数据、进行复杂计算或运行高性能软件的专业场景。普通家庭版、商用版、专业版或教育版用户默认情况下无法看到此模式选项,但可通过特定命令开启。
2023-06-26 12:46:08
385
转载
Kafka
...随着企业规模的增长,数据量也在不断增加,单一数据中心的数据处理能力已经无法满足需求,因此需要将数据复制到多个数据中心进行分布式处理。Kafka这款分布式流处理神器,本身就自带了跨数据中心数据复制的绝活儿。这篇文会手把手教你如何玩转Kafka,通过调整它的那些配置参数,再配上灵活运用Kafka的API接口,就能轻松实现让数据在不同数据中心之间复制、传输,就像变魔术一样简单有趣。 二、Kafka的跨数据中心复制原理 Kafka的跨数据中心复制是基于它的Replication(复制)机制实现的。在Kafka中,每个Topic下的每个Partition都会有一个Leader和多个Follower。Leader负责接收生产者发送的消息,并将消息传递给Follower进行复制。当Leader节点突然撂挑子罢工了,Follower里的小弟们可不会干瞪眼,它们会立马推选出一个新的Leader,这样一来,咱们整个系统的稳定性和可用性就能得到妥妥的保障啦。而跨数据中心复制这回事儿,其实就像是把Leader节点这位“数据大队长”派到其他的数据中心去,这样一来,各个数据中心之间的数据就能手牵手、肩并肩地保持同步啦。 三、如何设置Kafka的跨数据中心复制 1. 设置Zookeeper 在进行跨数据中心复制之前,需要先在Zookeeper中设置好复制组(Cluster)。复制组就像是由一群手拉手的好朋友组成的,这些好朋友其实是一群Kafka集群。每个Kafka集群都是这个大家庭中的一个小分队,它们彼此紧密相连,共同协作。咱们现在得在Zookeeper这家伙里头建一个新的复制小组,然后把所有参与跨数据中心数据同步的Kafka集群小伙伴们都拽进这个小组里去。 2. 配置Kafka服务器 在每个Kafka服务器中,都需要配置复制组相关的参数。其中包括: - bootstrap.servers: 用于指定复制组中各个Kafka服务器的地址。 - group.id: 每个客户端在加入复制组时必须指定的唯一标识符。 - replication.factor: 用于指定每个Partition的副本数量,也就是在一个复制组中,每个Partition应该有多少个副本。 - inter.broker.protocol.version: 用于指定跨数据中心复制时使用的网络协议版本。 四、使用Kafka API进行跨数据中心复制 除了通过配置文件进行跨数据中心复制之外,还可以直接使用Kafka的API进行手动操作。具体步骤如下: 1. 在生产者端,调用send()方法发送消息到Leader节点。 2. Leader节点接收到消息后,将其复制到所有的Follower节点。 3. 在消费者端,从Follower节点获取消息并进行处理。 五、总结 总的来说,通过设置Kafka的复制组参数和使用Kafka的API接口,我们可以轻松地实现在跨数据中心之间的数据复制。而且你知道吗,Kafka有个超赞的Replication机制,这玩意儿就像给数据上了个超级保险,让数据的安全性和稳定性杠杠的。哪怕某个地方突然出了状况,单点故障了,也能妥妥地防止数据丢失,可牛掰了! 六、致谢 感谢阅读这篇关于如何确保Kafka的跨数据中心复制的文章,如果您有任何疑问或建议,请随时与我联系,我将竭诚为您服务!
2023-03-17 20:43:00
531
幽谷听泉-t
Go-Spring
...地方织入到目标对象的方法调用过程中,实现了功能模块的重用和解耦。 XMLbean配置文件 , 在Go-Spring框架中,XMLbean配置文件是一个采用XML语法编写的文件,用于定义应用中的Bean以及它们之间的依赖关系、初始化属性值等信息。开发人员通过在该文件中声明Bean,框架会根据配置动态地创建和管理Bean的生命周期,这是实现IoC的重要方式。例如,在文中提到的XMLbean定义文件结构中,<bean>标签用于定义一个Bean实例,其属性id用于标识Bean的唯一名称,而class属性则指定了Bean的实现类。
2023-04-04 12:42:35
472
星河万里
站内搜索
用于搜索本网站内部文章,支持栏目切换。
知识学习
实践的时候请根据实际情况谨慎操作。
随机学习一条linux命令:
journalctl
- 查看系统日志。
推荐内容
推荐本栏目内的其它文章,看看还有哪些文章让你感兴趣。
2023-04-28
2023-08-09
2023-06-18
2023-04-14
2023-02-18
2023-04-17
2024-01-11
2023-10-03
2023-09-09
2023-06-13
2023-08-07
2023-03-11
历史内容
快速导航到对应月份的历史文章列表。
随便看看
拉到页底了吧,随便看看还有哪些文章你可能感兴趣。
时光飞逝
"流光容易把人抛,红了樱桃,绿了芭蕉。"